Jump to content
Sign in to follow this  
ragupta

StdinWrite not working if executing powershell.exe from run command

Recommended Posts

ragupta

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.

Share this post


Link to post
Share on other sites
water

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 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
ragupta

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.

Share this post


Link to post
Share on other sites
water

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 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
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
Sign in to follow this  

×