Jump to content

Recommended Posts

Posted

I have written the following small script to try and capture the output of a PowerShell file signing result.  I am able to get the output for the success, but I am unable to get an output for when the script is unable to sign a file.  Please let me know what I am missing.  Thank you.

$iPID = _RunDOSCommand(" /c powershell -command " & "import-module c:\SignTool\signpack\sign.psm1;" & " Add-Signature " & $sDefault_Dir & $Filename)

Func _RunDOSCommand($sCommand)
    Local $iPID = Run(@ComSpec & $sCommand, "", @SW_HIDE, BitOR($STDERR_CHILD, $STDOUT_CHILD))
    ;ProcessWaitClose($iPID)
    Local $sOutput = ""
    While 1
        $sOutput &= StdoutRead($iPID)
        If @error Then ; Exit the loop if the process closes or StdoutRead returns an error.
            ExitLoop
        EndIf
        ;MsgBox($MB_SYSTEMMODAL, "Stdout Read:", $sOutput)
    WEnd
   While 1
        $sOutput &= StderrRead($iPID)
        If @error Then ; Exit the loop if the process closes or StdoutRead returns an error.
            ExitLoop
        EndIf
        ;MsgBox($MB_SYSTEMMODAL, "Stdout Read:", $sOutput)
    WEnd
    Msgbox(64,"Result", "$sOutput)
    Return $sOutput
EndFunc   ;==>_RunDOSCommand

 

Posted (edited)

Hi.

  • You can use "Start-Transcript" in powershell to write all output to a TXT file you can analyse later on.
  • try $STDERR_MERGED
#include <AutoItConstants.au3>

$sDefault_Dir="some-dir"
$Filename="SomeFile.txt"
_RunDOSCommand(" /c powershell -command " & "import-module c:\SignTool\signpack\sign.psm1;" & " Add-Signature " & $sDefault_Dir & $Filename)

Func _RunDOSCommand($sCommand)
    Local $iPID = Run(@ComSpec & $sCommand, "", @SW_HIDE, $STDERR_MERGED)
    ProcessWaitClose($iPID)
    Local $sOutput = StdoutRead($iPID)
    If @error Then
        MsgBox(48, "Error", @error)
    Else
        MsgBox(0, "StdoutRead with $STDERR_MERGED", $sOutput)
    EndIf
EndFunc   ;==>_RunDOSCommand

 

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Posted (edited)

@Rudi,

Thank you for the STDERR_MERGED example.  That worked for me after I inserted a Return $sOutput.  I am now able to capture both STDOUT and STDERR streams.

/Double thumbs up!

Though I am curious and want to understand why the original method to capture the STDERR stream I used didn't work.  Knowing why it didn't work will help me to understand for future coding.

Edited by LisHawj
Posted

@LisHawj

glad to help.

 

If I'm allowed to comfort you: It took me quite a while to figure out that $STDERR_MEREGED thing, when I needed to grab that info for the first time.

 

And I'm still not through, e.g. the command line version of wireshark (tshark) is writing the number of packets captured so far to always the same position, and with STDOUT as well as STDERR it seems to be impossible to grab that output.

 

 

 

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...