PowerShell 2.0 Nukes Grunt Work! – Part 2

When we left off, I’d found the date and time of the last Restore Point taken on this machine.

And luckily, since this data is coming out of WMI, I can extract the name of the computer I’m running this on from the same object.

WMI helpfully supplies a ‘__Server’ property with its objects (note: that’s two underscore characters, not one).

So with a little finagling, we get this cute little one-liner (cutting out that awkward ForEach-Object).

The @{Name= … Expression=…} syntax is called a Calculated Property when I use it in Select-Object, and it’s a great little tactic for either renaming a property with an ugly name (like Mr. UnderscoreUnderscoreServer there) or for performing some operation on a piece of data before exposing it to the light of day (like running that date formatter function over the CreationTime property). Check out the Get-Help file for Select-Object, and look at the details of the –Property parameter (you’ll see you can also use the word ‘Label’ instead of ‘Name’.)

This looks great so far. The next trick is accomplishing this same task across a few hundred computers.

Does Get-ComputerRestorePoint have a –computername property we can use to connect to each computer in the boss’s list? Rats — nope. But maybe there’s another way. Take a look at what kind of object that cmdlet produces. Get-Member is the best way to accomplish that — it shows you the name of the .NET class and the list of each of the properties and methods of the objects you pass in to it on the pipeline.

Okay — so it’s a WMI object. Maybe we’re better off accessing the restore points directly through Get-WMIObject. That cmdlet DOES have a –computername property, so I can target it at different computers from a central point.

Alas, Get-ComputerRestorePoint has a built-in method for doing conversion of date information to the DateTime format. This is one of those times where we need to make a trade-off. With the large numbers of computers to inventory, I’ll take the clunkier format conversion to get the multiple computer support of Get-WMIObject. Alternatively, we could have tackled this using PowerShell Remoting, but let’s save that for another time. Let’s look for a WMI class that supports restore points.

Note that the first attempt to search for a restore-related class flopped. That’s because Get-WMIObject –list only searches WMI’s ‘root\cimv2’ namespace by default. In fairness, that’s where most of the good stuff lives, but in this case what we are looking for isn’t there. So where is it? The class type from the earlier call to Get-Member gives a clue. Look after the pound sign. The second attempt finds the class we need.

So now we can rebuild our query to directly attack the problem with Get-WMIObject – like so:

Yep — that’s got it. Now we just need to target this at our list of computers, and we’re home free. If we drop the list of computer names from the boss into a text file (ComputerNames.txt, for example), we can just do this:

And we’re done. I can take that output and throw it into a file with Out-File, or a CSV file with Export-CSV. Problem solved. Days of wasted time saved. The boss gets his data, and I go back to designing our cloud strategy!

In this article

Join the Conversation

1 comment

  1. Mike Hammond Reply

    Comments, as always, are welcome.