2014-02-21 39 views
2

我借用了一些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技能有點高級。有關如何防止對象中的一個項目的錯誤的任何建議?

回答

0

好像當$comp是一個項目,它不是一個數組。你需要「初始化」$comp作爲一個空的數組。我沒有看到它在你的代碼來了,但這樣的事情應該工作:

#This creates an empty array 
$comp = @() 
$comp += $foo 
+0

感謝您的指點。 – EdLipson

1

使用逗號操作符總是返回值作爲數組:

function result([string]$side) { 
    if ($ReturnVals) { 
     $obj = New-Object PSObject -Property @{ 
      'InputKey'= "$path$key"; 
      'SideIndicator' = $side; 
      'ReferenceValue' = $refValue; 
      'DifferenceValue' = $difValue; 
     } 
    } 
    else { 
     $obj = New-Object PSObject -Property @{ 
      'InputKey'= "$path$key"; 
      'SideIndicator' = $side; 
     } 
    } 
    return ,$obj 
} 
2

的Get-枚舉器是一種數組方法。如果該函數只返回一個對象,則不會獲取數組,因此該方法不起作用。這是正常和預期的行爲。

您可以修改該函數以始終返回一個數組,但這是一種非標準方法。您希望該功能儘可能像其他cmdlet一樣運行併發揮功能。更好地強制它進入函數外的數組,在你的主腳本中:

$comp = @(result) 
+1

正確的工作,它實際上將是我的第一個答案,但後來決定只是改變他給的代碼...任何信息,爲什麼總是返回值作爲一個數組是一種非標準的方法?只是好奇 – Cole9350

+0

它是非標準的,因爲它會產生意想不到的行爲。如果多個對象被返回,我只希望得到一個數組,因爲這就是每個其他cmdlet或函數我能想到的工作方式(恕我直言)。 – mjolinor

+0

但是你正在做同樣的事情......總是確保它是一個數組,當調用普通的PS cmdlet時這不是必需的。我認爲在功能級別上更改標準會更加標準,因爲函數是要重用的,所以無論您怎麼稱呼它,其行爲方式都是一樣的。但我想這只是我的看法 – Cole9350

0

這些都是很好的答案。我將不得不嘗試一下,看看我能理解它爲什麼起作用,爲什麼它不起作用。我制定了一個強大的解決方案,從GetType中查看項目類型,並在PSOBJECT或OBJECT時採取行動。從兩個測試腳本,有多個項目它是一個System.Object,只有一個它是一個... PSCustomObject。

********************** 
Windows PowerShell Transcript Start 
Start time: 20140221140133 

2014-02-21 2:02:01 PM Reading File 1 
2014-02-21 2:02:02 PM Loading File 1 to Dictionary 
2014-02-21 2:02:28 PM Reading File 2 
2014-02-21 2:02:29 PM Loading File 2 to Dictionary 
2014-02-21 2:02:56 PM Lines Compare 

    *** The value of comp variable is: @{InputKey=REPT.CRW.ARC_DIR_LST; SideIndicator=<>} @{InputKey=AD.CRW.PRDHOT07Y05; SideIndicator=<>} 
    *** The length of comp is: 2 
    *** The type is: System.Object[] 

2014-02-21 2:02:56 PM Writing Lines Compare Report 
2014-02-21 2:02:56 PM Differences 
2014-02-21 2:02:56 PM Compare Objects End 
..... 
2014-02-21 2:03:31 PM Reading File 1 
2014-02-21 2:03:32 PM Loading File 1 to Dictionary 
2014-02-21 2:03:58 PM Reading File 2 
2014-02-21 2:03:59 PM Loading File 2 to Dictionary 
2014-02-21 2:04:25 PM Lines Compare 

    *** The value of comp variable is: @{InputKey=AD.CRW.PRDHOT07Y05; SideIndicator=<>} 
    *** The length of comp is: 
    *** The type is: System.Management.Automation.PSCustomObject 

2014-02-21 2:04:25 PM Writing Lines Compare Report 
2014-02-21 2:04:25 PM Differences 
2014-02-21 2:04:25 PM Compare Objects End 

Windows PowerShell Transcript End 
End time: 20140221140438 
**********************