Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

What is the correct syntax for this If statement?

Posted on 2011-03-07
4
444 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
ID: 35058459
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
ID: 35058525
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
ID: 35058854
RussPitcher

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

Accepted Solution

by:
RussPitcher earned 500 total points
ID: 35059010
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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