这是一个数据类型转换问题。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
尝试的选择,而不是: '的foreach对象{@ {$ _名称= $ _名称}}' 选择返回对象,而不是散列 – Sigitas