Powershell – Waking up web server using powershell script

powershell

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 ?

Best Answer

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