Solved

Check parameters failed on powershell script

Posted on 2012-03-13
10
509 Views
Last Modified: 2012-03-14
Hello,

With this code, I check the parameters but they fail.
If I execute the script with the parameter -A InstallCluster, exit is done :

param
(
      [string]$A = "InstallCluster",
      [string]$I = "MAS",
      [string]$V = "MAS",
      [switch]$Help ,
      [switch]$verbose,
      [switch]$debug
)
if ($A -ne "InstallCluster" -or ($A -ne  "AddNode") -or ($A -ne "Uninstall"))
      {
            write-host -f Yellow "Setting -A parameter to InstallCluster, AddNode, Uninstall
      -I Instance Name -V VirtualServerName
      Example : mas_install_bin_1-0.ps1 -A InstallCluster -I Test -V test" ; exit
            
      }

Thanks

Regards

bibi
0
Comment
Question by:bibi92
[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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 10

Expert Comment

by:Joe Klimis
ID: 37714374
i think you need to change this line to use -eq  instead of -ne

if ($A -ne "InstallCluster" -or ($A -ne  "AddNode") -or ($A -ne "Uninstall"))


Let me know
0
 
LVL 24

Accepted Solution

by:
DBAduck - Ben Miller earned 500 total points
ID: 37714622
The code -or says that if anyone of those are Not Equal to the values then put the Yellow text.  So when you put -ne "InstallCluster" this is false because it does equal it.  But then you say OR $A -ne "AddNode" and this evaluates to True because it does not equal "AddNode" it equals "InstallCluster"  so once it hits a $true, then it will stop and give you the stuff in the { }

So in your case, you need to change it to -eq for each of them and execute the stuff you want to in the { } and then do an else { Write-Host ...... }
0
 

Author Comment

by:bibi92
ID: 37714879
Sorry, but when I execute the script with -A InstallCluster, exit is done and normally it will not exit and execute
if ($A -eq "InstallCluster")
$command = "$SourceFolder\setup.exe  /configurationfile=`"$file`""
write-output $command
So there is a problem in if ($A -ne "InstallCluster" -or ($A -ne  "AddNode") -or ($A -ne "Uninstall"))

Thanks
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 24

Expert Comment

by:DBAduck - Ben Miller
ID: 37715404
With multiple -or conditions, only one of them has to be true to fall into that block of code and in your case, the other 2 evaluate to true so the statement:

$false -or $true -or $true

always evaluates to $true so that Write-Host block of code will execute.

What I was saying is that you need to do something like this:

param
(
      [string]$A = "InstallCluster",
      [string]$I = "MAS",
      [string]$V = "MAS",
      [switch]$Help ,
      [switch]$verbose,
      [switch]$debug
)
if ($A -eq "InstallCluster" -or ($A -eq  "AddNode") -or ($A -eq "Uninstall"))
      {
            # Do something
            if($A -eq "InstallCluster") {
                    Write-Host "Found Install Cluster"
            }
      }
else {
            write-host -f Yellow "Setting -A parameter to InstallCluster, AddNode, Uninstall
      -I Instance Name -V VirtualServerName
      Example : mas_install_bin_1-0.ps1 -A InstallCluster -I Test -V test" ; exit
           
      }
0
 

Author Closing Comment

by:bibi92
ID: 37715522
Ok, I have modified the script with your advices and it works. Thanks
0
 

Author Comment

by:bibi92
ID: 37715877
param
(
      [string]$A = "",
      [string]$I = "",
      [string]$V = "",
      [switch]$Help ,
      [switch]$verbose,
      [switch]$debug
)
if ($A -eq "InstallCluster" -and $I -ne "" -and $V -ne "")
{$command = "$SourceFolder\setup.exe  /configurationfile=`"$FileNameAddNode`""
      write-output $command
      cmd /c $command
}

....
0
 
LVL 24

Expert Comment

by:DBAduck - Ben Miller
ID: 37715883
that would work as well.
0
 
LVL 8

Expert Comment

by:Brent Challis
ID: 37718049
A better approach, in my opinion, is to use an attribute on the input parameter to define what values are acceptable, for example:

function test
{
    param
    (
        [ValidateSet("InstallCluster","AddNode","Uninstall")]
        [string]$A = "InstallCluster"
     )
     Write-Host $A
}

test
test InstallCluster
test Uninstall
test BadValue

That way an error message is thrown when the value is not acceptable.
0
 

Author Comment

by:bibi92
ID: 37719227
For bchallis, exactly, how can give you points?

Thanks

Regards

bibi
0
 
LVL 8

Expert Comment

by:Brent Challis
ID: 37723135
You have the option of re-opening the question.  To do so, the you will need to request attention on the question and ask the Moderators to un-select the accepted solution, which would allow the you to select a new solution or solutions.

Cheers,
Brent
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

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…
A brief introduction to what I consider to be the best editor for PowerShell.
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…

696 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