Get-ACL使用Invoke-Command返回空值

时间:2018-03-23 15:45:11

标签: powershell invoke-command

所以我这里有一个脚本,用于远程获取目录中所有文件的列表以及一些基本信息。

$ a是计算机名称,$b是文件路径,$d是输出文件名(预定义),$e是输出类型。

$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
    }
}

我的问题是Get-ACL返回的“Owner”属性总是返回 null 或“ builtin\administrators ”,而不是实际所有者。当我在本地计算机上运行完全相同的命令(减去所有绒毛)时,它会返回适当的用户作为所有文件的所有者。但是,一旦我使用脚本针对某些东西运行它,所有权数据就会停止正确。

我使用的凭据不应该有任何权限问题,所以我很困惑为什么我没有提取正确的数据。

1 个答案:

答案 0 :(得分:0)

我能够通过编辑我的Invoke-Command来解决这个问题,如下所示,将Select语句和Get-ACL移动到块本身

$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)

然后我转换成

$script | ConvertTo-Json -Compress | Out-File $outputfilepath\"$d".json
相关问题