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 ?

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
6
  • 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 9 '12 at 8:48
  • 1
    @SteveB See updated answer. – Andy Arismendi Jan 10 '12 at 9: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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.