PowerShell for Windows Admins

Dec 15 2009   4:04PM GMT

System Discovery Part 2



Posted by: Richard Siddaway
Operating System

This is our script to examine the Operating System. I’ve made it a bit easier to read by using the line continuation character ` (that’s a back tick not an apostrophe) Get-WmiObject -Class Win32_OperatingSystem | Format-List CSName, Caption, `
ServicePackMajorVersion, ServicePackMinorVersion, BuildNumber, Version, `
OSArchitecture, SystemDevice, SystemDrive, WindowsDirectory, SystemDirectory, `
OSLanguage, OSProductSuite, OSType, CodeSet, CountryCode, EncryptionLevel, `
ForegroundApplicationBoost, DataExecutionPrevention_32BitApplications, `
DataExecutionPrevention_Available, DataExecutionPrevention_Drivers, `
DataExecutionPrevention_SupportPolicy, InstallDate, LastBootUpTime, LocalDateTime, `
Locale, FreePhysicalMemory, SizeStoredInPagingFiles, FreeSpaceInPagingFiles, `
TotalVisibleMemorySize, TotalVirtualMemorySize, FreeVirtualMemory

One problem with this is that the dates are reported like this
InstallDate           : 20090807113852.000000+060
LastBootUpTime  : 20091215080951.359600+000
LocalDateTime     : 20091215213858.961000+000

Which isn’t very readable.
If we look at the object produced by Win32_OperatingSystem using
Get-WmiObject -Class Win32_OperatingSystem | Get-Member
We can see that there are a couple of methods we can use to manipulate dates.
ConvertFromDateTime
ConvertToDateTime

We want to convert to a date and time that we can read. In order to do this we need to create a calculated field.

@{Name=”Installation Date”; Expression={$_.ConvertToDateTime($_.InstallDate)}}

We create a hash table (associative array) where the first element defines the name we want to give the field and the second element defines an expression to calculate the value.

$_ represents the object coming down the pipeline so we use the ConvertToDateTime method of that object and feed it the InstallDate property.
Likewise the other two fields can be calculated

@{Name=”Last Bootup time”; Expression={$_.ConvertToDateTime($_.LastBootUpTime)}}, `
@{Name=”Local Date Time”; Expression={$_.ConvertToDateTime($_.LocalDateTime)}},

The three dates then become
Installation Date : 07/08/2009 11:38:52
Last Bootup time : 15/12/2009 08:09:51
Local Date Time  : 15/12/2009 21:52:51

I’m in the UK so my dates are DD/MM/YYYY format.

Our whole script becomes
Get-WmiObject -Class Win32_OperatingSystem | Format-List CSName, Caption, `
ServicePackMajorVersion, ServicePackMinorVersion, BuildNumber, Version, `
OSArchitecture, SystemDevice, SystemDrive, WindowsDirectory, SystemDirectory, `
OSLanguage, OSProductSuite, OSType, CodeSet, CountryCode, EncryptionLevel, `
ForegroundApplicationBoost, DataExecutionPrevention_32BitApplications, `
DataExecutionPrevention_Available, DataExecutionPrevention_Drivers, `
DataExecutionPrevention_SupportPolicy, `
@{Name=”Installation Date”; Expression={$_.ConvertToDateTime($_.InstallDate)}}, `
@{Name=”Last Bootup time”; Expression={$_.ConvertToDateTime($_.LastBootUpTime)}}, `
@{Name=”Local Date Time”; Expression={$_.ConvertToDateTime($_.LocalDateTime)}}, `
Locale, FreePhysicalMemory, SizeStoredInPagingFiles, FreeSpaceInPagingFiles, `
TotalVisibleMemorySize, TotalVirtualMemorySize, FreeVirtualMemory

We will look at unravelling some of the numeric values next

Comment on this Post

Leave a comment: