Search code examples
powershellinvoke-command

Get-ACL Returning Null Values with Invoke-Command


So I have a script here that is meant to remotely get a listing of all the files in a directory along with some basic information.

$a is the computer name, $b is the file path, $d is the output file name (predetermined), and $e is the output type.

$script = (invoke-command -ComputerName $a -Credential $cred -ScriptBlock {param($path) get-childitem -Force -literalpath $path} -ArgumentList $b) 

switch ($e)
{
    json
    {
        $script | select Name,CreationTime,LastWriteTime,Length,Mode, @{n="Owner";e={(Get-Acl -LiteralPath $_.fullname).owner}} | ConvertTo-Json -Compress | Out-File $outputfilepath\"$d".json
    }
    csv
    {
        $script | select Name,CreationTime,LastWriteTime,Length,Mode, @{n="Owner";e={(Get-Acl -LiteralPath $_.fullname).owner}} | ConvertTo-Csv | Out-File $outputfilepath\"$d".csv
    }
}

My problem is this the "Owner" property that gets returned by Get-ACL is always returning either null or "builtin\administrators", instead of the actual owner. When I run the exact same command (minus all the fluff) on my local machine, it returns the proper user as the owner for all the files. However once I use the script to run it against something remote, the ownership data stops being correct.

The credentials I am using should not have any permission issues, so I am confused as to why I am not pulling the correct data.


Solution

  • I was able to solve the issue by editing my Invoke-Command to work like the below, moving the Select statement and the Get-ACL up into the block itself

    $script = (invoke-command -ComputerName $a -Credential $cred -ScriptBlock {param($path) Get-ChildItem -Force -LiteralPath $path | select Name, CreationTime, LastWriteTime, Length, Mode, @{Label=”Owner”; Expression={(Get-ACL ($path+$_.Name)).Owner}} } -ArgumentList $b)
    

    And then my conversions into

    $script | ConvertTo-Json -Compress | Out-File $outputfilepath\"$d".json