Sign in to follow this  
Followers 0
kor

how do you issue multiple commands to powershell/console via stdIn ?

8 posts in this topic

#1 ·  Posted (edited)

I need to work with PowerShell for some automation for some Exchange automation that I'm doing.

I need to issue multiple commands, read multiple outputs from those commands, etc. Basically just overall working with a powershell window for a few minutes.

I have the following code that works for issuing multiple commands to a dos window, but the same process doesn't work if I use the Run to open powershell instead of Dos.

#include <Constants.au3>

Global $data, $title, $pid
$pid = Run(@ComSpec, @WindowsDir, @SW_SHOW, $STDIN_CHILD + $STDOUT_CHILD)

If ProcessWait($pid, 5) Then
_StdIn('dir')
_StdIn('ipconfig')
_StdIn('exit')
ProcessWaitClose($pid)
EndIf

Exit

Func _StdIn($input)
If Not ProcessExists($pid) Then Return
$title = $input
StdinWrite($pid, $input & @CRLF)
If Not @error Then
     Sleep(100)
     _StdOut($data)
EndIf
EndFunc

Func _StdOut(ByRef $output)
If Not ProcessExists($pid) Then Return
$output = StdOutRead($pid)
Sleep(100)
MsgBox(0x40000, $title, $data)
EndFunc

I've also come across code by water here on the forum that will interact with powershell correctly, but I don't see how it has the ability to continue working with powershell after the first command has been run.

#include <Constants.au3>
$PID = Run("powershell.exe get-date", @WindowsDir, @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
ConsoleWrite($sSTDOUT)
; Process STDERR
$sSTDERR = ""
While 1
    $sOutput = StdErrRead($pid)
    If @error Then ExitLoop
    If $sOutput <> "" Then $sSTDERR = $sSTDERR & @CRLF & $sOutput
WEnd
ConsoleWrite($sSTDERR)
Edited by kor

Share this post


Link to post
Share on other sites



You use a semicolon to separate the commands. Example can be found (notice -Command at the beginning).


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

That sort of works, but I still need to issue commands based on the output of other commands. I cannot simply just inject 5 commands one after the other.

Is there not a way to work with powershell?

Share this post


Link to post
Share on other sites

I'm not very familiar with PowerShell but couldn't you call PowerShell multiple times with parameters set to the output of a previous command?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I'm not very familiar with PowerShell but couldn't you call PowerShell multiple times with parameters set to the output of a previous command?

not when working with exchange. To connect remotely using an implicit remote connection to exchange this is the command I'm running.

$Password = ConvertTo-SecureString -AsPlainText -Force -String PASSWORD

$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "ADAdministrator", $Password

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchange/powershell -Credential $Credentials

Technically I *can* combine all 3 of these into a single command using your suggested semi-colon method... however I then need to interact with the imported PS session. As soon as powershell closes you lose your connection to exchange and have to re-import all the modules again.

Running commands against exchange such as

"get-user -Identity " & $sUsername & " |select RecipientType"

"Enable-Mailbox -Identity " & $sUsername & "@" & $sDomain & " -Alias """ & $sUsername & """ -Database """ & $sExchangeDB & """"

"Get-Mailbox -Identity " & $sUsername & " | Set-Mailbox -HiddenFromAddressListsEnabled $true"
Edited by kor

Share this post


Link to post
Share on other sites

On the forum you can find a lot of examples how to interact with the DOS console.

Couldn't you start PS in "console mode" and then send all the commands to execute and retrieve the output. With this output you could create the next command etc.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

EDIT:

I've been able to get things to work for my needs with waters code.

Thanks!

Edited by kor

Share this post


Link to post
Share on other sites

Glad you got it working :D


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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  
Followers 0