Skip to content

Commit 8b72afa

Browse files
committed
Add webservice connector sample scripts.
1 parent 8ba8074 commit 8b72afa

File tree

3 files changed

+416
-0
lines changed

3 files changed

+416
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<#
2+
.SYNOPSIS
3+
Start an action with parameters.
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 TargetName
16+
Optional: Target override - the name (exact displayname string) of the target to use.
17+
A target override is possible only if the action has no targets configured, or
18+
if the action requires a selection from a list of given possible targets.
19+
20+
.PARAMETER Reason
21+
Optional: String describing the root cause why the action was executed
22+
23+
.PARAMETER WaitTime
24+
Optional: Time (in seconds) to block the server waiting for the script to finish.
25+
If the response (containing a JobControl in JSON) returns with Running=True, you
26+
can poll this instance until Running=False; then the instance will contain the
27+
complete results (report, result message, ...).
28+
#>
29+
30+
Param
31+
(
32+
[Parameter(Mandatory=$true)]
33+
[string]$ActionName,
34+
[string[]]$ParamNames,
35+
[string[]]$ParamValues,
36+
[string]$TargetName,
37+
[string]$Reason = '',
38+
[int]$WaitTime = 3
39+
)
40+
41+
function Get-ASRJobControl
42+
{
43+
PARAM(
44+
[string]$server,
45+
[int]$id,
46+
[ref]$jc
47+
)
48+
49+
$json = Invoke-WebRequest -Uri "http://$server/ScriptRunner/JobControl($id)" -Method Get -UseDefaultCredentials -ContentType application/json -UseBasicParsing
50+
if (!$json) {
51+
throw 'Request failed.'
52+
}
53+
$list = $json.Content | ConvertFrom-Json
54+
$jc.Value = $list;
55+
}
56+
57+
# set your ScriptRunner server here; for local loopback connector simply set 'localhost:port_number'
58+
$server='localhost:8091'
59+
60+
"Starting action $ActionName ..."
61+
# commands to start the action with the specified value
62+
$uri = "http://$server/ScriptRunner/ActionContextItem/StartASRNamedAction"
63+
64+
# Create a $bodyobj object to build up the JSON body for the web request, e.g. something like this:
65+
####$body = '{"ActionName": "My Remote Action","ParamNames":["ValueA", "ValueB"],"ParamValues":["17", "5"], "Options": [], "RunFlags":[]}'
66+
67+
$bodyobj = @{}
68+
$bodyobj['ActionName'] = $ActionName
69+
if ($TargetName) {
70+
$bodyobj['TargetNames'] = ,$TargetName
71+
}
72+
if ($ParamNames) {
73+
$bodyobj['ParamNames'] = @($ParamNames)
74+
}
75+
if ($ParamValues) {
76+
$bodyobj['ParamValues'] = @($ParamValues)
77+
}
78+
$bodyobj['Options'] = '', $Reason
79+
$bodyobj['RunFlags'] = , $WaitTime
80+
81+
$body = ''
82+
$body = $bodyobj | ConvertTo-Json
83+
$body
84+
85+
# This is the web request (POST with JSON body data as defined above).
86+
# Sending the request with e.g. CURL.EXE would be very similar!
87+
$json = $null
88+
$json = Invoke-WebRequest -Uri $uri -Body $body -Method Post -UseDefaultCredentials -ContentType 'application/json; charset=utf-8' -UseBasicParsing
89+
if (!$json) {
90+
throw 'Request failed.'
91+
}
92+
93+
# show the response
94+
'' + $json.RawContentLength + ' Bytes: ' + $json.StatusCode + ' ' + $json.StatusDescription
95+
$list = $json.Content | ConvertFrom-Json
96+
# This is the ID of the JobControl instance containing the result
97+
$jcids = $list.value.ID
98+
# in case of multiple targets, $jcids is a list.
99+
"Report ID: $jcids (Count = " + $jcids.Count + ")"
100+
101+
# In case of $list.value.Running=True, you would have to poll each JobControl($jcid) for the script to finish:
102+
# GET to URI "http://$server/ScriptRunner/JobControl($jcid)", as in
103+
# Invoke-WebRequest -Uri "http://$server/ScriptRunner/JobControl($jcid)" -Method Get -UseDefaultCredentials -ContentType application/json -UseBasicParsing
104+
105+
$jobs = @{}
106+
$running = $true
107+
while ($running)
108+
{
109+
$running = $false
110+
foreach ($id in $jcids) {
111+
$jc = $null
112+
Get-ASRJobControl -server $server -id $id -jc ([ref]$jc)
113+
$jobs["$id"] = $jc
114+
$running = $running -OR $jc.Running
115+
}
116+
if ($running) {
117+
'...'
118+
Start-Sleep -Seconds 1
119+
}
120+
}
121+
122+
# if every job has finished, we can access the result reports.
123+
foreach ($id in $jcids) {
124+
$jc = $jobs["$id"]
125+
# This is the result message, in case $jc.Running=False.
126+
if ($jc.OutResultMessage) {
127+
"$id : '" + $jc.OutResultMessage + "'"
128+
}
129+
# this is the PowerShell report, in case $jc.Running=False
130+
if ($jc.OutReportString) {
131+
"Report($id):"
132+
'================================='
133+
$jc.OutReportString
134+
'================================='
135+
}
136+
'.'
137+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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

Comments
 (0)