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 - native command messages/errors

Reply
 
Old 11-28-2007   #1 (permalink)
Joey


 
 

native command messages/errors

How do I get the verbose messages/errors displayed by native command calls
back into a variable inside of powershell or at least to show up in the
output? At the moment the closest thing I can find is the $lastexitcode
variable which is good for determining if an error occurred but doesn't help
in ascertaining the exact error message of a native command. The $error
object contains nothing when a native command fails. I want to convert my
processes over to powershell but without the ability to display exact error
messages from native commands the benefits will not outweigh the hassles of
trying to debug the scripts because the errors are not shown!

Is there a way to “pipe” the native command call to a text file and then
read that back into powershell after the command finishes? I tried to do this
but the DOS pipe “>” didn’t work for whatever reason.

E.g.
& "$TFExecutablePath\.\tf.exe workspace /delete $Name
/s:$TeamFoundationServerUrl /noprompt > c:\test.txt"


My System SpecsSystem Spec
Old 11-28-2007   #2 (permalink)
Jon


 
 

Re: native command messages/errors


"Joey" <Joey@xxxxxx> wrote in message
news:0D0A70B5-E0CC-4EE7-B7C4-01439B978F16@xxxxxx
Quote:

> How do I get the verbose messages/errors displayed by native command calls
> back into a variable inside of powershell or at least to show up in the
> output? At the moment the closest thing I can find is the $lastexitcode
> variable which is good for determining if an error occurred but doesn't
> help
> in ascertaining the exact error message of a native command. The $error
> object contains nothing when a native command fails. I want to convert my
> processes over to powershell but without the ability to display exact
> error
> messages from native commands the benefits will not outweigh the hassles
> of
> trying to debug the scripts because the errors are not shown!
>
> Is there a way to “pipe” the native command call to a text file and then
> read that back into powershell after the command finishes? I tried to do
> this
> but the DOS pipe “>” didn’t work for whatever reason.
>
> E.g.
> & "$TFExecutablePath\.\tf.exe workspace /delete $Name
> /s:$TeamFoundationServerUrl /noprompt > c:\test.txt"
>



You can use 2> to pipe error messages to a file, and the Get-Content cmdlet
to read them back again eg

xcopy nosuchfileA fileB 2>errors.txt
$anyerrors = Get-Content errors.txt
$anyerrors

--
Jon


My System SpecsSystem Spec
Old 11-28-2007   #3 (permalink)
Marco Shaw [MVP]


 
 

Re: native command messages/errors

Joey wrote:
Quote:

> How do I get the verbose messages/errors displayed by native command calls
> back into a variable inside of powershell or at least to show up in the
> output? At the moment the closest thing I can find is the $lastexitcode
> variable which is good for determining if an error occurred but doesn't help
> in ascertaining the exact error message of a native command. The $error
> object contains nothing when a native command fails. I want to convert my
> processes over to powershell but without the ability to display exact error
> messages from native commands the benefits will not outweigh the hassles of
> trying to debug the scripts because the errors are not shown!

This should help:
http://blogs.msdn.com/powershell/arc...rvariable.aspx


--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Old 11-28-2007   #4 (permalink)
Marco Shaw [MVP]


 
 

Re: native command messages/errors

Quote:

> Is there a way to “pipe” the native command call to a text file and then
> read that back into powershell after the command finishes? I tried to do this
> but the DOS pipe “>” didn’t work for whatever reason.
>
> E.g.
> & "$TFExecutablePath\.\tf.exe workspace /delete $Name
> /s:$TeamFoundationServerUrl /noprompt > c:\test.txt"

Try moving "> c:\test.txt" outside of the quotes.

Marco


--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Old 11-28-2007   #5 (permalink)
Brandon Shell [MVP]


 
 

Re: native command messages/errors

As a note here.. I have a blog series on Error handling.

http://bsonposh.com/modules/wordpress/?p=51

$erroractionpreference/$error may not have effect on "native" commands.


Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject

M> Joey wrote:
M>
Quote:
Quote:

>> How do I get the verbose messages/errors displayed by native command
>> calls back into a variable inside of powershell or at least to show
>> up in the output? At the moment the closest thing I can find is the
>> $lastexitcode variable which is good for determining if an error
>> occurred but doesn't help in ascertaining the exact error message of
>> a native command. The $error object contains nothing when a native
>> command fails. I want to convert my processes over to powershell but
>> without the ability to display exact error messages from native
>> commands the benefits will not outweigh the hassles of trying to
>> debug the scripts because the errors are not shown!
>>
M> This should help:
M> http://blogs.msdn.com/powershell/arc...oraction-and-e
M> rrorvariable.aspx
M> PowerGadgets MVP
M> http://www.powergadgets.com/mvp
M> Blog:
M> http://marcoshaw.blogspot.com


