2016-09-23 58 views
1

這是一個數據類型轉換問題。PowerShell將輸出轉換爲散列表數組(數據類型轉換)

我想從SCCM中獲取計算機的名稱並將其提供到SCCM報告中。報告commandlet接收散列表,其中變量必須命名爲「計算機名稱」。該值是計算機名稱。

ex. $computer = @{"Computer Name" = "MyComp01"} 

輸出Commandlet示例(從SCCM獲取計算機名稱)這起作用。

$unknownoutputtype = Get-CMDevice -CollectionName "My Collection Computers" | select @{N="Computer Name";E={$_.Name}} 

--Output-- 

Computer Name 
------------- 
MyComp01 
MyComp02 
MyComp03 
MyComp04 
MyComp05 
MyComp06 

PS> $unknownoutputtype.gettype() 

IsPublic IsSerial Name          BaseType 
-------- -------- ----          -------- 
True  True  Object[]         System.Array 

Recieving命令行開關的例子(處理查詢)這工作:

$computer = @{"Computer Name" = "MyComp01"} 
invoke-cmreport -ReportPath "Software - Companies and Products/Software registered in Add Remove Programs on a specific computer" -reportparameter $Computer -OutputFormat excel 

我需要的 「GET-CMDevice」 線路輸出如下類型。

PS> $Computer.gettype() 

IsPublic IsSerial Name          BaseType 
-------- -------- ----          -------- 
True  True  Hashtable        System.Object 

我失敗的嘗試

PS> Get-CMDevice -CollectionName "My Collection Computers" |select @{N="Computer Name";E={$_.Name}} | Foreach-object {invoke-cmreport -ReportPath "Software - Companies and Products/Software registered in Add Remove Programs on a specific computer" -SiteCode "MySite" -reportparameter $_ -OutputFormat Excel} 

錯誤輸出:

Invoke-CMReport : Cannot bind parameter 'ReportParameter'. Cannot convert value "@{Computer Name=MyComp01}" to type "System.Collections.Hashtable". Error: "Cannot convert the "@{Computer Name=MyComp01}" value of type 
"Selected.Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine.WqlResultObject" to type "System.Collections.Hashtable"." 
At line:1 char:280 
+ ... s on a specific computer" -SiteCode "{removed}" -reportparameter $_ -Output ... 
+                ~~ 
    + CategoryInfo   : InvalidArgument: (:) [Invoke-CMReport], ParameterBindingException 
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ConfigurationManagement.Cmdlets.Reporting.Commands.InvokeReportCommand 
+0

嘗試的選擇,而不是: '的foreach對象{@ {$ _名稱= $ _名稱}}' 選擇返回對象,而不是散列 – Sigitas

回答

2

Select-Object總是輸出PSCustomObject,而不是一個哈希表。

只是構建ForEach-Object體內的哈希表調用Invoke-CMReport前:

Get-CMDevice -CollectionName "My Collection Computers" |Select-Object -ExpandProperty Name | Foreach-object { 
    $ReportParameter = @{ 'Computer Name' = $_ } 
    invoke-cmreport -ReportPath "Software - Companies and Products/Software registered in Add Remove Programs on a specific computer" -SiteCode "MySite" -reportparameter $ReportParameter -OutputFormat Excel 
} 
+0

Mathias R. Jessen是一位聖人和學者。謝謝。 – Aaron

+0

@Aaron如果我的答案解決了您的問題,我非常榮幸^ _ ^,請點擊左側的複選標記接受它 –