Weekly Shell: Knowing the WMI Server Name
Last week, I showed you the WMI system properties that PowerShell can work with. This week, I promised to show you how they can be useful. Here’s the scenario: You want to query disk inventory information from multiple computers, and produce some sort of simple, formatted report. You’ve got the computer names in a text file, which lists one computer name per line. You might start with this simple command:
Get-WmiObject Win32_LogicalDisk -filter “DriveType=3″ -computerName (Get-Content C:\Computers.txt) | Format-Table DeviceID,Size,FreeSpace
Believe it or not, your targeted computers can be running any version of Windows back to NT 4.0! The problem is that your output won’t contain the computer name - meaning you won’t know which disk goes with which computer. That’s where WMI’s system properties come into play: Just add __SERVER to the list of properties (that’s two underscores on the front, there):
Get-WmiObject Win32_LogicalDisk -filter “DriveType=3″ -computerName (Get-Content C:\Computers.txt) | Format-Table __SERVER,DeviceID,Size,FreeSpace
Because each WMI object is tagged with the name of the computer it came from, you can simply use that information as part of your output. Want the table in a file?
Get-WmiObject Win32_LogicalDisk -filter “DriveType=3″ -computerName (Get-Content C:\Computers.txt) | Format-Table DeviceID,Size,FreeSpace | Out-File c:\DiskReport.txt
Prefer a CSV file?
Get-WmiObject Win32_LogicalDisk -filter “DriveType=3″ -computerName (Get-Content C:\Computers.txt) | Select DeviceID,Size,FreeSpace | Export-CSV c:\DiskSpace.csv
Or an HTML Table?
Get-WmiObject Win32_LogicalDisk -filter “DriveType=3″ -computerName (Get-Content C:\Computers.txt) | Select DeviceID,Size,FreeSpace | ConvertTo-HTML | Out-File C:\DiskInventory.html
PowerShell’s flexibility lets you accomplish all this by just stringing together a few commands - no need to write a script at all!




