Hello,
I have a script to check server OS and SP based on build number. I was wondering if there was a better way to write the if statement instead of a if statement for each build number.
#-- The purpose of this script is to check server OS and SP level based on build # list from Microsoft#-- Load modulesGet-Module -ListAvailable | Import-Module#-- Clear Screencls#-- Declare Variables#-- Start Script$Server=Read-Host " Enter Server actual name"#-- Get Server OS build # and compare to list from Microsoft$ComVer=Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Server$ComBuild=$ComVer.VersionIf ($ComBuild -eq "5.2.3790"){Write-Host $Server "= Win 2003"}If ($ComBuild -eq "6.0.6001"){Write-Host $Server "= Win 2008 Std"}If ($ComBuild -eq "6.1.7600"){Write-Host $Server "= Win 2008 R2 Std no sp"}If ($ComBuild -eq "6.1.7601"){Write-Host $Server "= Win 2008 R2 Std SP 1"}
It works just fine just trying to find better ways to write scripts any thoughts are more then welcome.
Dana
Instead of the multiple if statements try using a switch statement - something like this
switch ($ComBuild) { "5.2.3790" {Write-Host $Server "= Win 2003"}"6.0.6001" {Write-Host $Server "= Win 2008 Std"}"6.1.7600" {Write-Host $Server "= Win 2008 R2 Std no sp"}"6.1.7601" {Write-Host $Server "= Win 2008 R2 Std SP 1"}default {Write-Host "Unknown Version Number"}}
Well thats pretty slick. I will have to research that switch command and see what else can be done with it.
Thank you.