• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Trap an Error from an EXE

O

OldDog

#1
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
 
J

Joel Bennett [MVP]

#2
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
>
>
>
 
O

OldDog

#3
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: >

> > 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.
 
O

OldDog

#4
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
 
O

OldDog

#5
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