Question:
I am querying remote servers for their operating system. I know that I can return the Version, but I want to replace these values with the friendly name. The code I have so far is:
1 2 3 4 5 6 7 8 9 10 |
$Computer = (gc c:\servers.txt) $BuildVersion = Get-WmiObject -Class Win32_OperatingSystem -Property Version, CSName -ComputerName $Computer -ErrorAction SilentlyContinue $Build=$BuildVersion.version If ({$BuildVersion.Version -match "5.2.3790"}) {$Build="2003"} Elseif ({$BuildVersion.Version -match "6.1.7601"}) {$Build="2008"} Elseif ({$BuildVersion.Version -like "6.3.9600"}) {$Build="2012"} |
But this doesn’t seem to work and only returns “2003” regardless. Please help, I’m fairly new to PS and coding.
thanks
Answer:
You can use this:
1 2 |
Get-WmiObject -Class Win32_OperatingSystem | Select-Object -ExpandProperty Caption |
Additionally you can see everything this WMI object holds like this:
1 2 |
Get-WmiObject -Class Win32_OperatingSystem | fl * |
Edit: if you want to remove some text from the string, you can use
-replace
:
1 2 3 |
(Get-WmiObject -Class Win32_OperatingSystem | Select-Object -ExpandProperty Caption) -replace "Microsoft Windows Server ","" |