|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Start an action with parameters using the WebServiceConnector API2. |
| 4 | +
|
| 5 | + .PARAMETER ActionName |
| 6 | + The name (exact name string) of the action to execute. |
| 7 | +
|
| 8 | + .PARAMETER ParamNames |
| 9 | + Optional: List of parameter names required for the action (e.g., "abra","bebra") |
| 10 | + (i.e., the script parameters you want to set). |
| 11 | +
|
| 12 | + .PARAMETER ParamValues |
| 13 | + Optional: List of parameter values (matching the ParamNames list; e.g., "val1","val2") |
| 14 | +
|
| 15 | + .PARAMETER StartedBy |
| 16 | + Optional: Name or email of the end user to run this action for. |
| 17 | + If this parameter is specified, this name will show in the report as Started By, |
| 18 | + and will be set to $SRXEnv.SRXStartedBy for your script. Otherwise the |
| 19 | + WebServiceConnector service account will be used. |
| 20 | +
|
| 21 | + .PARAMETER TargetName |
| 22 | + Optional: Target override - the name (exact displayname string) of the target to use. |
| 23 | + A target override is possible only if the action has no targets configured, |
| 24 | + or if the action requires a selection from a list of given possible targets. |
| 25 | +
|
| 26 | + .PARAMETER Reason |
| 27 | + Optional: String describing the root cause why the action was executed, |
| 28 | + for the report. |
| 29 | +#> |
| 30 | + |
| 31 | +Param |
| 32 | +( |
| 33 | + [Parameter(Mandatory=$true)] |
| 34 | + [string]$ActionName, |
| 35 | + [string[]]$ParamNames, |
| 36 | + [string[]]$ParamValues, |
| 37 | + [string]$StartedBy = '', |
| 38 | + [string]$TargetName, |
| 39 | + [string]$Reason = '' |
| 40 | +) |
| 41 | + |
| 42 | +function Get-ASRJobControl |
| 43 | +{ |
| 44 | + PARAM( |
| 45 | + [string]$server, |
| 46 | + [int]$id, |
| 47 | + [ref]$jc |
| 48 | + ) |
| 49 | + Get-ASRJobControlFromUri -Uri "http://$server/ScriptRunner/JobControl($id)" -jc ([ref]$jc) |
| 50 | +} |
| 51 | + |
| 52 | +function Get-ASRJobControlFromUri |
| 53 | +{ |
| 54 | + PARAM( |
| 55 | + [string]$Uri, |
| 56 | + [ref]$jc |
| 57 | + ) |
| 58 | + $json = $null |
| 59 | + $json = Invoke-WebRequest -Uri $Uri -Method Get -UseDefaultCredentials -ContentType application/json -UseBasicParsing |
| 60 | + if (!$json) { |
| 61 | + throw 'Request failed.' |
| 62 | + } |
| 63 | + $list = $json.Content | ConvertFrom-Json |
| 64 | + $jc.Value = $list; |
| 65 | +} |
| 66 | + |
| 67 | +# set your ScriptRunner server here; for local loopback connector simply set 'localhost:port_number' |
| 68 | +$server='localhost:8091' |
| 69 | + |
| 70 | +"Starting action $ActionName ..." |
| 71 | +# commands to start the action with the specified value |
| 72 | +$uri = "http://$server/ScriptRunner/api2/StartAction" |
| 73 | + |
| 74 | +# Create a $bodyobj object to build up the JSON body for the web request, e.g. something like this: |
| 75 | +####$body = '{"ActionName": "My Remote Action","sr_ValueA":15,"sr_ValueB":5, "StartedBy": "[email protected]", "Reason":"Why do we do this"}' |
| 76 | + |
| 77 | +$bodyobj = @{} |
| 78 | +$bodyobj['ActionName'] = $ActionName |
| 79 | +if ($ParamNames) { |
| 80 | + for ($i=0; $i -lt $ParamNames.Length; $i++) { |
| 81 | + $pname = $ParamNames[$i] |
| 82 | + $pvalue = $ParamValues[$i] |
| 83 | + $bodyobj['sr_' + $pname] = $pvalue |
| 84 | + } |
| 85 | +} |
| 86 | +if ($StartedBy) { $bodyobj['StartedBy'] = $StartedBy } |
| 87 | +if ($TargetName) { $bodyobj['TargetName'] = $TargetName } |
| 88 | +if ($Reason) { $bodyobj['Reason'] = $Reason } |
| 89 | +$bodyobj['WaitTime'] = 0 |
| 90 | + |
| 91 | +$body = '' |
| 92 | +$body = $bodyobj | ConvertTo-Json |
| 93 | +$body |
| 94 | + |
| 95 | +# This is the web request (POST with JSON body data as defined above). |
| 96 | +# Sending the request with e.g. CURL.EXE would be very similar! |
| 97 | +$json = $null |
| 98 | +$json = Invoke-WebRequest -Uri $uri -Body $body -Method Post -UseDefaultCredentials -ContentType 'application/json; charset=utf-8' -UseBasicParsing |
| 99 | +if (!$json) { |
| 100 | + throw 'Request failed.' |
| 101 | +} |
| 102 | + |
| 103 | +# show the response |
| 104 | +'' + $json.RawContentLength + ' Bytes: ' + $json.StatusCode + ' ' + $json.StatusDescription |
| 105 | +'...' |
| 106 | +if ($json.StatusCode -eq 202) { |
| 107 | + $json.Headers |
| 108 | + $Uri = $json.Headers['Location'] |
| 109 | + "Relocating to $Uri ..." |
| 110 | +} |
| 111 | +elseif ($json.StatusCode -eq 200) { |
| 112 | + $list = $json.Content | ConvertFrom-Json |
| 113 | + # This is the ID of the JobControl instance containing the result |
| 114 | + $jcids = $list.value.ID |
| 115 | + $id = $jcids[0] |
| 116 | + # in case of multiple targets, $jcids is a list. |
| 117 | + "Report ID: $jcids (Count = " + $jcids.Count + ")" |
| 118 | + $Uri = "http://$server/ScriptRunner/JobControl($id)" |
| 119 | +} |
| 120 | +else { |
| 121 | + throw "Error: Status Code $($json.StatusCode)" |
| 122 | +} |
| 123 | +# In case of $list.value.Running=True, you would have to poll each JobControl($jcid) for the script to finish: |
| 124 | +# GET to URI "http://$server/ScriptRunner/JobControl($jcid)", as in |
| 125 | +# Invoke-WebRequest -Uri "http://$server/ScriptRunner/JobControl($jcid)" -Method Get -UseDefaultCredentials -ContentType application/json -UseBasicParsing |
| 126 | +'...' |
| 127 | +Start-Sleep -Seconds 2 |
| 128 | +$running = $true |
| 129 | +while ($running) |
| 130 | +{ |
| 131 | + $running = $false |
| 132 | + $jc = $null |
| 133 | + Get-ASRJobControlFromUri -Uri $Uri -jc ([ref]$jc) |
| 134 | + $running = $running -OR $jc.Running |
| 135 | + if ($running) { |
| 136 | + 'running...' |
| 137 | + Start-Sleep -Seconds 1 |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +# if the job has finished, we can access the result report. |
| 142 | +# this is the PowerShell report, in case $jc.Running=False |
| 143 | +if ($jc.OutReportString) { |
| 144 | + "Report($Uri):" |
| 145 | + '=================================' |
| 146 | + $jc.OutReportString |
| 147 | + '=================================' |
| 148 | +} |
| 149 | +# This is the result message, after $jc.Running=False. |
| 150 | +if ($jc.OutResultMessage) { |
| 151 | + "ResultMessage: '" + $jc.OutResultMessage + "'" |
| 152 | +} |
0 commit comments