![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 news EEAD103-3F6B-4FE5-8ABA-8BB444671BF9@xxxxxxQuote: > 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 Specs![]() |
| | #3 (permalink) |
| | Re: error handling "MaxMad" <MaxMad@xxxxxx> wrote in message news EEAD103-3F6B-4FE5-8ABA-8BB444671BF9@xxxxxxQuote: > 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 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 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%== 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 Specs![]() |
| | #4 (permalink) |
| | Re: error handling thanks :-) Thats what i was looking for! Greets MaxMad "Keith Hill [MVP]" wrote: Quote: > "MaxMad" <MaxMad@xxxxxx> wrote in message > news EEAD103-3F6B-4FE5-8ABA-8BB444671BF9@xxxxxxQuote: > > 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 Specs![]() |
![]() |
| 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 | |||