Multi-Threading in PowerShell

From WikiWiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Get-Job
Receive-Job
Remove-Job
Resume-Job
Start-Job
Stop-Job
Suspend-Job
Wait-Job


  • create a new the function
  • create the job (pass arguments) and keep the ID as reference
  • get job status
  • receive the data
$templist=$null
$scriptblock={
  param($subscriptionName,$proxycreds)
  [System.Net.WebRequest]::DefaultWebProxy.Credentials = $proxycreds 
  #select-azuresubscription -SubscriptionName $subscriptionName
  (Get-AzureService).servicename | foreach {Get-AzureVM -ServiceName $_ }
}
$id = Start-Job –Name getAllVMOptions –Scriptblock $scriptblock -ArgumentList @($subscriptionName, $global:proxycreds) | select id
while((Get-Job -id $id.id).State -ne "completed" )
{
  $time++
  if($time -ge 100)
    {$time=0}
  #write-host "running"
  Write-Progress -activity "Waiting..." -status "Time: $time"  -PercentComplete ($time)
  Start-Sleep 1 
}
$templist = Receive-Job -Id $id.id