Thread: -ne operator
View Single Post
Old 11-23-2008   #3 (permalink)
tojo2000
Guest


 
 

Re: -ne operator

On Nov 23, 7:49*am, David Muegge <da...@xxxxxx> wrote:
Quote:

> Simon Walsh wrote:
Quote:

> > Hi
>
Quote:

> > I have a script that accepts an argument from the user. There are four
> > possible values that I wish the user to pass to the script. I wil then take
> > different actions based on the argument passed.
> > If I do not get one of those values I want to error out. I am not really
> > sure how to use the the operators.
>
Quote:

> > What I have so far
>
Quote:

> > $Input = $args[0]
> > If ($Input -ne "1")
> > {
> > Write-Host "Error"
> > Break
> > }
>
Quote:

> > So here I will break out if I do not get 1 as an input. I also want to error
> > out if I don't get 2, 3 or 4.
>
Quote:

> > I have tried:
> > If ($Input -ne "1" -ne "2" -ne "3" -ne "4")
> > {
> > Write-Host "Error"
> > Break
> > }
> > *but that doesn't work..
>
Quote:

> > Anyone have any tips?
>
Quote:

> > Thanks
> > Simon
>
> Simon,
>
> I would look at the switch statement
>
> Something like this
>
> switch ($input) {
>
> * * * * 1 {"It’s one"}
>
> * * * * 2 {"It’s two"}
>
> * * * * 3 {"It’s three"}
>
> * * * * 4 {"It’s four"}
>
> * * * * default {"Wrong Answer"}
>
> }
>
> Place whatever code you want to execute for each condition in the
> corresponding script block.
>
> Regards,
>
> David Muegge
Another option would be to create an array of valid values like this:

$valid_values = 'one', 'two', 'three', 'radio'

if ($valid_values -notcontains $user_input) {
Write-Error 'Invalid Entry'
}
My System SpecsSystem Spec