使用PowerShell从WQL中的不同表返回多个属性

时间:2018-02-02 14:59:00

标签: powershell sccm wql

我试图从SCCM获取查询以在PowerShell中返回相同的输出。在SCCM中运行查询返回两列:SMS_G_SYSTEM_COMPUTER_SYSTEM.name和SMS_G_SYSTEM_SYSTEM_ENCLOSURE.chassistypes,用于给定的一组机箱类型

然后我尝试将查询语言直接粘贴到powershell中的WQL查询中;

$computername = "sccmserver"
$namespace = "root\sms\site_sitecode"
$query = @"select SMS_G_System_COMPUTER_SYSTEM.Name,SMS_G_System_SYSTEM_ENCLOSURE.chassistypes from  SMS_R_System inner join SMS_G_System_COMPUTER_SYSTEM on SMS_G_System_COMPUTER_SYSTEM.ResourceID = SMS_R_System.ResourceId inner join SMS_G_System_SYSTEM_ENCLOSURE on SMS_G_System_SYSTEM_ENCLOSURE.ResourceId = SMS_R_System.ResourceId where SMS_G_System_SYSTEM_ENCLOSURE.ChassisTypes in ("1","2","3","4","6","7","9","10","15")
"@
Get-WMIObject -Query $query -computername $computername -namespace $namespace

返回预期的对象,但没有属性。相反,我得到:

SMS_G_System_COMPUTER_SYSTEM  : System.Management.ManagementBaseObject
SMS_G_System_SYSTEM_ENCLOSURE : System.Management.ManagementBaseObject

如果我运行相同的查询但是告诉它只选择一个属性,它按预期工作: 例如 - ChassisTypes : 1

如何让它正确返回两个值?

1 个答案:

答案 0 :(得分:1)

首先,我试试:

Get-CimInstance -Query $query -computername $computername -namespace $namespace

该命令通常可以更好地格式化WMI / CIM输出,因为它在PowerShell v1.0之后出现。

我对直接答案的猜测是:

Get-WMIObject -Query $query -computername $computername -namespace $namespace |
    Select-Object -Property @{n='Name';e={$_.SMS_G_System_COMPUTER_SYSTEM.Name}}, @{n='chassistypes';e={$_.SMS_G_System_SYSTEM_ENCLOSURE.chassistypes}}