?
Solved

Check parameters failed on powershell script

Posted on 2012-03-13
10
Medium Priority
?
513 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 11

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 25

Accepted Solution

by:
DBAduck - Ben Miller earned 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 25

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 25

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
Screencast - Getting to Know the Pipeline

839 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