Jump to content

Capturing Output from Powershell


DigDeep
 Share

Recommended Posts

Hi,

I have a Powershell script which I cannot capture output in any external format of text or any other file and I want AutoIT to read the Output ($Result). Is that doable?

Powershell Output capture:

if ($data -match $GetOps) {
  $Result = "True"
}
ELse{
  $Result = "False"
}

 

AutoIT:

Local $PScript = "C:\Test.ps1"
Local $Txt = "", $sSTDout = ""
Local $RunCMD = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command . '" & $PScript & "'"
Local $pid = Run($RunCMD, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)
    StdinWrite($pid)

    $GetOutput = StdoutRead($pid)
    While 1
        $GetOutput = StdoutRead($pid)
            If @error Then ExitLoop
        If $GetOutput <> "" Then
            MsgBox("", "", $GetOutput)
        EndIf
    WEnd

Problem is running this, I am able to capture the Powershell PID but not the output.

Please help...

Edited by DigDeep
Link to comment
Share on other sites

I use the following code to grab the output of a PS script:

$sCMD = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file " & @ScriptDir & "\NM.ps1"
    $pid = Run($sCMD, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)
    StdinWrite($pid, @CRLF) ; Close STDIN
    StdinWrite($pid)
    ; Get output from STDOUT
    $sSTDOUT = ""
    While 1
        $sOutput = StdoutRead($pid)
        If @error Then ExitLoop
        If $sOutput <> "" Then $sSTDOUT = $sSTDOUT & @CRLF & $sOutput ; Drops empty lines
    WEnd
    ; Get output from STDERR
    $sSTDERR = ""
    While 1
        $sOutput = StderrRead($pid)
        If @error Then ExitLoop
        If $sOutput <> "" Then $sSTDERR = $sSTDERR & @CRLF & $sOutput ; Drops empty lines
    WEnd

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • 1 month later...

What about from a ps commandline?

I have a PShell function I want to pass commands to, and return the PS console output.

Like so:

PShell_ConsoleCapture("Get-ADComputer -Identity PC-Name") ; example PS only

Func PShell_ConsoleCapture($sCmdline)
    $sCMD = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command " & $sCmdline
    $pid = Run($sCMD, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)
    StdinWrite($pid, @CRLF) ; Close STDIN
    StdinWrite($pid)
    ; Get output from STDOUT
    $sSTDOUT = ""
    While 1
        $sOutput = StdoutRead($pid)
        If @error Then ExitLoop
        If $sOutput <> "" Then $sSTDOUT = $sSTDOUT & @CRLF & $sOutput ; Drops empty lines
    WEnd
    ; Get output from STDERR
    $sSTDERR = ""
    While 1
        $sOutput = StderrRead($pid)
        If @error Then ExitLoop
        If $sOutput <> "" Then $sSTDERR = $sSTDERR & @CRLF & $sOutput ; Drops empty lines
    WEnd
    ConsoleWrite("Output:" & @CRLF & $sOutput)
    ConsoleWrite("$sSTDERR:" & @CRLF & $sSTDERR)
    Return
EndFunc

Neither $sOutput nor $sSTDERR contain data.

Any help would be appreciated.

Link to comment
Share on other sites

@nikink

__ExampleA()
Func __ExampleA()
    Local $o_CmdString = ' -command get-module -listavailable'
    Local $o_powershell = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
    Local $o_Pid = Run($o_powershell & $o_CmdString, '', @SW_Hide, $STDERR_CHILD + $STDOUT_CHILD)
    ProcessWaitClose($o_Pid)

    Local $o_Output = StdoutRead($o_Pid)
    ConsoleWrite($o_Output & @CRLF)
EndFunc

 

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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