Ask and ye, shall receive. I actually built this little diddy a few days ago because I was trying to test for an intermittent error that I was getting with a command. (thousands of executions and finally found that the issue was the actual command (not the script... the actual command... strange). Anyway, I haven't had time to 'clean' this up but it will get you going. This is a simple script to execute a hard coded workflow with no User inputs though I do have the user inputs section definable in the script. The script executes the workflow and waits for the job to complete
Jeremy Goodrum, NetApp
The Pirate
Twitter: @virtpirate
Blog: www.virtpirate.com
--------------------------------------------
function Execute-HTTPPostCommand() {
param(
[string] $target = $null
)
$username = "admin"
$password = "admin"
$webRequest = [System.Net.WebRequest]::Create($target)
$auth = [System.Text.Encoding]::UTF8.GetBytes($username+":"+$password)
$base64 = [System.Convert]::ToBase64String($auth)
$webRequest.Headers.Add("AUTHORIZATION", "Basic $base64");
$webRequest.PreAuthenticate = $true
$webRequest.Method = "POST"
$webRequest.ContentType = "application/xml"
$bytes = [System.Text.Encoding]::UTF8.GetBytes($post.OuterXML)
$webRequest.ContentLength = $bytes.Length
[System.IO.Stream] $requestStream = [System.IO.Stream]$webRequest.GetRequestStream()
$requestStream.Write($bytes,0,$bytes.Length)
$requestStream.Close()
[System.Net.WebResponse] $resp = $webRequest.GetResponse();
$rs = $resp.GetResponseStream();
[System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
[string] $results = $sr.ReadToEnd();
return $results;
}
function Execute-HTTPGetCommand() {
param(
[string] $target = $null
)
$username = "admin"
$password = "admin"
$webRequest = [System.Net.WebRequest]::Create($target)
$auth = [System.Text.Encoding]::UTF8.GetBytes($username+":"+$password)
$base64 = [System.Convert]::ToBase64String($auth)
$webRequest.Headers.Add("AUTHORIZATION", "Basic $base64");
$webRequest.PreAuthenticate = $true
[System.Net.WebResponse] $resp = $webRequest.GetResponse();
$rs = $resp.GetResponseStream();
[System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
[string] $results = $sr.ReadToEnd();
return $results;
}
# <userInputEntry key='vcs' value='192.168.0.31'/>
$post = [xml] "<workflowInput><userInputValues></userInputValues></workflowInput>"
$URL = "http://192.168.0.5/rest/workflows/66187d17-1092-4d2d-9072-2e5b06d9e723/jobs"
for ($i=1;$i -lt 101;$i++) {
$wait = 0
Write-Host "Starting Iteration #$i"
$results = Execute-HTTPPostCommand $URL
$jobs = [xml] $results
$JobURL = $URL + "/" + $jobs.job.jobId
DO {
$results = Execute-HTTPGetCommand $JobURL
$job = [xml] $results
$jobStatus = $job.job.jobStatus.jobStatus
switch ($jobStatus) {
"SCHEDULED" {$wait = 0;break;}
"PENDING" {$wait = 0;break;}
"EXECUTING" {$wait = 0;break;}
"COMPLETED" {$wait = 1;Write-Host "--Workflow Run $i Completed" -foregroundcolor "magenta";break;}
"FAILED" {$wait = 1;Write-Host "--Workflow Failed!!!!" -foregroundcolor "red"; Write-Host $job.job.jobStatus.errorMessage -foregroundcolor "red"; exit -1;break;}
"PLANNING" {$wait =0;break;}
default {$wait = 1;Write-Host "--Unexpected Message - $jobStatus" -foregroundcolor "red"; exit -1;break;}
}
} UNTIL ($wait -eq 1)
}
--------------------------------------------