System Discovery Part 4

Tags:
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 { $loc = DATA { ## codeset info ## country codes Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer | |
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