Windows Vista Forums
Vista Forums Home Join Vista Forums 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 Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Case/Select functionality

Reply
 
Old 02-25-2007   #1 (permalink)
LoneWolf


 
 

Case/Select functionality

I'm writing a script to check if a VPN connection has failed. I want to
display the return code from a failed ping. Is there any way to do a Case,
Select? Right now I just have a bunch of If statements:

if ($a -eq 11001)
{ $errorcode = "Buffer Too Small "}
if ($a -eq 11002)
{ $errorcode = "Destination Net Unreachable "}
if ($a -eq 11003)
....

Any ideas?


My System SpecsSystem Spec
Old 02-25-2007   #2 (permalink)
Keith Hill


 
 

Re: Case/Select functionality

"LoneWolf" <LoneWolf@discussions.microsoft.com> wrote in message
news:97570D07-EE48-4C57-9A30-378CD3A29EE9@microsoft.com...
> I'm writing a script to check if a VPN connection has failed. I want to
> display the return code from a failed ping. Is there any way to do a
> Case,
> Select? Right now I just have a bunch of If statements:
>
> if ($a -eq 11001)
> { $errorcode = "Buffer Too Small "}
> if ($a -eq 11002)
> { $errorcode = "Destination Net Unreachable "}
> if ($a -eq 11003)
> ...


