Windows Vista Forums

Trap an Error from an EXE

  1. #1


    OldDog Guest

    Trap an Error from an EXE

    Hi,

    I am using PSinfo.exe to do a software inventory on a list of
    machines.

    my command looks like this:

    PS Scripts:\> $psinfoOutput = C:\scripts\psinfo.exe -s Applications \
    \CHEL1-D1

    PsInfo v1.75 - Local and remote system information viewer
    Copyright (C) 2001-2007 Mark Russinovich
    Sysinternals - www.sysinternals.com

    Could not connect to CHEL1-D1:
    Access is denied.

    As you can see, I can not connect to CHEL1-D1 and access is dinied.

    How can I "Trap" these "errors" and save them in a log or in my case
    an excel file?

    $error is empty (I cleared it) and
    PS Scripts:\> $LASTEXITCODE
    -1




      My System SpecsSystem Spec

  2. #2


    Joel Bennett [MVP] Guest

    Re: Trap an Error from an EXE

    The problem you will have is that PSInfo doesn't actually "output" that
    message, it writes it directly to the console. It can't be redirected
    at all, as far as I can tell.

    On the plus side, as you've already discovered, it returns an error
    code, which you can check and log, but you can't tell from that the
    difference between "Access denied." and "The network path was not
    found." ...

    What you CAN do, is yank the text straight out of the host using the
    console stuff:

    $data = &{
    C:\Users\Joel\Downloads\PsTools\Psinfo.exe \\Mover
    if($LASTEXITCODE -lt 0){ # Capture the last LINE of output:
    $bottom = $top = $Host.UI.RawUI.CursorPosition
    $top.Y -= 1
    $bottom.X += $host.UI.RawUI.WindowSize.Width
    $Rectangle = New-Object `
    management.automation.host.rectangle $Top,$Bottom
    $Buffer = $Host.UI.RawUI.GetBufferContents($rectangle)
    $Error = @($output | Select -Expand Character) -join ""
    ## Do whatever you like with that message now:
    throw $Error
    }
    }


    OldDog wrote:

    > Hi,
    >
    > I am using PSinfo.exe to do a software inventory on a list of
    > machines.
    >
    > my command looks like this:
    >
    > PS Scripts:\> $psinfoOutput = C:\scripts\psinfo.exe -s Applications \
    > \CHEL1-D1
    >
    > PsInfo v1.75 - Local and remote system information viewer
    > Copyright (C) 2001-2007 Mark Russinovich
    > Sysinternals - www.sysinternals.com
    >
    > Could not connect to CHEL1-D1:
    > Access is denied.
    >
    > As you can see, I can not connect to CHEL1-D1 and access is dinied.
    >
    > How can I "Trap" these "errors" and save them in a log or in my case
    > an excel file?
    >
    > $error is empty (I cleared it) and
    > PS Scripts:\> $LASTEXITCODE
    > -1
    >
    >
    >

      My System SpecsSystem Spec

  3. #3


    OldDog Guest

    Re: Trap an Error from an EXE

    On Aug 4, 3:13*pm, "Joel Bennett [MVP]" <Jay...@xxxxxx>
    wrote:

    > The problem you will have is that PSInfo doesn't actually "output" that
    > message, it writes it directly to the console. *It can't be redirected
    > at all, as far as I can tell.
    >
    > On the plus side, as you've already discovered, it returns an error
    > code, which you can check and log, but you can't tell from that the
    > difference between "Access denied." and "The network path was not
    > found." ...
    >
    > What you CAN do, is yank the text straight out of the host using the
    > console stuff:
    >
    > $data = &{
    > * * C:\Users\Joel\Downloads\PsTools\Psinfo.exe \\Mover
    > * * if($LASTEXITCODE -lt 0){ # Capture the last LINE of output:
    > * * * *$bottom = $top = $Host.UI.RawUI.CursorPosition
    > * * * *$top.Y -= 1
    > * * * *$bottom.X += $host.UI.RawUI.WindowSize.Width
    > * * * *$Rectangle = New-Object `
    > * * * * * * * * management.automation.host.rectangle $Top,$Bottom
    > * * * *$Buffer = $Host.UI.RawUI.GetBufferContents($rectangle)
    > * * * *$Error *= @($output | Select -Expand Character) -join ""
    > * * * *## Do whatever you like with that message now:
    > * * * *throw $Error
    > * * }
    >
    >
    >
    > }
    > OldDog wrote:

    > > Hi,
    >

    > > I am using PSinfo.exe to do a software inventory on a list of
    > > machines.
    >

    > > my command looks like this:
    >

    > > PS Scripts:\> *$psinfoOutput = C:\scripts\psinfo.exe -s Applications \
    > > \CHEL1-D1
    >

    > > PsInfo v1.75 - Local and remote system information viewer
    > > Copyright (C) 2001-2007 Mark Russinovich
    > > Sysinternals -www.sysinternals.com
    >

    > > Could not connect to CHEL1-D1:
    > > Access is denied.
    >

    > > As you can see, I can not connect to CHEL1-D1 and access is dinied.
    >

    > > How can I "Trap" these "errors" and save them in a log or in my case
    > > an excel file?
    >

    > > $error is empty (I cleared it) and
    > > PS Scripts:\> *$LASTEXITCODE
    > > -1
    Now that is something I have never seen. Thank you, I'll give it a try.

      My System SpecsSystem Spec

  4. #4


    OldDog Guest

    Re: Trap an Error from an EXE

    On Aug 5, 7:39*am, OldDog <mikef2...@xxxxxx> wrote:

    > On Aug 4, 3:13*pm, "Joel Bennett [MVP]" <Jay...@xxxxxx>
    > wrote:
    >
    >
    >
    >
    >

    > > The problem you will have is that PSInfo doesn't actually "output" that
    > > message, it writes it directly to the console. *It can't be redirected
    > > at all, as far as I can tell.
    >

    > > On the plus side, as you've already discovered, it returns an error
    > > code, which you can check and log, but you can't tell from that the
    > > difference between "Access denied." and "The network path was not
    > > found." ...
    >

    > > What you CAN do, is yank the text straight out of the host using the
    > > console stuff:
    >

    > > $data = &{
    > > * * C:\Users\Joel\Downloads\PsTools\Psinfo.exe \\Mover
    > > * * if($LASTEXITCODE -lt 0){ # Capture the last LINE of output:
    > > * * * *$bottom = $top = $Host.UI.RawUI.CursorPosition
    > > * * * *$top.Y -= 1
    > > * * * *$bottom.X += $host.UI.RawUI.WindowSize.Width
    > > * * * *$Rectangle = New-Object `
    > > * * * * * * * * management.automation.host.rectangle $Top,$Bottom
    > > * * * *$Buffer = $Host.UI.RawUI.GetBufferContents($rectangle)
    > > * * * *$Error *= @($output | Select -Expand Character) -join ""
    > > * * * *## Do whatever you like with that message now:
    > > * * * *throw $Error
    > > * * }
    >

    > > }
    > > OldDog wrote:

    > > > Hi,
    >

    > > > I am using PSinfo.exe to do a software inventory on a list of
    > > > machines.
    >

    > > > my command looks like this:
    >

    > > > PS Scripts:\> *$psinfoOutput = C:\scripts\psinfo.exe -s Applications \
    > > > \CHEL1-D1
    >

    > > > PsInfo v1.75 - Local and remote system information viewer
    > > > Copyright (C) 2001-2007 Mark Russinovich
    > > > Sysinternals -www.sysinternals.com
    >

    > > > Could not connect to CHEL1-D1:
    > > > Access is denied.
    >

    > > > As you can see, I can not connect to CHEL1-D1 and access is dinied.
    >

    > > > How can I "Trap" these "errors" and save them in a log or in my case
    > > > an excel file?
    >

    > > > $error is empty (I cleared it) and
    > > > PS Scripts:\> *$LASTEXITCODE
    > > > -1
    >
    > Now that is something I have never seen. Thank you, I'll give it a try.- Hide quoted text -
    >
    > - Show quoted text -
    Hi,

    I tried it and I am getting an error here:

    $Error = @($output | Select -Expand Character) -j <<<< oin ""

    It does not like -join for some reason.
    As I am not sure what you are doing here, I don't know how to fix it.

    OldDog

      My System SpecsSystem Spec

  5. #5


    OldDog Guest

    Re: Trap an Error from an EXE

    On Aug 5, 10:27*am, OldDog <mikef2...@xxxxxx> wrote:

    > On Aug 5, 7:39*am, OldDog <mikef2...@xxxxxx> wrote:
    >
    >
    >
    >
    >

    > > On Aug 4, 3:13*pm, "Joel Bennett [MVP]" <Jay...@xxxxxx>
    > > wrote:
    >

    > > > The problem you will have is that PSInfo doesn't actually "output" that
    > > > message, it writes it directly to the console. *It can't be redirected
    > > > at all, as far as I can tell.
    >

    > > > On the plus side, as you've already discovered, it returns an error
    > > > code, which you can check and log, but you can't tell from that the
    > > > difference between "Access denied." and "The network path was not
    > > > found." ...
    >

    > > > What you CAN do, is yank the text straight out of the host using the
    > > > console stuff:
    >

    > > > $data = &{
    > > > * * C:\Users\Joel\Downloads\PsTools\Psinfo.exe \\Mover
    > > > * * if($LASTEXITCODE -lt 0){ # Capture the last LINE of output:
    > > > * * * *$bottom = $top = $Host.UI.RawUI.CursorPosition
    > > > * * * *$top.Y -= 1
    > > > * * * *$bottom.X += $host.UI.RawUI.WindowSize.Width
    > > > * * * *$Rectangle = New-Object `
    > > > * * * * * * * * management.automation.host.rectangle $Top,$Bottom
    > > > * * * *$Buffer = $Host.UI.RawUI.GetBufferContents($rectangle)
    > > > * * * *$Error *= @($output | Select -Expand Character) -join ""
    > > > * * * *## Do whatever you like with that message now:
    > > > * * * *throw $Error
    > > > * * }
    >

    > > > }
    > > > OldDog wrote:
    > > > > Hi,
    >

    > > > > I am using PSinfo.exe to do a software inventory on a list of
    > > > > machines.
    >

    > > > > my command looks like this:
    >

    > > > > PS Scripts:\> *$psinfoOutput = C:\scripts\psinfo.exe -s Applications \
    > > > > \CHEL1-D1
    >

    > > > > PsInfo v1.75 - Local and remote system information viewer
    > > > > Copyright (C) 2001-2007 Mark Russinovich
    > > > > Sysinternals -www.sysinternals.com
    >

    > > > > Could not connect to CHEL1-D1:
    > > > > Access is denied.
    >

    > > > > As you can see, I can not connect to CHEL1-D1 and access is dinied.
    >

    > > > > How can I "Trap" these "errors" and save them in a log or in my case
    > > > > an excel file?
    >

    > > > > $error is empty (I cleared it) and
    > > > > PS Scripts:\> *$LASTEXITCODE
    > > > > -1
    >

    > > Now that is something I have never seen. Thank you, I'll give it a try.- Hide quoted text -
    >

    > > - Show quoted text -
    >
    > Hi,
    >
    > I tried it and I am getting an error here:
    >
    > $Error *= @($output | Select -Expand Character) -j <<<< oin ""
    >
    > It does not like -join for some reason.
    > As I am not sure what you are doing here, I don't know how to fix it.
    >
    > OldDog
    OK, Here it is, OLD School

    $psinfoOutput = C:\pstools\psinfo.exe -s Applications \\CHEL1-D1 2>&1

    This puts the errors and the regular output in $psinfoOutput.


    Check it out:

    5# $psinfoOutput = C:\pstools\psinfo.exe -s Applications \\CHEL1-D1
    2>&1
    6# $psinfoOutput
    psinfo.exe :
    At line:1 char:38
    + $psinfoOutput = C:\pstools\psinfo.exe <<<< -s Applications \\CHEL1-
    D1 2>&1
    psinfo.exe : PsInfo v1.75 - Local and remote system information viewer
    At line:1 char:38
    + $psinfoOutput = C:\pstools\psinfo.exe <<<< -s Applications \\CHEL1-
    D1 2>&1
    psinfo.exe : Copyright (C) 2001-2007 Mark Russinovich
    At line:1 char:38
    + $psinfoOutput = C:\pstools\psinfo.exe <<<< -s Applications \\CHEL1-
    D1 2>&1
    psinfo.exe : Sysinternals - www.sysinternals.com
    At line:1 char:38
    + $psinfoOutput = C:\pstools\psinfo.exe <<<< -s Applications \\CHEL1-
    D1 2>&1
    psinfo.exe :
    At line:1 char:38
    + $psinfoOutput = C:\pstools\psinfo.exe <<<< -s Applications \\CHEL1-
    D1 2>&1
    psinfo.exe : The network path was not found. <----- The Problem
    At line:1 char:38
    + $psinfoOutput = C:\pstools\psinfo.exe <<<< -s Applications \\CHEL1-
    D1 2>&1
    psinfo.exe :
    At line:1 char:38
    + $psinfoOutput = C:\pstools\psinfo.exe <<<< -s Applications \\CHEL1-
    D1 2>&1
    psinfo.exe : Connecting to CHEL1-D1...
    At line:1 char:38
    + $psinfoOutput = C:\pstools\psinfo.exe <<<< -s Applications \\CHEL1-
    D1 2>&1
    psinfo.exe :
    At line:1 char:38
    + $psinfoOutput = C:\pstools\psinfo.exe <<<< -s Applications \\CHEL1-
    D1 2>&1
    psinfo.exe : Could not connect to CHEL1-D1:
    At line:1 char:38
    + $psinfoOutput = C:\pstools\psinfo.exe <<<< -s Applications \\CHEL1-
    D1 2>&1

    So you look inside $psinfoOutput for some key words and away you go.

    OldDog

      My System SpecsSystem Spec

Trap an Error from an EXE

Similar Threads
Thread Forum
Vbscript unable to trap error into excel
Hi Guys, I am trying to grab some information about a list of servers into excel files, which works fine. What i am unable to do is trap the...
VB Script
80005000 Error trap
Hi, I can muddle through scripting ok most of the time but I'm not sure how to handle this. So here's my dilemma, I want to use a text file for...
VB Script
Ping error trap?
Hi guys, I've been looking for a way to ping a range of servers. So far so good, but the script ends with a nasty error whenever a server is...
PowerShell
why cannot Trap catch error
hi I have below statement in my code but for some reason the trap is not catching the error. Any reason why ?? function CheckFolder() { trap...
PowerShell
How to get more information about the error from trap statement
Hi, Is there any way we can get more information from a Trap statement about the error that has just occured. Like the Line number where the...
PowerShell
trap error in an error
Is there a way to trap an error inside of an error handler? Here's a short fictional example. In real life I'm trying to log errors and one of...
PowerShell
How to trap this error in PowerShell
this errorhandling drives me nuts, can't figure this one out : as well continue,break as return do not work : how do I trap this the right way,...
PowerShell