January 4, 2012

PowerShell 3.0 + WWF + WebRequests #mswin #mspartner #Powershell

Here at New Signature we’re huge fans of System Center Orchestrator (nee Opalis). Creating simple runbooks to fully automate even the most complex of heterogenous environments is easy with Orchestrator.

With PowerShell 3.0, some of the workflow automation previously only available with Orchestrator has now been made possible at a lower entry point. Best of all, the workflow included in PowerShell 3.0 is Windows Workflow Foundation (WWF), the same technology found in many existing products we love such as Forefront Identity Manager and SharePoint 2010. This means that not only can you re-use existing XAML-based workflows, but that you can edit them in Visual Studio, further blurring the developer/administrator divide.

To explore the new workflow capabilities in detail, I fired up the PowerShell 3.0 ISE and used one of the other new classes, invoke-webrequest. For anyone that’s used cURL to download web pages before, invoke-webrequest is even more powerful and allows you to download pages to rich objects to take actions on them.

The hypothetical scenario I was testing was the following: if a new bit of code is going to be uploaded to a web-farm, how can you check to ensure that it’s been properly uploaded to all the servers in the farm? This simple script runs multiple parallel tasks to check the last modification date of the websites in question and if they complete, the job finishes. In a more traditional workflow, I’d be actually connecting to each server in parallel remotely, rather than issuing a series of parallel tasks, but the architecture is the same.

Here’s the sample code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
workflow Invoke-ParallelWebCheck {
    $websites = "http://newsignature.com","http://www.newsignature.com/","http://newsignature.com/intune/"
    $StartTime = ([Datetime]::Now)
    $UpdateTime = ([Datetime]::Now).AddMinutes(-3)
    "StartTime: $StartTime UpdateTime: $UpdateTime"
    $Starttime -gt $UpdateTime
    foreach -parallel($site in $websites) {
        inlineScript {
        while ($StartTime -gt $updatetime) {
        "StartTime $StartTime UpdateTime: $UpdateTime"
        $headers = Invoke-WebRequest -uri $site;
        $updatetime = [DateTime]$headers.headers.get_Item("Last-Modified")
        start-Sleep -seconds 3
       
        }
        }
    }
}

 

To execute the workflow, you’ll merely need to type “invoke-parallelwebcheck -asjob” to begin the job execution process. Want to check on the job as it runs? Simply run get-job to get details. If the job itself spawns other jobs, you can see the details from within the get-job cmdlet as well.

Workflows aren’t as full featured as the regular PowerShell syntax, so be sure to read the Windows Management Framework 3.0 – Community Technology Preview #2 for all the caveats. In no time, you’ll be able to kick off jobs on server farms and see the benefits of automation in action.

Comments are closed.