Welcome to 2009 - where we've got a LOT of great stuff lined up for you. First of all, our "Monsters of Tech" class in Denver this coming April is still open for registration, and we've lowered the per-student price to just $2,000 for the entire week. Check out the Read the full story »
Features

These ongoing series of articles bring you new content on a weekly, biweekly, or monthly basis. Check back often!

Jack of All Tech

The minimum you need to know about the varied technologies that you support - even though they’re not really your thing.

Just for Fun

A little off-topic, but still eminently interesting: Gadgets, hobbies, contests, giveaways, and whatever pops into our minds.

Musings

Our take on what’s happening in the IT world - and most importantly how we think it’ll affect you. What do YOU think?

Windows Admin

Managing (and automating) Windows servers and desktops, including scripting and Windows PowerShell

Home » Features, Windows PowerShell

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!


Technorati Tags: , ,

Related Posts

Rate this Post

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...