switch $a {
11001 { $errorcode = 'Buffer too small' }
11002 { $errorcode = 'Destination net unreachable; }
...
default { $errorcode = 'Unknown error code' }
}

--
Keith

My System SpecsSystem Spec
Old 02-25-2007   #3 (permalink)
LoneWolf


 
 

Re: Case/Select functionality

I knew that would be simple. Thanks for helping out a newbie on his second
day using Pshell.

"Keith Hill" wrote:

> "LoneWolf" <LoneWolf@discussions.microsoft.com> wrote in message
> news:97570D07-EE48-4C57-9A30-378CD3A29EE9@microsoft.com...
> > I'm writing a script to check if a VPN connection has failed. I want to
> > display the return code from a failed ping. Is there any way to do a
> > Case,
> > Select? Right now I just have a bunch of If statements:
> >
> > if ($a -eq 11001)
> > { $errorcode = "Buffer Too Small "}
> > if ($a -eq 11002)
> > { $errorcode = "Destination Net Unreachable "}
> > if ($a -eq 11003)
> > ...

>
> switch $a {
> 11001 { $errorcode = 'Buffer too small' }
> 11002 { $errorcode = 'Destination net unreachable; }
> ...
> default { $errorcode = 'Unknown error code' }
> }
>
> --
> Keith
>

My System SpecsSystem Spec
Old 02-25-2007   #4 (permalink)
Keith Hill


 
 

Re: Case/Select functionality

"LoneWolf" <LoneWolf@discussions.microsoft.com> wrote in message
news:F971A17A-7FCA-46EA-95AD-2703CFE8C9EC@microsoft.com...
>I knew that would be simple. Thanks for helping out a newbie on his second
> day using Pshell.


No problem. Hang in there. The learning curve can be a bit steep but you
have a couple of things going for you. First this forum is pretty
responsive even to newbie questions and second the payoff is well worth the
potential up front frustration.

--
Keith

My System SpecsSystem Spec
Old 02-25-2007   #5 (permalink)
LoneWolf


 
 

Re: Case/Select functionality

I tried what you said and I keep getting an error :

Missing '{' in switch statement.
At C:\Documents and Settings\jeff\test2.ps1:12 char:9
+ switch $ <<<< b {

What am I missing? Here is a snippet, (the names have been changed):

function init ()
{
$GLOBALingtarget = "www.gohome.com"
$GLOBAL:LocalRouter = "172.16.1.1"
$GLOBAL:externaltarget = "www.google.com"
}

function pingit
{
$global:targetresponse = Get-WmiObject -Class Win32_PingStatus -Filter
"Address= '$globalingtarget'"
$a = $global:targetresponse.statuscode
switch $a {
11001 { $global:targeterror = 'Buffer too small' }
11002 { $global:targeterror = 'Destination net unreachable' }
11003 { $global:targeterror = "Destination Host Unreachable "}
11004 { $global:targeterror = "Destination Protocol Unreachable"}
11005 { $global:targeterror = "Destination Port Unreachable "}
11006 { $global:targeterror = "No Resources "}
11007 { $global:targeterror = "Bad Option "}
11008 { $global:targeterror = "Hardware Error "}
11009 { $global:targeterror = "Packet Too Big "}
11010 { $global:targeterror = "Request Timed Out" }
11011 { $global:targeterror = "Bad Request "}
11012 { $global:targeterror = "Bad Route "}
11013 { $global:targeterror = "TimeToLive Expired Transit "}
11014 { $global:targeterror = "TimeToLive Expired Reassembly" }
11015 { $global:targeterror = "Parameter Problem "}
11016 { $global:targeterror = "Source Quench "}
11017 { $global:targeterror = "Option Too Big "}
11018 { $global:targeterror = "Bad Destination "}
11032 { $global:targeterror = "Negotiating IPSEC" }
11050 { $global:targeterror = "General Failure "}
default { $global:targeterror = 'Unknown error code' }
}

}


"Keith Hill" wrote:

> "LoneWolf" <LoneWolf@discussions.microsoft.com> wrote in message
> news:F971A17A-7FCA-46EA-95AD-2703CFE8C9EC@microsoft.com...
> >I knew that would be simple. Thanks for helping out a newbie on his second
> > day using Pshell.

>
> No problem. Hang in there. The learning curve can be a bit steep but you
> have a couple of things going for you. First this forum is pretty
> responsive even to newbie questions and second the payoff is well worth the
> potential up front frustration.
>
> --
> Keith
>

My System SpecsSystem Spec
Old 02-25-2007   #6 (permalink)
LoneWolf


 
 

Re: Case/Select functionality

NEVER MIND...

switch ($a)

Forgot the parens...

"LoneWolf" wrote:

> I tried what you said and I keep getting an error :
>
> Missing '{' in switch statement.
> At C:\Documents and Settings\jeff\test2.ps1:12 char:9
> + switch $ <<<< b {
>
> What am I missing? Here is a snippet, (the names have been changed):
>
> function init ()
> {
> $GLOBALingtarget = "www.gohome.com"
> $GLOBAL:LocalRouter = "172.16.1.1"
> $GLOBAL:externaltarget = "www.google.com"
> }
>
> function pingit
> {
> $global:targetresponse = Get-WmiObject -Class Win32_PingStatus -Filter
> "Address= '$globalingtarget'"
> $a = $global:targetresponse.statuscode
> switch $a {
> 11001 { $global:targeterror = 'Buffer too small' }
> 11002 { $global:targeterror = 'Destination net unreachable' }
> 11003 { $global:targeterror = "Destination Host Unreachable "}
> 11004 { $global:targeterror = "Destination Protocol Unreachable"}
> 11005 { $global:targeterror = "Destination Port Unreachable "}
> 11006 { $global:targeterror = "No Resources "}
> 11007 { $global:targeterror = "Bad Option "}
> 11008 { $global:targeterror = "Hardware Error "}
> 11009 { $global:targeterror = "Packet Too Big "}
> 11010 { $global:targeterror = "Request Timed Out" }
> 11011 { $global:targeterror = "Bad Request "}
> 11012 { $global:targeterror = "Bad Route "}
> 11013 { $global:targeterror = "TimeToLive Expired Transit "}
> 11014 { $global:targeterror = "TimeToLive Expired Reassembly" }
> 11015 { $global:targeterror = "Parameter Problem "}
> 11016 { $global:targeterror = "Source Quench "}
> 11017 { $global:targeterror = "Option Too Big "}
> 11018 { $global:targeterror = "Bad Destination "}
> 11032 { $global:targeterror = "Negotiating IPSEC" }
> 11050 { $global:targeterror = "General Failure "}
> default { $global:targeterror = 'Unknown error code' }
> }
>
> }
>
>
> "Keith Hill" wrote:
>
> > "LoneWolf" <LoneWolf@discussions.microsoft.com> wrote in message
> > news:F971A17A-7FCA-46EA-95AD-2703CFE8C9EC@microsoft.com...
> > >I knew that would be simple. Thanks for helping out a newbie on his second
> > > day using Pshell.

> >
> > No problem. Hang in there. The learning curve can be a bit steep but you
> > have a couple of things going for you. First this forum is pretty
> > responsive even to newbie questions and second the payoff is well worth the
> > potential up front frustration.
> >
> > --
> > Keith
> >

My System SpecsSystem Spec
Old 02-25-2007   #7 (permalink)
Keith Hill


 
 

Re: Case/Select functionality

"LoneWolf" <LoneWolf@discussions.microsoft.com> wrote in message
news:CF471119-7B9D-4A98-B051-B34B9D59952A@microsoft.com...
> NEVER MIND...
>


Oops. Sorry about that.

--
Keith

My System SpecsSystem Spec
Old 02-25-2007   #8 (permalink)
LoneWolf


 
 

Re: Case/Select functionality

No problem. Hard-won knowlege is the best knowlege.

"Keith Hill" wrote:

> "LoneWolf" <LoneWolf@discussions.microsoft.com> wrote in message
> news:CF471119-7B9D-4A98-B051-B34B9D59952A@microsoft.com...
> > NEVER MIND...
> >

>
> Oops. Sorry about that.
>
> --
> Keith
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Script that uses the DatePart & Case select VB Script
select case does not work ... why? VB Script
Logon script - function array and select case not working VB Script
Re: Select case question VB Script
Select Files - Disable or Enable Full Row Select Tutorials


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