L’industrialisation et la performance sont deux sujets qui reviennent souvent dans l’informatique. Voici la base d’un script Powershell permettant de paralléliser l’exécution d’une tâche plutôt que de rester en mode séquentiel. La finalité est bien évidement d’accélérer vos traitements. Pour la mise en oeuvre du parallélisme Powershell, le principe est le suivant, il y a 3 étapes à retenir :
| 1 | $Throttle = 60 | 
| 1 2 3 4 | $Computers = get-content C:\Temp\computers.txt foreach ($ComputerName in $Computers) { } | 
| 1 | $Command = "Get-WmiObject -Class Win32_BIOS -NameSpace 'root\CIMV2' -ComputerName $ComputerName" | 
Voici le script complet qui permet de gérer le parallélisme.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | clear-host $ScriptDirectory = Split-Path $MyInvocation.MyCommand.Path $Throttle = 60 $ScriptBlock = {    Param (       [guid]$RunGuid,[string]$ComputerName,[string]$Command    )     $RunResult = New-Object PSObject -Property @{       RunGuid = $RunGuid       ComputerName = $ComputerName       Command = $Command     }     invoke-expression -Command $Command    Return $RunResult } $RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, $Throttle) $RunspacePool.Open() $Jobs = @() Try {     $RunGuid = [guid]::NewGuid()     $Computers = get-content C:\Temp\computers.txt     foreach ($ComputerName in $Computers)     {         $Command = "Get-WmiObject -Class Win32_BIOS -NameSpace 'root\CIMV2' -ComputerName $ComputerName"                                $Job = [powershell]::Create().AddScript($ScriptBlock).AddArgument($RunGuid).AddArgument($ComputerName).AddArgument($Command)         $Job.RunspacePool = $RunspacePool         $Jobs += New-Object PSObject -Property @{         		    RunGuid = $RunGuid         		    Pipe = $Job         		    Result = $Job.BeginInvoke()                  }     }     Write-Host "Waiting.." -NoNewline     Do {        Write-Host "." -NoNewline        Start-Sleep -Seconds 1     } While ( $Jobs.Result.IsCompleted -contains $false)     Write-Host "All jobs completed!"     $Results = @()     ForEach ($Job in $Jobs)     {   $Results += $Job.Pipe.EndInvoke($Job.Result)     }     $Results | Out-GridView } Catch {        Write-Host ($_.Exception.Message) } Finally {     Write-Host "Done and now ?" } | 
L’objet de cet article était d’aborder sereinement le parallélisme Powershell, il en est de même dans un autre article mais traitant cette fois de SSIS.
