2014-04-25 57 views
1

我想我需要幫助的是將多個WMI查詢合併到一個表中。將多個WMI查詢合併爲一個格式表

例如:

Get-WmiObject Win32_Processor -ComputerName $server | Format-Table PSComputerName,DataWidth 

這很好,但我也需要操作系統版本在同一行。爲此,我必須查詢Win32_OperatingSystem並提取Caption屬性。那麼如何將兩個單獨的WMI查詢放到同一個表中?

我已經試過如下:

$os = gmi Win32_Processor -Computername $server 
$cpu = gmi Win32_OperatingSystem -Computername $server 

然後,我能得到我想要輕易的屬性:

$os.PSComputerName,$cpu.Caption,$os.DataWidth 

但是從我讀過,格式表只接受數據一條管道。它似乎不知道如何處理(變量)做(財產)

理想情況下,我能夠作爲腳本塊的最後一個cmdlet的運行:

Format-Table $os.PSComputername,$cpu.Caption,$os.DataWidth 

當然的,但是,格式表總是在行的末尾,從不在開始。

回答

1

您可以使用該對象中的一個Calculated Property和其他正常管道:

$os = gwmi Win32_OperatingSystem -Computername $server 
gwmi Win32_Processor -ComputerName $server | Format-Table @{name="ComputerName"; exp={$os.PSComputerName}},Caption,DataWidth 
+0

這是我推薦的。或者,您可以將所有內容合併到一個大表中,如果此解決方案不適用於您,我可以提供幫助。 – TheMadTechnician

+0

就近了@zdan。足夠接近讓我錯誤的解決方案。你的答案從Win32_Processor中獲取Caption,這不是我所需要的。我倒置了你的答案(加上一些trial-n-error)來得到這個結果:'$ cpu = gwmi Win32_Processor -ComputerName $ server gwmi Win32_OperatingSystem -ComputerName $ server | Format-Table @ {name =「ComputerName」; exp = {$ cpu.PSComputerName}},Caption,@ {name =「DataWidth」; EXP = {$ cpu.DataWidth}}' –

0

如果我以前的編輯並不清楚:

$ CPU = gwmi Win32_Processor -ComputerName $ server

gwmi Win32_OperatingSystem -ComputerName $ server | Format-Table @ {name =「ComputerName」; exp = {$ cpu.PSComputerName}},Caption,@ {name =「DataWidth」; exp = {$ cpu.DataWidth}}