Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

What is the correct syntax for this If statement?

Posted on 2011-03-07
4
Medium Priority
?
454 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

916 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