Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Windows 7 Forum Vista Tutorials Tags

Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Vista Tutorial - -ne operator

Reply
 
LinkBack Thread Tools Display Modes
Old 11-23-2008   #1 (permalink)
Simon Walsh
Guest


 

-ne operator

Hi

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.

What I have so far

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

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.

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

Anyone have any tips?

Thanks
Simon


My System SpecsSystem Spec
Old 11-23-2008   #2 (permalink)
David Muegge
Guest


 

Re: -ne operator

Simon Walsh wrote:
Quote:

> Hi
>
> 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.
>
> What I have so far
>
> $Input = $args[0]
> If ($Input -ne "1")
> {
> Write-Host "Error"
> Break
> }
>
> 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.
>
> I have tried:
> If ($Input -ne "1" -ne "2" -ne "3" -ne "4")
> {
> Write-Host "Error"
> Break
> }
> but that doesn't work..
>
> Anyone have any tips?
>
> 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
My System SpecsSystem Spec
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
Old 11-23-2008   #4 (permalink)
Simon Walsh
Guest


 

Re: -ne operator

Thanks for the answers guys
I will try them both out

/Simon
"tojo2000" <tojo2000@xxxxxx> wrote in message
news:be02a293-d1ab-4c57-aeca-33e8b9558d1f@xxxxxx
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
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Powershell Operator AdityaKir PowerShell 3 08-18-2008 04:55 PM
-f operator Tibor Soos PowerShell 3 05-28-2008 07:36 AM
Operator Error dgmacmi Vista performance & maintenance 0 04-07-2008 12:10 PM
What does the $() operator do? Kevin Buchan PowerShell 3 02-08-2008 02:05 PM
-replace operator Bob Weiner PowerShell 1 06-30-2006 01:46 PM


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53