Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What is the correct syntax for this If statement?

Posted on 2011-03-07
4
Medium Priority
?
451 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
[X]
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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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ā€¦
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastrā€¦

661 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