###########################################################################
# SampleInvokeRunbookWithAsync.PS1
###########################################################################
## Following parameters will be passed by VConnect and available for your script
#$HostServerName = @(HostServerName)
#$HostServerPort = @(HostServerPort)
#$UserName = @(UserName)
#$Password = @(Password)
#$Datacenter = @(Datacenter)
#$Cluster = @(Cluster)
#$HostName = @(HostName)
#$SubscriptionId = @(SubscriptionId)
#$VMName = @(VMName)
#$ResourcePoolName = @(ResourcePoolName)
#$FolderName = @(FolderName)
###########################################################################
$scomServer = '[YOUR ORCHESTRATOR SERVER]'
$scomUserDomain = '[YOUR DOMAIN]'
$scomUserName = '[YOUR ADMIN USER]'
$scomUserPassword = '[YOUR PASSWORD]'
# This is for the sample runbook. Use your Runbook Guid
$runBookId = [guid]"32cee37d-bada-4cc6-9f69-bc85e0e08100"
# This is for the sample runbook param. you should look up for your runbook in the $runbook.Parameters
$param1GUID = "957ef51d-9912-4454-84b6-c83854120e6a"
Import-Module C:\inetpub\MgmtSvc-CloudAssert-VConnect\OrchestratorServiceModule.psm1
if (!$?)
{
$errorMessage = "Failure loading the module.`nError Message:`n"
$errorMessage += $error[0].ToString()
$result = @{
IsSuccess = $false
Message = "Error: " + $errorMessage
ErrorCode = 0
}
New-Object PSObject -Property $result
return
}
function CheckJobStatus()
{
param([guid] $JobId,[int] $MaxIteration)
$runBookjob = Get-OrchestratorJob -JobId $JobId -ServiceUrl $url -Credentials $creds
$iterationCount = 0;
while($runBookjob -ne $null -and !(IsCompleted $runBookjob.Status) -and ($iterationCount++ -lt $MaxIteration))
{
start-sleep -seconds 10
$runBookjob = Get-OrchestratorJob -JobId $JobId -ServiceUrl $url -Credentials $creds
if($runBookjob -eq $null -or ($_ -ne $null -and $_.Exception -ne $null))
{
return $null;
}
if(IsErrorStatus($runBookjob.Status))
{
return $runBookjob;
}
}
return $runBookjob;
}
function IsErrorStatus($RunBookstatus)
{
return $RunBookstatus.StartsWith("Error") -or $RunBookstatus.StartsWith("Fail") -or $RunBookstatus.StartsWith("Cancelled");
}
function IsSuccessStatus($RunBookstatus)
{
return $RunBookstatus.Equals("Completed");
}
function IsCompleted($RunBookstatus)
{
$isSuccess = IsSuccessStatus $RunBookstatus;
$isFailed = IsErrorStatus $RunBookstatus;
return ($isSuccess -or $isFailed);
}
function GetErrorResult($message, $resultData)
{
$resultDataJson = ConvertTo-Json -InputObject $resultData -Compress
$result = @{
IsSuccess = $false
Message = $message
Details = $resultDataJson
ErrorCode = 1
}
return $result
}
function GetSuccessResult($message, $resultData)
{
$resultDataJson = ConvertTo-Json -InputObject $resultData -Compress
$result = @{
IsSuccess = $true
Message = $message
Details = $resultDataJson
ErrorCode = 0
}
return $result
}
function GetAsyncResult($resultData)
{
$asyncResultDataJson = ConvertTo-Json -InputObject $resultData -Compress
$asyncResult = @{
IsSuccess = $true
IsWaitingForAsyncCompletion = $true
Message = "Completed async. " + $asyncResultDataJson
ErrorCode = 0
Details = $asyncResultDataJson
}
return $asyncResult;
}
function GetAsyncDataRunBookJobId()
{
if($asyncStateJson -ne $null) {
$asyncInput = ConvertFrom-Json -InputObject $asyncStateJson
return [guid]$asyncInput.RunBookJobId;
}
return $null
}
function GetResultToReturn($job)
{
if($job -eq $null)
{
$msg = "Error: Runbook execution didnt happen." + $error[0]
$errorResult = GetErrorResult $msg
return $errorResult;
}
if(IsErrorStatus $job.Status)
{
$errorResult = GetErrorResult "Error: Runbook error."
return $errorResult;
}
if(IsSuccessStatus $job.Status)
{
$ri=Get-OrchestratorRunbookInstance $job -Credentials $creds
# NOTE: change these based on your Runbook Out Parameters. This sample runbook publishes 1 out string param
$params=@(Get-OrchestratorRunbookInstanceParameter -RunbookInstance $ri -Credentials $creds)
$resultOut = $params | where {$_.Name -eq 'ResultData'}
$resultData = @{ ResultData = $resultOut[0].Value }
$resultDataJson = ConvertTo-Json -InputObject $resultData -Compress
$message = "Job: " + $job.id + ".Status: " + $job.Status + ". Result: " + $resultOut[0].Value
$successResult = GetSuccessResult $message $resultData;
return $successResult;
}
if(!(IsCompleted $job.Status))
{
$asyncResultData = @{ RunBookJobId = $job.Id }
$asyncResult = GetAsyncResult $asyncResultData;
return $asyncResult;
}
}
function ProcessAsyncCallback()
{
$RunBookJobId = GetAsyncDataRunBookJobId
if($RunBookJobId -ne $null) {
$job = CheckJobStatus $RunBookJobId 3
$result = GetResultToReturn $job;
New-Object PSObject -Property $result
return
}
else
{
$errorResult = GetErrorResult "Error: Async Call Back did not receive the required input param: RunBookJobId"
New-Object PSObject -Property $errorResult
return
}
}
function ProcessMain()
{
# Use runbook id
$runbook = Get-OrchestratorRunbook -serviceurl $url -runbookid $runBookId -credentials $creds
if ($runbook -ne $null)
{
$childParams = @{ $param1GUID=$VMName }
$job = Start-OrchestratorRunbook -runbook $runbook -credentials $creds -Parameters $childParams
if ($job -ne $null)
{
$jobId = [guid]($job.Id);
$job = CheckJobStatus $jobId 3;
$result = GetResultToReturn $job;
}
else
{
if($_ -ne $null -and $_.Exception -ne $null)
{
$response = $($_.Exception.Response)
if ($response -ne $null -and $response.ContentLength -gt 0)
{
$reader = [IO.StreamReader] $response.GetResponseStream()
$output = $reader.ReadToEnd()
$reader.Close()
}
}
$msg = $error[0] | out-string
$result = GetErrorResult "Exception while calling execute Runbook: " + $rbarray.Name + ". Errors: " + $output + '. ' + $msg;
}
}
else
{
$msg = $error[0] | out-string
$result = GetErrorResult "Exception while getting Runbook. Errors: " + $msg;
}
return New-Object PSObject -Property $result
}
### Main Entry ###
# get credentials (set to $null to UseDefaultCredentials)
$creds = Get-Credentials $scomUserDomain $scomUserName $scomUserPassword
# create the base url to the service
$url = Get-OrchestratorServiceUrl -server $scomServer
if($IsAsyncCallback)
{
ProcessAsyncCallback
}
else
{
ProcessMain
}
###################
|