我正在使用PowerShell從遠程服務器列表中收集數據,然後將其轉換爲JSON對象。一切工作正常,但我得到了一些非常奇怪的輸出,我似乎無法排除。從Invoke-Command返回的無關數據
我試過管道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
,輸出:
{
"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
}
]
}
我要麼不正確集成該解決方案,或者它不適用於這類問題。 我已更新問題以反映此問題。 – DiscOH
不是你。我認爲這是因爲這些是反序列化的對象,而且它們不像預期的那樣行事。 – mjolinor