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 - error handling

Reply
 
Old 12-17-2007   #1 (permalink)
MaxMad


 
 

error handling

Hi again!

How do i handle with errors in PS?

Here are NT Shell examples for what i need:

net user %username% /domain || echo %username% does not exit! >> error.txt

or

net user %username% /domain 1>nul: 2>error.txt

-------

What are the equivalents in Powershell? How can i log my own information
about a command that doesnt execute because of an error like a not existing
object?

I need the PS solutions for '||' '&&' '2>' ...... if %errorlevel%==


I think you will get a lot of more such questions the next days from me ;-)

Thanks and Greets!
MaxMad

My System SpecsSystem Spec
Old 12-17-2007   #2 (permalink)
Brandon Shell [MVP]


 
 

Re: error handling

I have a series on my site about this
http://bsonposh.com/modules/wordpress/?p=51

"MaxMad" <MaxMad@xxxxxx> wrote in message
newsEEAD103-3F6B-4FE5-8ABA-8BB444671BF9@xxxxxx
Quote:

> Hi again!
>
> How do i handle with errors in PS?
>
> Here are NT Shell examples for what i need:
>
> net user %username% /domain || echo %username% does not exit! >> error.txt
>
> or
>
> net user %username% /domain 1>nul: 2>error.txt
>
> -------
>
> What are the equivalents in Powershell? How can i log my own information
> about a command that doesnt execute because of an error like a not
> existing
> object?
>
> I need the PS solutions for '||' '&&' '2>' ...... if %errorlevel%==
>
>
> I think you will get a lot of more such questions the next days from me
> ;-)
>
> Thanks and Greets!
> MaxMad
My System SpecsSystem Spec
Old 12-18-2007   #3 (permalink)
Keith Hill [MVP]


 
 

Re: error handling

"MaxMad" <MaxMad@xxxxxx> wrote in message
newsEEAD103-3F6B-4FE5-8ABA-8BB444671BF9@xxxxxx
Quote:

> Hi again!
>
> How do i handle with errors in PS?
>
> Here are NT Shell examples for what i need:
>
> net user %username% /domain || echo %username% does not exit! >> error.txt
$? indicates whether or not the last command succeeded or failed which in
the case of a console exe means 0 or non-zero for failure.

net user $username /domain
if (!$?) { "$username does not exists" >> error.txt }
Quote:

>
> or
>
> net user %username% /domain 1>nul: 2>error.txt
net use $username /domain 2>error.txt > $null
Quote:

> What are the equivalents in Powershell? How can i log my own information
> about a command that doesnt execute because of an error like a not
> existing
> object?
>
> I need the PS solutions for '||' '&&' '2>' ...... if %errorlevel%==
&& just changes the "if (!$?) { ... }" to "if ($?} { ... }". $LastExitCode
holds the last exit code returned by a call to an EXE.

BTW I do this a lot so I have created a CheckLastExitCode function that
looks like this (and uses another function called Get-CallStack):

function Get-CallStack {
trap { continue }
1..100 | foreach {
$var = Get-Variable -scope $_ MyInvocation
$var.Value.PositionMessage -replace "`n"
}
}

function CheckLastExitCode {
param ([int[]]$SuccessCodes = @(0), [scriptblock]$CleanupScript=$null)

if ($SuccessCodes -notcontains $LastExitCode) {
if ($CleanupScript) {
"Executing cleanup script: $CleanupScript"
&$CleanupScript
}
$OFS = $NL = [System.Environment]::NewLine
throw "EXE RETURNED EXIT CODE ${LastExitCode}${NL}$(Get-CallStack)"
}
}

Some EXEs have multiple success codes or they don't use 0 as a success code
so you can pass in an array of success codes to this function but it does
default to 0 as the one success code.

--
Keith

My System SpecsSystem Spec
Old 12-18-2007   #4 (permalink)
MaxMad


 
 

Re: error handling

thanks :-) Thats what i was looking for!

Greets MaxMad

"Keith Hill [MVP]" wrote:
Quote:

> "MaxMad" <MaxMad@xxxxxx> wrote in message
> newsEEAD103-3F6B-4FE5-8ABA-8BB444671BF9@xxxxxx
Quote:

> > Hi again!
> >
> > How do i handle with errors in PS?
> >
> > Here are NT Shell examples for what i need:
> >
> > net user %username% /domain || echo %username% does not exit! >> error.txt
>
> $? indicates whether or not the last command succeeded or failed which in
> the case of a console exe means 0 or non-zero for failure.
>
> net user $username /domain
> if (!$?) { "$username does not exists" >> error.txt }
>
Quote:

> >
> > or
> >
> > net user %username% /domain 1>nul: 2>error.txt
>
> net use $username /domain 2>error.txt > $null
>
Quote:

> > What are the equivalents in Powershell? How can i log my own information
> > about a command that doesnt execute because of an error like a not
> > existing
> > object?
> >
> > I need the PS solutions for '||' '&&' '2>' ...... if %errorlevel%==
>
> && just changes the "if (!$?) { ... }" to "if ($?} { ... }". $LastExitCode
> holds the last exit code returned by a call to an EXE.
>
> BTW I do this a lot so I have created a CheckLastExitCode function that
> looks like this (and uses another function called Get-CallStack):
>
> function Get-CallStack {
> trap { continue }
> 1..100 | foreach {
> $var = Get-Variable -scope $_ MyInvocation
> $var.Value.PositionMessage -replace "`n"
> }
> }
>
> function CheckLastExitCode {
> param ([int[]]$SuccessCodes = @(0), [scriptblock]$CleanupScript=$null)
>
> if ($SuccessCodes -notcontains $LastExitCode) {
> if ($CleanupScript) {
> "Executing cleanup script: $CleanupScript"
> &$CleanupScript
> }
> $OFS = $NL = [System.Environment]::NewLine
> throw "EXE RETURNED EXIT CODE ${LastExitCode}${NL}$(Get-CallStack)"
> }
> }
>
> Some EXEs have multiple success codes or they don't use 0 as a success code
> so you can pass in an array of success codes to this function but it does
> default to 0 as the one success code.
>
> --
> Keith
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Re: error handling VB Script
WMI Error Handling PowerShell
Re: Error handling SQl database VB Script
Crash Course in Error Handling? PowerShell
Error Handling PowerShell


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