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: " $strComputerif ($OS.Version -eq "5.1.2600") {write-host "OS Version: Windows XP".\PromptReset.ps1}else{write-host "OS Version: Windows 2003".\PromptReset.ps1}}#Unknown OSelse {write-host "The OS for: $strComputer is unknown"}
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 OSelse {write-host "The OS for: $strComputer is unknown"}
Beyond that, a switch may work better. I usually use switches once I get past 2 conditions, but, 3 is not far past 2.
RussPitcher
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"}
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.
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.