Just a question

rated by 0 users
This post has 2 Replies | 1 Follower

Top 200 Contributor
Posts 17
danarstevenson Posted: 04-11-2012 10:51 AM

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 modules
Get-Module -ListAvailable | Import-Module

#-- Clear Screen
cls

#-- 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.Version
If ($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

Top 25 Contributor
Posts 296
Microsoft MVP
Top Contributor

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"}
}

Top 200 Contributor
Posts 17

Well thats pretty slick.  I will have to research that switch command and see what else can be done with it.

 

Thank you.

Page 1 of 1 (3 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.