![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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> 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 Specs![]() |
| | #6 (permalink) |
| | 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. & "$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 Specs![]() |
| | #7 (permalink) |
| | 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 > 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 Specs![]() |
![]() |
| 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 | |||