Page 1 sur 1

[..] Session Powershell et multiligne

Posté : mar. 10 déc. 2019 13:25
par jpascal
Bonjour à tous,

Vu le peu d’intérêt qu'a suscité ce post (viewtopic.php?f=3&t=15290), et mon faible niveau, je vais voir moins grand et rester sur du basique. :lol:

Pour faire simple, j'arrive à créer un process PowerShell et le réutiliser pour lancer différentes commandes.
Cela permet d'améliorer la vitesse de traitement si vous devez faire appel à plusieurs scripts Powershell dans votre programme ou si vous devez réutiliser des variables Powershell dans les différents scripts.

Le seul problème, je n'arrive pas à passer une commande Powershell sur plusieurs lignes.
Pouvez-vous me dire si cela est possible ?
#include <Array.au3>
#include <AutoItConstants.au3>

HotKeySet('{ESC}', "ExitApp")

Global $g_iPSHost = Run('powershell.exe -command - ', @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDERR_MERGED)

Go()

Func Go()
   Local $aPSCmd[0]

   RunPSHost('get-childitem c:\') ; fonctionne

   _ArrayAdd($aPSCmd, 'get-childitem c:\ | ForEach-Object {', 0, '£') ; séparateur défini à £ pour ne pas prendre en compte le |
   _ArrayAdd($aPSCmd, 'write-host $_.Name')
   _ArrayAdd($aPSCmd, 'write-host $_.Mode')
   _ArrayAdd($aPSCmd, '}')

   RunPSHost($aPSCmd) ; ne fonctionne pas

   ConsoleWrite('Fin de la commande' & @CRLF)
EndFunc   ;==>Go

Func RunPSHost($PSCmd)
   Local $sOutput = ''

   If IsArray($PSCmd) Then
      ConsoleWrite('$PSCmd est un tableau' & @CRLF)
      For $i = 0 To UBound($PSCmd) - 2
         ConsoleWrite($PSCmd[$i] & @CRLF)
         StdinWrite($g_iPSHost, $PSCmd[$i] & @CRLF)
         Sleep(1000)
      Next
      ConsoleWrite($PSCmd[UBound($PSCmd) - 1] & @CRLF)
      StdinWrite($g_iPSHost, $PSCmd[UBound($PSCmd) - 1] & ';Write-Host END_OF_OUTPUT' & @CRLF)
   Else
      StdinWrite($g_iPSHost, $PSCmd & ';Write-Host END_OF_OUTPUT' & @CRLF)
   EndIf

   While 1 ; detects when data starts to stream in and then exits the loop
      $sOutput &= StdoutRead($g_iPSHost)
      If @extended > 0 Then ExitLoop
      Sleep(1000)
   WEnd
   While 1 ; data has begun to stream, wait text "END_OF_OUTPUT" which tells first command has completed
      $sOutput &= StdoutRead($g_iPSHost)
      If StringInStr($sOutput, 'END_OF_OUTPUT') Then ExitLoop
      Sleep(1000)
   WEnd
   $sOutput = StringReplace($sOutput, 'END_OF_OUTPUT', '')
   ConsoleWrite($sOutput & @CRLF)
   Return $sOutput
EndFunc   ;==>RunPSHost

Func ExitApp()
   Exit
EndFunc   ;==>ExitApp