Orchestrator Web Service PowerShell CMDLet Library

I have just put the finishing touches on a PowerShell library for manipulating the Orchestrator web service. The cmdlets are available at https://scorch.codeplex.com/releases/view/107858 and the following functions are exposed

  • Get-SCOEvent
  • Start-SCORunbook
  • Stop-SCOJob
  • Get-SCOMonitorRunbook
  • Get-SCOSubfolder
  • Get-SCORunbookServer
  • Get-SCOJob
  • New-SCOWebserverURL
  • Get-SCORunbook

Each function comes complete with its own help document with examples of how to use it

Using these modules you can do a number of interesting functions, one that I have implemented is failing over monitor policies from one environment to another. As an example, this script to accomplish this is available below

Param(
  [string]$activeEnvironmentName,
  [string]$activeEnvironmentPort,
  [string]$failoverEnvironmentName,
  [string]$failoverEnvironmentPort,
  [string]$baseFolderPath
)

Import-Module scorch.posh

$activeWS = New-SCOWebserverURL $activeEnvironmentName $activeEnvironmentPort
Write-Host "Active Environment:   $activeWS"
$failoverWS = New-SCOWebserverURL $failoverEnvironmentName $failoverEnvironmentPort
Write-Host "Failover Environment: $failoverWS"

Write-Host ""
Write-Host "Loading $activeEnvironmentName Base Folders"
$activeBaseFolders = Get-SCOSubFolder $activeWS $baseFolderPath $true
$activeRBS = @()
foreach($runbook in $activeBaseFolders)
{
	$activePotentialAdd = $runbook.Runbooks | ? {$_.IsMonitor -eq $true}
	if($activePotentialAdd -ne $null)
	{
		$activeRBS += $runbook.Runbooks | ? {$_.IsMonitor -eq $true}
	}
}
$activePaths = $activeRBS | select Path;
$activePaths | ft Path

Write-Host ""
Write-Host "Loading $failoverEnvironmentName Base Folders"
$failoverBaseFolders = Get-SCOSubFolder $failoverWS $baseFolderPath $true
$failoverRBS = @()
foreach($runbook in $failoverBaseFolders)
{
	$failoverPotentialAdd = $runbook.Runbooks | ? {$_.IsMonitor -eq $true}
	if($failoverPotentialAdd -ne $null)
	{
		$failoverRBS += $runbook.Runbooks | ? {$_.IsMonitor -eq $true}
	}
}
$failoverPaths = $failoverRBS | select Path;
$failoverPaths | ft Path

$misMatch = Compare-Object -ReferenceObject $activePaths -DifferenceObject $failoverPaths

if($misMatch)
{
      Write-Host "Monitor Runbooks not idential between target environments"
      Write-Host $misMatch
}
else
{
	foreach($rb in $activeRBS)
	{
		Write-Host "------------------------------------------------------------------------------------------------------------------------"
		Write-Host "Starting work on"  $rb.Path
		Write-Host ""
		Write-Host ""
		
		#Stop Job(s)
		$job = $rb | Get-SCOJob $activeWS -jobStatus "Running" -LoadJobDetails
		
		#Stop jobs if there are none running
		
		if(($job | Measure-Object).count -gt 0)
		{
			#If there are more than 1 jobs running wait for all but the 'monitor' to finish
			$givenMessage = $false
			while($job.ActiveInstances -gt 1)
			{
				if($givenMessage)
				{
					Write-Host -NoNewline .
				}
				else
				{
					Write-Host "Waiting for jobs to complete in $activeEnvironmentName"
					Write-Host "Instance Count: "  $job.ActiveInstances
					$givenMessage = $true
				}
				#sleep -Seconds 5
				$job = $rb | Get-SCOJob $activeWS -jobStatus "Running" -LoadJobDetails
			}
			if($givenMessage) { Write-Host "" }
			
			Write-Host "Stopping in $activeEnvironmentName"
			#Stop the job			
			$job | Stop-SCOJob $activeWS | Out-Null
			$jobStatus = ($job | Get-SCOJob $activeWS).job.Status
			Write-Host -NoNewline .
			#wait for job to stop
			while($jobStatus -eq "Running")
			{
				#update Job Status
				$job = $rb | Get-SCOJob $activeWS -jobStatus "Running" -LoadJobDetails
				$jobStatus = ($job | Get-SCOJob $activeWS).job.Status
				if($jobStatus -eq "Running") 
				{ 
					#if job Status is still running try re-sending a stop
					$job | Stop-SCOJob $activeWS
					$job = $rb | Get-SCOJob $activeWS -jobStatus "Running" -LoadJobDetails
					$jobStatus = ($job | Get-SCOJob $activeWS).job.Status
				}
				Write-Host -NoNewline .				
			}
		}
		else
		{
			Write-Host "Stopping in $activeEnvironmentName"
			Write-Host "Already stopped on $activeEnvironmentName"
		}
		Write-Host ""
		
		#Start Job in failover
		Write-Host "Starting in $failoverEnvironmentName"
		
		#check for job already running
		$failoverRB = Get-SCORunbook $failoverWS -RunbookPath $rb.Path
		$newJob = $failoverRB | Get-SCOJob $failoverWS -jobStatus "Running"
		
		#job is already running
		if(($newJob | Measure-Object).count -gt 0)
		{
			Write-Host "Already running on $failoverEnvironmentName"
		}
		else
		{
			#start the job for the runbook by path
			$newJob = $failoverRB | Start-SCORunbook $failoverWS
			$newJobStatus = ($newJob | Get-SCOJob $failoverWS).job.Status
			
			#wait for the job to start
			while($newJobStatus -eq "Pending")
			{
				Write-Host -NoNewline .
				$newJobStatus = ($newJob | Get-SCOJob $failoverWS).job.Status
			}
		}
		Write-Host ""
		Write-Host "------------------------------------------------------------------------------------------------------------------------"
	}
}

This script ends up looking like the following

Hopefully you will find these modules useful!

Advertisements
Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: