2011-12-24 11 views
1

我正在使用Get-WmiObject Win32_service並將輸出傳輸到CSV。 我的腳本正在收集運行的服務,但我想在我的csv的同一行顯示osname,ip和域的對象(這樣我就可以輕鬆地使用Excel進行排序)。使用Win32_Computersystem與Win32_service在同一行和output-csv

有沒有辦法將兩者結合?

Get-WmiObject Win32_Computersystem 
Get-WmiObject Win32_service 

或者,也許我可以從我的Get-QADComputer一些變量潛入該行:

Get-WmiObject Win32_service -Computername $server.name -Filter "State='Running'" | select -Property SystemName,Name,Status,State,Startmode,DisplayName | Export-Csv -Append -NoClobber -NoTypeInformation -Path $reportfile 

回答

3

使用New-Object cmdlet爲每個服務對象創建一個新對象,並從Win32_OperatingSystem WMI類(os標題在Win32_ComputerSystem類中不可用)添加您想要的屬性(osname)。

$ComputerName = 'PC1' 

$os = Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName 

Get-WmiObject Win32_Service -ComputerName $ComputerName -Filter "State='Running'" | Foreach-Object{ 

    New-Object -TypeName PSObject -Property @{ 
     SystemName=$_.SystemName 
     Name=$_.Name 
     Status=$_.Status 
     State=$_.State 
     StartMode=$_.Startmode 
     DisplayName=$_.DisplayName 
     IPAddress = [System.Net.Dns]::GetHostAddresses($_.SystemName)[0].IPAddressToString 
     OSName = $os.Caption    
    } | Select-Object SystemName,Name,Status,State,StartMode,DisplayName,IPAddress,OSName 

} | Export-Csv -Append -NoClobber -NoTypeInformation -Path $reportfile 
+0

非常感謝你謝伊!我不瞭解New-Object。 – Sune 2011-12-28 19:23:33

+0

非常感謝謝伊!我不瞭解New-Object。但是.. IPAddress只返回System.Net.IPAddress []和$ os.caption是否返回微軟? Windows服務器? 2008標準。我怎樣才能簡單地獲得IP地址和正確格式化的操作系統版本名稱?順便祝聖誕快樂! – Sune 2011-12-28 19:30:05

+0

是的,它返回一個地址數組。我更新了代碼,以便獲得第一個地址。 – 2011-12-29 08:25:08

相關問題