Solved

Check parameters failed on powershell script

Posted on 2012-03-13
10
504 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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 10

Expert Comment

by:JoeKlimis
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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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:bchallis
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:bchallis
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now