PowerShell for Windows Admins

Dec 19 2009   4:45AM GMT

System Discovery Part 4

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

 

This is out last look at Win32_OperatingSystem.  I’ve taken the script we had last time and added a few more calculated fields to determine language, country, locale and codeset information.  I’ve left the URLs where I found the information as comments. If you are not UK based you will want to add extra values from the tables I’ve referenced.

The other change I’ve made at the beginning of the script is to allow it to accept a computer name as a parameter. Up to now we have just been looking at the local machine. We can now extend this to cover remote machines because we’ve added the –computername parameter to the Get-WmiObject call.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
param ([string]$computer = "."  ) 
$os = DATA {
ConvertFrom-StringData -StringData @’
14 = MSDOS
15 = WIN3X
16 = WIN95
17 = WIN98
18 = WINNT
19 = WINCE
‘@

}

##
## for language and locale settings see
## http://msdn.microsoft.com/en-us/goglobal/bb964664.aspx
##

$lang = DATA {
ConvertFrom-StringData -StringData @’
1033 = English US
2057 = English UK
‘@

}

$loc = DATA {
ConvertFrom-StringData -StringData @’
0409 = English US
0809 = English UK
‘@

}

## codeset info
## http://msdn.microsoft.com/en-us/goglobal/bb964654.aspx
$code = DATA {
ConvertFrom-StringData -StringData @’
1252 = Latin I
‘@

}

## country codes
##http://msdn.microsoft.com/en-us/library/dd387951(VS.85).aspx
$country = DATA {
ConvertFrom-StringData -StringData @’
1 = USA
44 = United Kingdom
‘@

}

Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer | 
Format-List CSName, Caption, ServicePackMajorVersion, 
ServicePackMinorVersion, BuildNumber, Version, OSArchitecture,
SystemDevice, SystemDrive, WindowsDirectory, SystemDirectory, 
@{Name="OS Language"; Expression={$lang["$($_.OSLanguage)"]}}, 
OSProductSuite, 
@{Name="OS Type"; Expression={$os["$($_.OSType)"]}}, 
@{Name="Code Set"; Expression={$code["$($_.CodeSet)"]}}, 
@{Name="Country Code"; Expression={$country["$($_.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)}}, 
@{Name="Locale "; Expression={$loc["$($_.Locale)"]}},
@{Name="Free Physical Memory (GB)"; Expression={"{0:F3}" -f $($_.FreePhysicalMemory/1GB*1kb)}},
@{Name="Size Stored In Paging Files (GB)"; Expression={"{0:F3}" -f $($_.SizeStoredInPagingFiles *1kb /1GB)}},
@{Name="Free Space In Paging Files (GB)"; Expression={"{0:F3}" -f $($_.FreeSpaceInPagingFiles *1kb /1GB)}},
@{Name="Total Visible Memory Size (GB)"; Expression={"{0:F3}" -f $($_.TotalVisibleMemorySize *1kb /1GB)}}, 
@{Name="Total Virtual Memory Size (GB)"; Expression={"{0:F3}" -f $($_.TotalVirtualMemorySize *1kb /1GB)}}, 
@{Name="Free Virtual Memory (GB)"; Expression={"{0:F3}" -f $($_.FreeVirtualMemory*1kb /1GB)}}

The final change is at the end of the script where I’ve added calculated fields to convert the memory fields to GB from the KB they are reported in. These fields all take the same form

@{Name="Free Virtual Memory (GB)"; Expression={"{0:F3}" -f $($_.FreeVirtualMemory*1kb /1GB)}}

Create a new name for the field and then multiply the field by 1kb to turn it to bytes and the divide by 1GB to get gigabytes.  PowerShell recognises kb, mb, gb, tb and pb as kilobytes, megabytes etc (tb and pb are only in PowerShell 2). They are case insensitive so kb and KB both work.  I then take the calculated result and format it into a string using the –f operator.  The formatting restricts the display to 3 decimal places.

I’ve shown a number of steps in the evolution of this script as it reflects they way these things are often developed. Start with the raw display from the WMI object and then refine to give more meaningful information. In future scripts I’ll just jump straight to the end point, usually, as I used this example to show case the techniques we can use.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: