Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Check parameters failed on powershell script

Posted on 2012-03-13
10
Medium Priority
?
512 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 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
Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

636 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