Jump to content

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


kor
 Share

Recommended Posts

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
Link to comment
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 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

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?

Link to comment
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 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

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
Link to comment
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 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

Glad you got it working :D

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