【问题标题】:Get-ACL Returning Null Values with Invoke-CommandGet-ACL 使用 Invoke-Command 返回 Null 值
【发布时间】:2018-03-23 15:45:10
【问题描述】:

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

$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 返回的“所有者”属性始终返回 null 或“builtin\administrators”,而不是实际所有者.当我在本地机器上运行完全相同的命令(减去所有绒毛)时,它会返回正确的用户作为所有文件的所有者。但是,一旦我使用脚本对远程对象运行它,所有权数据就不再正确。

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

【问题讨论】:

  • 但是本地机器上的文件不是远程文件。正确的测试是将 RDP 转移到远程机器并在本地发出这些命令,然后然后确定结果是否正确。另外,如果我没有误读您在做什么,您是在远程文件上直接运行Get-Acl——您希望它如何工作? $_.fullname 中有什么内容?你不应该在Get-ACL within Invoke-Command 做吗?
  • 因此,当我的同事在本地机器上运行命令(Get-ChildItem "C:\Users\coworker\Desktop\" | select Name,CreationTime,LastWriteTime,Length,Mode, @{n="Owner";e={(Get-Acl $_.fullname).owner}} )时,他得到了预期的结果:他被列为大多数文件的所有者(域\同事)。当我使用脚本时检查同一个目录,在 Owner 字段中没有返回任何内容,或者它被列为 builtin\administrators 用户。
  • 我的意思是,如果您使用Invoke-Command 获取远程文件列表,那么您不会检查相同的文件,而是在本地运行Get-Acl为其提供远程文件的名称文件。它会尝试获取文件的 ACL,因为它存在于您的机器上,我预计在大多数情况下会以“不存在”失败,或者至少返回错误的所有者。
  • 我现在明白你的意思了。我在使用相同基本结构的其他脚本上没有遇到这个问题,所以这是出乎意料的。那我如何将它传递给 Invoke-Command 块?我可以像$script = (invoke-command -ComputerName $a -Credential $cred -ScriptBlock {param($path) Get-ChildItem -Force -LiteralPath $path | select Name, CreationTime, LastWriteTime, Length, Mode, @{n="Owner";e={(Get-Acl $_.Name).owner}} } -ArgumentList $b) 一样将 Select 和 Get-ACL 部分通过管道传输到末尾吗?

标签: powershell invoke-command


【解决方案1】:

我可以通过编辑我的 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多