Solved

What is the correct syntax for this If statement?

Posted on 2011-03-07
4
443 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

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 …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

772 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