我借用了一些PowerShell代碼來比較哈希表,並且它返回一個自定義對象,其中包含哈希條目的名稱和差異指示。我想輸出返回的差異。powershell psobject getenumerator one
對象:
function result([string]$side) {
if ($ReturnVals) {
New-Object PSObject -Property @{
'InputKey'= "$path$key";
'SideIndicator' = $side;
'ReferenceValue' = $refValue;
'DifferenceValue' = $difValue;
}
}
else {
New-Object PSObject -Property @{
'InputKey'= "$path$key";
'SideIndicator' = $side;
}
}
}
當返回的對象的工作,如果是空或有多個入口,一切都很好,GetEnumerator的做所需要的輸出被分在文件:
if ($comp -eq $Null) {
write-host $d "No Differences"
out-file -filepath $of -inputobject "`nNo Differences" -Encoding UTF8 -append
}
else {
write-host $d "Differences"
$comp.GetEnumerator() | Sort-Object -property InputKey |
out-file -filepath $of -append -Encoding UTF8
}
如果有一個差,PowerShell中引發錯誤作爲對象沒有方法GetEnumerator:
方法調用失敗,因爲[系統。 Management.Automation.PSCustomObject]不包含名爲'GetEnumerator'的方法。
我試圖使用.count來查看是否有一個區別,但我沒有得到一個只有一個計數。我得到2個或更多的計數。
自定義對象對於我的PowerShell技能有點高級。有關如何防止對象中的一個項目的錯誤的任何建議?
感謝您的指點。 – EdLipson