My System SpecsSystem Spec
Old 11-28-2007   #6 (permalink)
Keith Hill [MVP]


 
 

Re: native command messages/errors

"Marco Shaw [MVP]" <marco.shaw@_NO_SPAM_gmail.com> wrote in message
news:#5wKAybMIHA.4476@xxxxxx
Quote:

>
Quote:

>> Is there a way to “pipe” the native command call to a text file and then
>> read that back into powershell after the command finishes? I tried to do
>> this but the DOS pipe “>” didn’t work for whatever reason.
>>
>> E.g.
>> & "$TFExecutablePath\.\tf.exe workspace /delete $Name
>> /s:$TeamFoundationServerUrl /noprompt > c:\test.txt"
>
>
> Try moving "> c:\test.txt" outside of the quotes.
And combine with Jon's suggestion:

& "$TFExecutablePath\tf.exe" workspace /delete $Name
/s:$TeamFoundationServerUrl /i 2>&1 > c:\test.txt

This is something I do all the time with my build scripts (we use TFS also).
You might also find this useful

#--------------------------------------------------------------------
# Helper function to deal with legacy exe exit codes
#--------------------------------------------------------------------
function CheckLastExitCode {
param ([int[]]$successCodes = @(0), [scriptblock]$script)

$Scriptname = $MyInvocation.Scriptname
$ScriptLine = $MyInvocation.ScriptLineNumber - 2
$failureMessage = (gc $Scriptname)[$ScriptLine]

if ($successCodes -notcontains $LastExitCode) {
if ($script) {
"Executing cleanup script: $script"
&$script
}
throw "EXE EXITED WITH CODE ${LastExitCode}: $failureMessage"
}
}

The one thing I like about this WRT TF.exe is that TF.exe returns a partial
success code (1) so you can specify an array of "success" codes in this
function as well as a cleanup function if it fails e.g.:

tf.exe ....
CheckLastExitCode 0,1 { tf.exe undo $path /r /i }

--
Keith

My System SpecsSystem Spec
Old 12-03-2007   #7 (permalink)
Joey


 
 

Re: native command messages/errors



"Keith Hill [MVP]" wrote:
Quote:

> "Marco Shaw [MVP]" <marco.shaw@_NO_SPAM_gmail.com> wrote in message
> news:#5wKAybMIHA.4476@xxxxxx
Quote:

> >
Quote:

> >> Is there a way to “pipe” the native command call to a text file and then
> >> read that back into powershell after the command finishes? I tried to do
> >> this but the DOS pipe “>” didn’t work for whatever reason.
> >>
> >> E.g.
> >> & "$TFExecutablePath\.\tf.exe workspace /delete $Name
> >> /s:$TeamFoundationServerUrl /noprompt > c:\test.txt"
> >
> >
> > Try moving "> c:\test.txt" outside of the quotes.
>
> And combine with Jon's suggestion:
>
> & "$TFExecutablePath\tf.exe" workspace /delete $Name
> /s:$TeamFoundationServerUrl /i 2>&1 > c:\test.txt
>
> This is something I do all the time with my build scripts (we use TFS also).
> You might also find this useful
>
> #--------------------------------------------------------------------
> # Helper function to deal with legacy exe exit codes
> #--------------------------------------------------------------------
> function CheckLastExitCode {
> param ([int[]]$successCodes = @(0), [scriptblock]$script)
>
> $Scriptname = $MyInvocation.Scriptname
> $ScriptLine = $MyInvocation.ScriptLineNumber - 2
> $failureMessage = (gc $Scriptname)[$ScriptLine]
>
> if ($successCodes -notcontains $LastExitCode) {
> if ($script) {
> "Executing cleanup script: $script"
> &$script
> }
> throw "EXE EXITED WITH CODE ${LastExitCode}: $failureMessage"
> }
> }
>
> The one thing I like about this WRT TF.exe is that TF.exe returns a partial
> success code (1) so you can specify an array of "success" codes in this
> function as well as a cleanup function if it fails e.g.:
>
> tf.exe ....
> CheckLastExitCode 0,1 { tf.exe undo $path /r /i }
>
> --
> Keith
>
Thanks for all the suggestions. I ultimately went with adding the 2>&1 to
the end of all of my native commands and i'm now able to see errors/messages
from them in the normal powershell output stream.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Running a command from inside a script, command line is corrupted PowerShell
WebCam starting then stopping & errors messages Live Messenger
explorer errors, program errors, no task manager Vista performance & maintenance
Getting Copy-Item to display messages (like the old COPY command in CMD.EXE) ?? PowerShell
Native command redirection in Monad (MSH) 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