Jump to content

How to get text written on the console by PowerShell write-host cmdlet?


liet
 Share

Recommended Posts

I am looking for a way to automate an interactive PowerShell script. In essence, it writes some data to the screen using the write-host cmdlet and then wait for keyboard input. I would like to be able to either run the PS script via AutoIT or run the PS script separately and make AutoIT focus on the window.

I can handle the input part using Send function, but I can't seem to be able to read the content of the window.

So far I have tried with StdoutRead, ConsoleRead and WinGetText, but I can't seem to be able to pull any text from the console window. If anyone has done something like this before, I would very much welcome the help with my problem.

Link to comment
Share on other sites

  • Developers

Please show your code that you used with STDOutRead() so we can see whether that looks correct.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

1 hour ago, Jos said:

Please show your code that you used with STDOutRead() so we can see whether that looks correct.

Jos

It goes something like this:

Local $iPID = Run('PowerShell.exe -executionpolicy Bypass -File "C:\Users\QKXC64\Desktop\script.ps1"')

Local $sOutput

For $i = 200 To 1 Step -1
    $sOutput &= StdoutRead($iPID)
    Sleep(25)
Next

MsgBox($MB_SYSTEMMODAL, "", "Received: " & @CRLF & @CRLF & $sOutput)

 

Link to comment
Share on other sites

  • Developers
53 minutes ago, liet said:

It goes something like this:

Something like this huh... ;)

Well that's not going to work as you clearly missed some parameter there.. just look at the example in StdOutRead().

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Moderators

I have been playing around with this, as I am doing a lot with PowerCLI right now for managing VMware environments. You might think about wrapping it in a child GUI, like so (simple example):

#include <GUIEdit.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Local $hGUI_Main, $hGUI_Child, $sPing, $sText = "", $editProgress

$hGUI_Main = GUICreate("Powershell Test", 300, 150)
$btnExit = GUICtrlCreateButton("Exit", 10, 100, 80, 30)
$btnGo = GUICtrlCreateButton("Execute", 200, 100, 80, 30)

    GUISetState(@SW_SHOW)

    _Create_Child($hGUI_Main)
    GUIRegisterMsg($WM_MOVE, "_Position_Child")

    While 1
        $iMsg = GUIGetMsg()
        Switch $iMsg
            Case $GUI_EVENT_CLOSE, $btnExit
                Exit
            Case $btnGo
                $sPing = "Ping www.Google.com"
                _SH_Child($hGUI_Main, $btnGo)
                Sleep(500)
                _execute()
        EndSwitch
    WEnd

    GUIDelete()


Func _execute()
    $sCMD = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command " & $sPing
    $pid = Run($sCMD, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)
    StdinWrite($pid)

    $sSTDOUT = ""
        While 1
            $sOutput = StdoutRead($pid)
            If @error Then ExitLoop
            If $sOutput <> "" Then
                $sText &= $sOutput & @CRLF
                _GUICtrlEdit_AppendText($editProgress, $sText)
            EndIf
        WEnd
EndFunc

Func _Create_Child($hGUI_Main)
    $hGUI_Child = GUICreate("Follower", 600, 625, -1, -1, BitOR($WS_POPUP, $WS_BORDER), 0, $hGUI_Main)
    $editProgress = GUICtrlCreateEdit($sText, 1, 1, 598, 624, BitOR($WS_VSCROLL, $ES_READONLY, $ES_AUTOVSCROLL))
    GUISetFont(11, 400, Default, "Arial")
    _Position_Child($hGUI_Main, 0, 0, 0)
    GUISetState(@SW_HIDE, $hGUI_Child)
EndFunc

Func _Position_Child($hWnd, $iMsg, $wParam, $lParam)
    If $hWnd <> $hGUI_Main Then Return
    Local $aGUI_Main_Pos = WinGetPos($hGUI_Main)
    WinMove($hGUI_Child, "", $aGUI_Main_Pos[0] + $aGUI_Main_Pos[2] + 2, $aGUI_Main_Pos[1])
EndFunc

Func _SH_Child($hGUI_Main, $btnGo)
    If GUICtrlRead($btnGo) = "Execute" Then
        GUISetState(@SW_SHOW, $hGUI_Child)
        GUICtrlSetData($btnGo, "Back")
        WinActivate($hGUI_Main)
    Else
        GUISetState(@SW_HIDE, $hGUI_Child)
        GUICtrlSetData($btnGo, "Execute")
    EndIf
EndFunc

 

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

So far using the CLR.au3 automation there is no descend way to get the data transfer in the console... The only workaround for the moment is to output to Clipboard... and read this back into AutoIT...

I have been reading half the internet and not found a structural solution...

https://www.howtogeek.com/163127/how-powershell-differs-from-the-windows-command-prompt/

Quote

...automate an interactive PowerShell script. In essence, it writes some data to the screen using the write-host cmdlet and then wait for keyboard input ....

I just tested the "ActiveXPoSH" COM object running a Parameter Function that has a mandatory Parameter input... and this COM Object support BI-Directional input.

The PowerShell script is paused untill the parameter is entered...

function Something
{
    Param
    (
         [Parameter(Mandatory=$true, Position=0)]
         [string] $Name
    )

    write-host "Output : $Name "

}

Something

Here is an Example in combination of ActiveXPoSH and  VMWare https://stujordan.wordpress.com/tag/powershell/

Hope this helps...

Link to comment
Share on other sites

Quote

"I have been reading half the internet and not found a structural solution..."

Then the answer must be in the other half :D so far I didn't find it either but also no statements found its not possible

https://www.google.nl/search?q=powershell+pipe+to+stdout
https://www.google.nl/search?q=powershell+kills+stdout

 

 

Link to comment
Share on other sites

There's a lot to read, but not really relevant .... ;)

All of the examples found and discussed are using C# code intermediate solutions... which we are not looking for. I am trying to find a AU3 native solution so we are not depending on anything else...

If not, I we could as well keep on using the "ActiveXPoSH" Com object... which can handle anything even parameter scripts as you can see ... 

 

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