Help with the script. Output doesn't contain any data

Hello,
I have this script that is working on some servers but not the others. I am confused and dont know what the issue is.

So, the script is working and I am able to get NTFS report with owners on local machine and one other server. But now I ran the same script on other servers (I modified the directory in the script to match the other server). The script gives output file and it is 0 kb and nothing in the report.....I checked the path to directory that I am querying - and it is correct. Also, tried specifying just c: or e: local drive instead of network share, and it still produces report with nothing in it. How come it collects data on some computers and doesn't collect anything on others.....

In particular, I specify path like this:
E:\DFSShare\NYC\Users

I can access the share with no issues. Why is NTFS owner report empty?

could you please maybe suggest other scripts that would do the same for netwrok shares to give me list of folder owners for the files?

I need in the output the Path, and the Owner. Would be even better if I have the Path, Permissions, and the current owner
Path      Owner
E:\BACKUP-NTFSPermissions      BUILTIN\Administrators
E:\c drive expansion      BUILTIN\Administrators
E:\c drive expansion\Dell Extpart      BUILTIN\Administrators


Here is the current script:

function Get-FolderOwner {
    param(
        [string] $Path = "c:",
        [int] $RecurseDepth = 10
    )

    $RecurseDepth--

    Get-ChildItem $Path -Directory | ForEach-Object {
        # Get-Acl throws terminating errors, so we need to wrap it in
        # a ForEach-Object block; included -ErrorAction Stop out of habit
        try {
            $Owner = $_ | Get-Acl -ErrorAction Stop | select -exp Owner
        }
        catch {
            $Owner = "Error: {0}" -f $_.Exception.Message
        }

        [PSCustomObject] @{
            Path = $_.FullName
            Owner = $Owner
        }

        if ($RecurseDepth -gt 0) {
            Get-FolderOwner -Path $_.FullName -RecurseDepth $RecurseDepth
        }
    }
}

Try
{
    Get-FolderOwner C: -RecurseDepth 100 | Export-Csv Owner-Exportv2.csv

}
Catch [System.OutOfMemoryException]
{
   # Restart-Computer localhost
}
Catch
{
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    # Send-MailMessage -From ExpensesBot@MyCompany.Com -To WinAdmin@MyCompany.Com -Subject "HR File Read Failed!" -SmtpServer EXCH01.AD.MyCompany.Com -Body "We failed to read file $FailedItem. The error message was $ErrorMessage"
    Break
}
Finally
{
    $Time=Get-Date
    "This script made a read attempt at $Time" | out-file c:\scripts\NTFSscriptlog.log -append
}
creative555Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
Get-FolderOwner {
    param(
        [string] $Path = "c:",
        [int] $RecurseDepth = 10
        )
$DirPermissions = @()

    $RecurseDepth--
    $directories = Get-ChildItem -path $Path -Directory -Recurse
ForEach($dir in $directories) 
    {
    try {
        $Directory =  Get-Acl -Path $dir.Fullname -ErrorAction SilentlyContinue 
        }
    catch {
            $Owner = "Error: {0}" -f $_.Exception.Message
            $owner
        }

$Permissions = New-Object -TypeName PSObject
$Permissions | Add-Member -Type NoteProperty  -Name Path -Value $dir.FullName
$Permissions | Add-Member -Type NoteProperty  -Name Owner -Value $Directory.Owner
$DirPermissions += $Permissions
}
$DirPermissions | Export-Csv -nti -Path C:\scripts\Owner-Exportv2.csv
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David Johnson, CD, MVPOwnerCommented:
your original script actually was checking all files not just directories.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.