2

I'm building a script that perform http request in order to "wake up" some asp.net applications.

The following script is working, but is synchronous :

function WakeUp([string] $url)
{
    Write-Host "Waking up $url ..." -NoNewLine
    $client = new-object system.net.WebClient
    $client.UseDefaultCredentials = $true
    $null = $client.OpenRead($url)
    $client.Dispose()
    Write-Host " Ok"

}
# Run your code that needs to be elevated here
@(
    "http://app1/",
    "http://app2/",
    "http://app3/"
 ) | % { WakeUp $_ }

I'd like to improve this script to avoid having the target site be awaken. In fact, the call to OpenRead waits for an answer before returning.

I've tried to use OpenReadAsync, but the call to Dispose occurs before the request is actually emitted and the target app is not waked up.

What are my options ?

[Edit] Applying Andy Arismendi's suggestions, my script is now :

$WakeUp = {
    Param ([string] $url)
    try 
    {
        Write-Output "Waking up $url ..."
        $client = new-object system.net.WebClient
        $client.UseDefaultCredentials = $true
        $null = $client.OpenRead($url)
        $client.Dispose()
        Write-Output "$url Ok"
    } catch {
        Write-Error $_
        throw $_
    }
}

$jobs = @()
# Run your code that needs to be elevated here
@(
    "http://app1/",
    "http://app2/",
    "http://app3/"
 ) | % { 
    $jobs += Start-Job -ScriptBlock $WakeUp -ArgumentList $_ 
    }

Receive-Job -Job $jobs -Keep
Wait-Job -Job $jobs

This seems to work as expected, but I loose the Write-Host. Any thought ?

Steve B
  • 245
  • 4
  • 13

1 Answers1

5

Background jobs can make your workflow asynchronous.

  • Convert the wake up function to a script block:

    $WakeUp = {
        ([string] $url)
    
        try {
            # Processing...
        } catch {
            throw $_
        }
    }
    
  • For each URL spin up the script block as a background job.

    @(
        "http://app1"
        # More...
    ) | % {
        Start-Job -ScriptBlock $WakeUp -ArgumentList $_
    }
    

Check out my answer from another question to see how you can receive data from the background job and detect which failed.

Update

This addresses the issue in your version about not receiving output. You want to use Wait-Job before Receive-Job to allow them to finish because they are running asynchronously.

$WakeUp = {
    param ([string] $url)
    try {
        Write-Output "Waking up $url ..."
        Start-Sleep -Seconds 3
        if ($url -eq "http://app2/") {
            throw ($url + " failed")
        }
        Write-Output "$url Ok"
    } catch {
        throw $_
    }
}

$jobs = @()
# Run your code that needs to be elevated here
@(
    "http://app1/",
    "http://app2/",
    "http://app3/"
 ) | % { 
    $jobs += Start-Job -ScriptBlock $WakeUp -ArgumentList $_ 
}

Wait-Job -Job $jobs | Out-Null
Receive-Job -Job $jobs -Keep
Andy Arismendi
  • 1,188
  • 5
  • 16
  • 27
  • This seems to works, however I'm loosing my output. Is it possible to keep writing to the output ? I've tried with replacing `Write-Host` with `Write-Ouput`, creating a `$job = @()` array you did in the linked answer, and finally replacing Wait-Job by Receive-Job, but with no success – Steve B Jan 09 '12 at 08:48
  • 1
    @SteveB See updated answer. – Andy Arismendi Jan 10 '12 at 09:51
  • this allows me to get the output, but only at the end of all jobs. Isn't it possible to get an immediate output ? Without immediate output, I only see a black screen before everything is done. – Steve B Jan 10 '12 at 10:00
  • @SteveB Yes you can use a `while` loop getting running events perhaps using `Get-Job -State Running` in combination with `Wait-Job -Any` or use `Register-ObjectEvent` (more advanced). – Andy Arismendi Jan 10 '12 at 11:04
  • @AndyArismendi Your example above the update doesn't work. If I hardcode the url within the scriptblock it works fine so I'm assuming however the start-job is passing parameters is broken. Unfortunately I can't get the debugger to work in the script block to see what's going on. – The Muffin Man Jan 29 '15 at 17:56
  • @AndyArismendi Looks like the way you're consuming (or passing) arguments to the script block is wrong. I couldn't get it to work without using `$args` in the script block. – The Muffin Man Jan 29 '15 at 18:47