Solved

Check parameters failed on powershell script

Posted on 2012-03-13
10
506 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

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
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 …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

815 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

12 Experts available now in Live!

Get 1:1 Help Now