Jump to content

StdinWrite not working if executing powershell.exe from run command


Recommended Posts

Hi.

Local $foo = Run("powershell.exe", "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD) <-- Not Working

Local $foo = Run("cmd.exe", "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)

Sleep(2000)

StdinWrite($foo, "hostname" & @CRLF & "get-date" & @CRLF )

StdinWrite($foo)

Sleep(2000)

; Read from child's STDOUT and show

Local $data

While True

$data &= StdoutRead($foo)

If @error Then ExitLoop

ConsoleWrite("<>")

Sleep(25)

WEnd

ConsoleWrite( $data & @CRLF)

When I use powershell.exe then it just goes into loop though when I use cmd.exe it works fine.

Please advice, how to pass input to powershell console.

Link to comment
Share on other sites

I use the following script (snippet) to create an Exchange mailbox for a user and grab STDOUT and STDERR.

Hope this helps:

$sCMD = "C:\Windows\System32\WindowsPowerShellv1.0powershell.exe -command . " & _
    "'D:Exchange ServerV14binRemoteExchange.ps1'; Connect-ExchangeServer -auto; Enable-Mailbox -Identity " & _
    $Kurzzeichen & " -Alias " & $Kurzzeichen & " -Database " & $sEXDatabase & $sSMTPAddress
$PID = Run($sCMD, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)
StdinWrite($PID, @CRLF)
StdinWrite($PID)
; Process STDOUT
$sSTDOUT = ""
While 1
    $sOutput = StdoutRead($pid)
    If @error Then ExitLoop
    If $sOutput <> "" Then $sSTDOUT = $sSTDOUT & @CRLF & $sOutput
WEnd
; Process STDERR
$sSTDERR = ""
While 1
    $sOutput = StdErrRead($pid)
    If @error Then ExitLoop
    If $sOutput <> "" Then $sSTDERR = $sSTDERR & @CRLF & $sOutput
WEnd
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

  • 2 weeks later...

I use the following script (snippet) toc reate an Exchange mailbox for a user and grab STDOUT and STDERR.

Hope this helps:

$sCMD = "C:\Windows\System32\WindowsPowerShellv1.0powershell.exe -command . " & _
    "'D:Exchange ServerV14binRemoteExchange.ps1'; Connect-ExchangeServer -auto; Enable-Mailbox -Identity " & _
    $Kurzzeichen & " -Alias " & $Kurzzeichen & " -Database " & $sEXDatabase & $sSMTPAddress
$PID = Run($sCMD, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)
StdinWrite($PID, @CRLF)
StdinWrite($PID)
; Process STDOUT
$sSTDOUT = ""
While 1
    $sOutput = StdoutRead($pid)
    If @error Then ExitLoop
    If $sOutput <> "" Then $sSTDOUT = $sSTDOUT & @CRLF & $sOutput
WEnd
; Process STDERR
$sSTDERR = ""
While 1
    $sOutput = StdErrRead($pid)
    If @error Then ExitLoop
    If $sOutput <> "" Then $sSTDERR = $sSTDERR & @CRLF & $sOutput
WEnd

I have changed $scmd, i.e. now $sCMD = 'C:\Windows\System32\WindowsPowerShellv1.0powershell.exe -command "get-date"' and rest same same. When I execute this it just exit with success status, without displaying date.
Link to comment
Share on other sites

Powershell opens the output window, executes the command and exits. If you want the output window to stay open and display the date you need to add some kind of "wait" command (I'm not familiar with PS at all).

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

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