2

I am having the below code to get the data from remote servers. thanks to @Santiago Squarzon

$serverlist = Get-Content -Path "C:\ServerList.txt"

# Collect results here
$result = Invoke-Command -ComputerName $serverlist -ScriptBlock {
    $paths_list = $env:Path -Split [System.IO.Path]::PathSeparator
    foreach($sys_Path in $paths_list)
    {
        $Permissions = (Get-Acl -Path $sys_Path).Access
        foreach($acl in $Permissions)
        {
            if(-not $acl.IdentityReference)
            {
                continue
            }   

            [pscustomobject]@{
                ComputerName     = $env:ComputerName
                SystemFolderPath = $sys_Path
                IdenityReference = $acl.IdentityReference.Value
                FileSystemRights = $acl.FileSystemRights
            }
        }
    }
} -HideComputerName

$result | Export-Csv -Path "C:\status_report.csv" -NoTypeInformation

But I am getting below error while executing it

Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or
empty, and then try the command again.
    + CategoryInfo          : InvalidData: (:) [Get-Acl], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetAclCommand
    + PSComputerName 

Please let me know on this.

Empty Coder
  • 589
  • 6
  • 19
  • 2
    Could it be that `$env:Path` contains an empty path (like two `;;` with no content)? – Patrick Jan 04 '22 at 12:24
  • Try with `$paths_list = $env:Path -Split [System.IO.Path]::PathSeparator -ne ''`. I'm wondering if this happens on all servers or only one. And if it's only one, does the `$env:Path` variable on that server look ok? – Santiago Squarzon Jan 04 '22 at 12:42
  • @SantiagoSquarzon: for the first server itself I am facing this issue. have to check for rest. $env:Path is fine. values are as expected – Empty Coder Jan 04 '22 at 13:01
  • 1
    Are these servers all Windows based? I believe for Windows the separator to use is the semi-colon `;`, but in linux it apparently is the colon `:` (found that [here](https://stackoverflow.com/a/2228021/9898643)) – Theo Jan 04 '22 at 14:19
  • 2
    To be on the safe side, do `$paths_list = $env:Path -split [System.IO.Path]::PathSeparator | Where-Object { $_ -match '\S' }`, so you can be certain there are no empty or whitespace-only elements in your array – Theo Jan 04 '22 at 14:26
  • 1
    @Theo I learnt that too a few days ago, that's why I started using `[System.IO.Path]::PathSeparator` hehe – Santiago Squarzon Jan 04 '22 at 15:07

1 Answers1

2

Might adding the following check before $Permissions = (Get-Acl -Path $sys_Path).Access would resolve the issue:

if (($sys_Path -eq $null) -or ($sys_Path -eq '') ) {
    continue
}
Patrick
  • 2,128
  • 16
  • 24