Solved

What is the correct syntax for this If statement?

Posted on 2011-03-07
4
439 Views
Last Modified: 2012-05-11
Experts,

       Below I have code that will install another script based on the OS. However the last else statement is printing every time the script is run for either OS. How do I get the program to print Computer is unknown only if XP or Win2k3 is not found?

Thanks
Missymadi
if(get-PSSnapin Quest.ActiveRoles.ADManagement) {write-host Quest Tools are installed}
 else {Write-Host "No Quest Tools are installed"}
 Add-PSSnapin Quest.ActiveRoles.ADManagement
 Write-Host "Quest Active Roles for Active Directory is now installed on your system." 

$strComputer = Read-Host "Enter Computer Name"
$OS = gwmi win32_OperatingSystem -Comp $strComputer

# if statement to run code for Windows XP and Windows 2003 Server.
write-host "Computer Name: " $strComputer
if ($OS.Version -eq "5.1.2600") {
write-host "OS Version: Windows XP"
.\PromptReset.ps1
}
else
{
write-host "OS Version: Windows 2003"
.\PromptReset.ps1
}
}

#Unknown OS

else {write-host "The OS for: $strComputer is unknown"}

Open in new window

0
Comment
Question by:missymadi
  • 2
4 Comments
 
LVL 10

Expert Comment

by:wls3
Comment Utility
Perhaps elseif would work better on your second condition
if (($OS.Version -eq "5.1.2600") -OR ()) {
write-host "OS Version: Windows XP"
.\PromptReset.ps1
}
[b]elseif[/b]
{
write-host "OS Version: Windows 2003"
.\PromptReset.ps1
}
}

#Unknown OS

else {write-host "The OS for: $strComputer is unknown"}

Open in new window

Beyond that, a switch may work better.  I usually use switches once I get past 2 conditions, but, 3 is not far past 2.
0
 
LVL 3

Expert Comment

by:RussPitcher
Comment Utility
I think you've got half a script there.  If I re-organise your code with my indenting conventions I get this:

	if(get-PSSnapin Quest.ActiveRoles.ADManagement) {
		write-host Quest Tools are installed
	} else {
		Write-Host "No Quest Tools are installed"
	}
	
	Add-PSSnapin Quest.ActiveRoles.ADManagement
	Write-Host "Quest Active Roles for Active Directory is now installed on your system." 
	
	$strComputer = Read-Host "Enter Computer Name"
	$OS = gwmi win32_OperatingSystem -Comp $strComputer
	
	# if statement to run code for Windows XP and Windows 2003 Server.
	write-host "Computer Name: " $strComputer
	if ($OS.Version -eq "5.1.2600") {
		write-host "OS Version: Windows XP"
		.\PromptReset.ps1
	} else {
		write-host "OS Version: Windows 2003"
		.\PromptReset.ps1
	}
} else {
	write-host "The OS for: $strComputer is unknown"
}

Open in new window


As you can see, you've got the last half of an if..else block there - The 'if ([condition]) {' part is missing.  I'm guessing that you've errors supressed so PS skips over the stuff it can't make sense of, because the initial part is 'missing' and merrily executes the line it can understand.

0
 

Author Comment

by:missymadi
Comment Utility
RussPitcher

I got an unexpected token on line 22.
0
 
LVL 3

Accepted Solution

by:
RussPitcher earned 500 total points
Comment Utility
That's right.  Lines 22-24 (in my example, 20-24 in yours) are part of an if...else block.  The problem is that the first part is missing.  It should read something like the below example where I've added an extra line at the top.

if ($OS -eq "2008") {
	if(get-PSSnapin Quest.ActiveRoles.ADManagement) {
		write-host Quest Tools are installed
	} else {
		Write-Host "No Quest Tools are installed"
	}
	
	Add-PSSnapin Quest.ActiveRoles.ADManagement
	Write-Host "Quest Active Roles for Active Directory is now installed on your system." 
	
	$strComputer = Read-Host "Enter Computer Name"
	$OS = gwmi win32_OperatingSystem -Comp $strComputer
	
	# if statement to run code for Windows XP and Windows 2003 Server.
	write-host "Computer Name: " $strComputer
	if ($OS.Version -eq "5.1.2600") {
		write-host "OS Version: Windows XP"
		.\PromptReset.ps1
	} else {
		write-host "OS Version: Windows 2003"
		.\PromptReset.ps1
	}
} else {
	write-host "The OS for: $strComputer is unknown"
} 

Open in new window


or, more likely now that Iook at it again, you've got an extra } near the bottom and you need to change the first 'else' to an 'elseif' like this example:

if(get-PSSnapin Quest.ActiveRoles.ADManagement) {
	write-host Quest Tools are installed
} else {
	Write-Host "No Quest Tools are installed"
}

Add-PSSnapin Quest.ActiveRoles.ADManagement
Write-Host "Quest Active Roles for Active Directory is now installed on your system." 

$strComputer = Read-Host "Enter Computer Name"
$OS = gwmi win32_OperatingSystem -Comp $strComputer

# if statement to run code for Windows XP and Windows 2003 Server.
write-host "Computer Name: " $strComputer
if ($OS.Version -eq "5.1.2600") {
	write-host "OS Version: Windows XP"
	.\PromptReset.ps1
} elseif ($OS.Version -eq "5.2.3790") {
	write-host "OS Version: Windows 2003"
	.\PromptReset.ps1
} else {
	write-host "The OS for: $strComputer is unknown"
}
 

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now