这是数据类型转换问题。
我正在尝试从SCCM中获取计算机的名称并将其提供给SCCM报告。报告命令行开关接收哈希表,其中变量必须命名为“计算机名”。值是计算机名称。
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
接收Commandlet示例(处理查询)这有效:
$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 :(得分: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
}