Invoke-Command 不 return 基于 PSObject 属性的对象

Invoke-Command doesn't return objects based on PSObject properties

使用下面的脚本,它的工作是联系远程机器并提取本地组的成员资格,然后创建自定义 PS 对象以根据需要对数据进行排序。当从本地机器上拉取代码时代码工作正常并且 PSCustomObject 被接受。但是,在远程调用中,对象没有正确存储在 PScustomObjects 中。

寻找一些 insight/help 到 return 格式正确的

$params = $args
$Target = $args[0]
$PrivUser = "$($params[1])$($params[2])"
$PrivPwd = ConvertTo-SecureString -String $params[3] -AsPlainText -Force
$cred = [pscredential]::new($PrivUser,$PrivPwd)
$Groups =  @('Administrators','Remote Desktop Users','Power Users')
$results = @()
try {
    Invoke-Command -ComputerName $Target -Credential $cred -HideComputerName -ScriptBlock {
          #Change $using:groups to $groups to pull local objects
        foreach($group in $using:Groups) 
        {
        foreach($member in Get-LocalGroupMember -Name $group) {
            [pscustomobject]@{
                Machine     = $env:COMPUTERNAME
                Group       = $group
                Member      = $member.Name
            }
        }
            $results += $output
        }
        return $results 
        
    }
 

} catch {
    throw "Unable to connect to target: $($args[0]) `n$_"
}

从本地机器输出

Machine Group                Member               
------- -----                ------               
BLT-SRV Administrators       BLT\Domain Admins    
BLT-SRV Administrators       BLT\svr.blt.ad.ss    
BLT-SRV Administrators       BLT\svr.blt.ss.ps    
BLT-SRV Administrators       BLT\svr.kiwi         
BLT-SRV Administrators       BLT-SRV\Administrator
BLT-SRV Administrators       BLT-SRV\BLT_Admin    
BLT-SRV Remote Desktop Users BLT\cpopopo  

远程机器的输出

Machine    : BLT-SRV
Group      : Administrators
Member     : BLT\Domain Admins
RunspaceId : 81840b66-4a15-467f-956f-1552b068847d

Machine    : BLT-SRV
Group      : Administrators
Member     : BLT\svr.blt.ad.ss
RunspaceId : 81840b66-4a15-467f-956f-1552b068847d

Machine    : BLT-SRV
Group      : Administrators
Member     : BLT\svr.blt.ss.ps
RunspaceId : 81840b66-4a15-467f-956f-1552b068847d

Machine    : BLT-SRV
Group      : Administrators
Member     : BLT\svr.kiwi
RunspaceId : 81840b66-4a15-467f-956f-1552b068847d

Machine    : BLT-SRV
Group      : Administrators
Member     : BLT-SRV\Administrator
RunspaceId : 81840b66-4a15-467f-956f-1552b068847d

Machine    : BLT-SRV
Group      : Administrators
Member     : BLT-SRV\BLT_Admin
RunspaceId : 81840b66-4a15-467f-956f-1552b068847d

Machine    : BLT-SRV
Group      : Remote Desktop Users
Member     : BLT\hgdghdh
RunspaceId : 81840b66-4a15-467f-956f-1552b068847d

您运行反对序列化。 查看 this post to understand 发生了什么。

排除 RunspaceId 属性 应该默认将对象显示为 Table:

Invoke-Command -ComputerName $Target -Credential $cred -HideComputerName -ScriptBlock {
    foreach($group in $using:Groups) {
        foreach($member in Get-LocalGroupMember -Name $group) {
            [pscustomobject]@{
                Machine     = $env:COMPUTERNAME
                Group       = $group
                Member      = $member.Name
            }
        }
    }
} | Select-Object * -ExcludeProperty RunspaceId

或者,您可以将 Invoke-Command 的结果通过管道传递给 Format-Table,以强制对象显示为 table:

Invoke-Command -ComputerName $Target -Credential $cred -HideComputerName -ScriptBlock {
    # code here
} | Format-Table -AutoSize

但是,请注意Format-Table专用于控制台显示,如果你想使用它,请不要使用它导出输出。

我不确定我是否理解您的问题,但我认为您得到的结果相同,但显示方式不同。您可以使用 Format-Table(或简单地 ft)强制 table 查看(您的第一个输出示例)。要获得列表视图(第二个),请使用 Format-List(或 fl)。下面的两个命令 return 结果相同,但显示方式不同:

Get-ChildItem -Path c:\ | Format-Table

Get-ChildItem -Path c:\ | Format-List