2016-09-23 61 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如果我的答案解决了您的问题,我非常荣幸^ _ ^,请点击左侧的复选标记接受它 –