I\'ve been searching around the internet and combining lots of different pieces of code, but I\'m just not succeeding at creating a callback for my asynchronous job.
Try this one!
`
$rsPool = [runspacefactory]::CreateRunspacePool(1,2)
$rsPool.Open();
$WebRequest = {
param($url)
return Invoke-WebRequest -Uri ($url)
}
$jobs = @()
$PSinstance = [powershell]::Create();
$PSinstance.AddScript($WebRequest).AddArgument("https://google.com")
$PSinstance.RunspacePool = $rsPool
$Jobs += [PSCustomObject]@{ Pipe = $PSinstance; Status = $PSinstance.BeginInvoke() }
$results=@()
while ($Jobs.Status -ne $null)
{
start-sleep -s 1
write-host "." -nonewline -fore cyan
foreach ($completedjob in $Jobs|?{ $_.Status.IsCompleted -eq $true })
{
$results+=$completedjob.Pipe.EndInvoke($completedjob.Status)
$completedjob.Status = $null
}
}
$rsPool.close();
$results|out-host
`