PowerShell S2E5 (multithreading)

preview_player
Показать описание
*How to do multithreading with powershell using powershell start-job
Below a link to a much better video on multithreading and installing software remotely.

*start-job
*powershell multithreading
*get-job
*receive-job
*wait-job
*start-sleep

This video is even better on the multithreading

How to multithread with powershell to speed up your scripts and loops
how to use jobs in powershell to enable multithreading.
using powershell cmdlets : start-job, wait-job and receive-job

Mulithreading script from demo:

#script created by Mark Bakker
$getserviceinfo = { #this is the definition you call from start-job
param( #accepts computername as input argument
[string]$ComputerName
)
function checkSystem ($ComputerName) { #accepts computername as input argument
if ($ComputerName -eq "dsc01") {
Start-Sleep -Seconds 1
}
else {
Start-Sleep -Seconds 1
}
$result = Test-Connection $($ComputerName) -Count 1 -ErrorAction SilentlyContinue -TimeToLive 64
Return $result
}

#here the function gets called
checkSystem $ComputerName
}

$Computers = @("dsc11","dsc12","dsc04","dsc01","dsc02","dsc03",

foreach($computer in $Computers){
Start-Job -name $computer -ScriptBlock $getserviceinfo `
-ArgumentList $computer
}

$runningcount = (get-job | where State -eq running).count

while ($runningcount -ne 0){
$runningcount = (get-job | where State -eq running).count
Write-Output "sleeping for 1 second"
Start-Sleep -Seconds 1
}

$endResult = get-job | Receive-Job -Force -Wait #sometimes 1 job hang forever,force is needed otherwise you will wait foreverto get output :-)
$endResult | ogv

get-job | remove-job -Force
Рекомендации по теме
Комментарии
Автор

People like you are the unsung heroes of this world.

woxrocks