从 Invoke-Command 返回的无关数据

Dis*_*cOH 4 powershell json remote-server invoke-command

我正在使用 PowerShell 从远程服务器列表收集数据,然后将其转换为 JSON 对象。一切工作正常,但我得到了一些我似乎无法排除的非常奇怪的输出。

我尝试过管道传输Invoke-Command结果并排除属性。我还尝试从返回的哈希文件中手动删除这些项目,但我似乎无法让它们消失。

我缺少什么?

编辑:

为了找出问题所在,这里有一个简化但仍然损坏的脚本:

$returnedServer = @{}
$pass = cat "C:\...\securestring.txt" | convertto-securestring
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist "UserName",$pass
$s = @("xx.xxx.xxx.xxx","xx.xxx.xxx.xxx") 


foreach($server in $s)
{
    $returnedServer.$server += ,(Invoke-Command -ComputerName $server -ScriptBlock 
{
1
}-credential $mycred | select -ExcludeProperty PSComputerName,RunSpaceID,PSShowComputerName) 

$returnedServer| ConvertTo-Json
Run Code Online (Sandbox Code Playgroud)

哪个输出:

{
"xx.xxx.xxx.xxx":  [
                       {
                           "value":  1,
                           "PSComputerName":  "xx.xxx.xxx.xxx",
                           "RunspaceId":  "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
                           "PSShowComputerName":  xxxx
                       }
                   ],
"xx.xxx.xxx.xxx":  [
                       {
                           "value":  1,
                           "PSComputerName":  "xx.xxx.xxx.xxx",
                           "RunspaceId":  "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"",
                           "PSShowComputerName":  xxxx
                       }
                   ]
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*mer 6

这篇文章确实很老了,但六年后我找不到可以接受的答案,所以我自己写了。

$invokeCommandResults | ForEach-Object {
  $_.PSObject.Properties.Remove('PSComputerName')
  $_.PSObject.Properties.Remove('RunspaceId')
  $_.PSObject.Properties.Remove('PSShowComputerName')
}
Run Code Online (Sandbox Code Playgroud)