Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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 ?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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