1
+ <#
2
+ . NOTES
3
+ Copyright (c) ScriptRunner Software GmbH. All rights reserved.
4
+
5
+ THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
6
+ The customer or user is authorized to copy the script from the repository and use them for ScriptRunner.
7
+ The terms of use for ScriptRunner do not apply to this script. In particular, ScriptRunner Software GmbH
8
+ assumes no liability for the function, the use and the consequences of the use of this freely available script.
9
+ PowerShell is a product of Microsoft Corporation. ScriptRunner is a product of ScriptRunner Software GmbH.
10
+
11
+ . SYNOPSIS
12
+ This PowerShell script file contains example PowerShell code calling the ScriptRunner WebService Connector.
13
+ . DESCRIPTION
14
+ Dot source the script in your PowerShell session to make the contained commands available,
15
+ and type "Get-help Start-AsrWeb*" for a contained function to see the detailed description
16
+ of the function and the parameters.
17
+ . LINK
18
+ https://github.com/scriptrunner/ActionPacks/blob/master/ScriptRunner/WebSvcConnector
19
+ #>
20
+
1
21
' '
2
22
' This PowerShell script file contains example PowerShell code calling the ScriptRunner WebService Connector.'
23
+ ' Dot source the script in your PowerShell session to make the contained commands available.'
3
24
' '
4
25
# For conveniance you can set your ScriptRunner server here, and run the functions without the endpoint parameter.
5
- # The default is localhost:port, using the built-in local loopback connector on the ScriptRunner host.
26
+ # The default is http://localhost:8091/, to e.g. use the built-in local loopback connector
27
+ # directly on the ScriptRunner host.
6
28
$defaultserver = ' localhost:8091'
7
29
$defaultendpoint = " http://$defaultserver /ScriptRunner/"
8
30
@@ -13,8 +35,8 @@ $defaultendpoint = "http://$defaultserver/ScriptRunner/"
13
35
' '
14
36
' Use the Start-AsrWebSvcConnector, Start-AsrWebhook, and Start-AsrWebSvcConnector2 function respectively'
15
37
' to give it a try. '
16
- ' The built-in endpoint URI http://localhost:8091/ScriptRunner may work on the ScriptRunner host only.'
17
-
38
+ ' The built-in default endpoint URI http://localhost:8091/ScriptRunner will work on the ScriptRunner host only.'
39
+ ' '
18
40
19
41
# ################################################################
20
42
# ################### Action Webhook API ####################
@@ -32,6 +54,8 @@ $defaultendpoint = "http://$defaultserver/ScriptRunner/"
32
54
33
55
. PARAMETER BodyString
34
56
Arbitrary payload string to be transfered in the body of the POST request.
57
+ ScriptRunner will call your script with this string given as the $bodyString parameter value
58
+ (if your script has such a parameter).
35
59
To send structured data, like a Hashtable, send it as a JSON string, and decode it in your script
36
60
using ConvertFrom-Json.
37
61
@@ -53,10 +77,34 @@ $defaultendpoint = "http://$defaultserver/ScriptRunner/"
53
77
and the ID of the Action.
54
78
You find the Webhook URI of an Action in the Admin App, on the first card of the Action Edit Wizard.
55
79
80
+ . PARAMETER WaitForResult
81
+ Optional: Do not only trigger the given Action in ScriptRunner, but also wait for the Action to
82
+ finish, and fetch and output the results of the Action.
83
+
56
84
. PARAMETER BasicAuthCreds
57
85
Optional: Basic Auth credentials, to use Basic Authentication (on the ScriptRunner STS port).
58
86
Without this parameter, the call will use Windows Integrated Auth for the current user
59
87
(Invoke-WebRequest -UseDefaultCredentials).
88
+
89
+ . EXAMPLE
90
+ Start-AsrWebhook -BodyString 'somerawvalue' -ActionID 10 -WaitForResult
91
+
92
+ This command will start the Action with ID 10 in ScriptRunner, with a raw body value 'somerawvalue' (no JSON),
93
+ and will print the results after the respective script has finished.
94
+ This command assumes ScriptRunner to listen on the endpoint URI http://localhost:8091/ScriptRunner
95
+ (unless you did not change the default at the top of this script); which means you have to run
96
+ this example locally on the ScriptRunner host, and did not switch ScriptRunner to HTTPS or
97
+ to a port different than 8091.
98
+ Alternatively you can run Start-AsrWebhook (locally or from remote) and add the -Endpoint parameter with a
99
+ proper endpoint URI.
100
+
101
+ . EXAMPLE
102
+ Start-AsrWebhook -BodyString '{ "firstName": "John", "lastName": "Doe" }' -ActionID 10 -Endpoint 'https://host.domain.com:8092/ScriptRunner'
103
+
104
+ This command will start the Action with ID 10 in ScriptRunner, with a JSON body value you can decode
105
+ in your script with ConvertFrom-Json.
106
+ This command assumes ScriptRunner to listen on the endpoint URI https://host.domain.com:8092/ScriptRunner,
107
+ i.e. on a machine host.domain.com where a proper SSL certificate is installed and bound on port 8092.
60
108
#>
61
109
62
110
function Start-AsrWebhook {
@@ -71,7 +119,7 @@ function Start-AsrWebhook {
71
119
[string ]$ActionID ,
72
120
[Parameter (ParameterSetName = " ID" )]
73
121
[string ]$Endpoint ,
74
- [switch ]$waitForResult ,
122
+ [switch ]$WaitForResult ,
75
123
[PSCredential ]$BasicAuthCreds = $null
76
124
)
77
125
@@ -129,7 +177,7 @@ function Start-AsrWebhook {
129
177
if ($jcuri ) {
130
178
# Poll this URI until the returned JobControl structure has Running=False, at which point the script has finished.
131
179
# Then the JobControl structure will contain the results along with the script execution report:
132
- if ($waitForResult .IsPresent ) {
180
+ if ($WaitForResult .IsPresent ) {
133
181
$jc = $null
134
182
_WaitForResultFromUri - Uri $jcuri - basicAuthHeader $Headers - jc ([ref ]$jc )
135
183
# if the job has finished, we can access the results.
@@ -155,7 +203,13 @@ function Start-AsrWebhook {
155
203
156
204
. DESCRIPTION
157
205
You can start a ScriptRunner Action by Action name or by Action ID. The OData API expects a POST body
158
- containing JSON data in a specific structure. OData can transport an element ID in the request URI.
206
+ containing JSON data in a specific structure. OData transports the Action ID in the request URI.
207
+ Since PowerShell execution is asynchronous in ScriptRunner (and may take a long time, depending on the script),
208
+ the Action may not have finished when the WebService call returns; therefore, ScriptRunner responds
209
+ with a JobControl control structure containing the status that you can poll (on another URI)
210
+ until the Action completes.
211
+ Due to this asynchronous complexity, it may be preferable to explicitly respond, to the calling system,
212
+ from your script executing in ScriptRunner, using a respective API of the calling system.
159
213
160
214
. PARAMETER Endpoint
161
215
Optional: The ScriptRunner endpoint for the WebService connector, like 'http://server:port/ScriptRunner/' or
@@ -192,11 +246,15 @@ function Start-AsrWebhook {
192
246
Optional: String describing the root cause why the Action was executed
193
247
194
248
. PARAMETER WaitTime
195
- Optional: Time (in seconds) to block the server waiting for the script to finish.
196
- Use carefully! Alternatively, you can poll ScriptRunner for the results:
197
- If the response (containing a JobControl in JSON) returns with Running=True, you
198
- can poll this instance until Running=False; then the instance will contain the
199
- complete results (report, result message, ...).
249
+ Optional: Time (in seconds) to block the server internally waiting for the script to finish.
250
+ Use with care, to avoid blowing up your ScriptRunner instance!
251
+ Alternatively, if you really need the results in your calling system, consider
252
+ * explicitly calling back into the calling system with a new status, directly
253
+ in your script executing in ScriptRunner, or
254
+ * poll ScriptRunner asynchronously for the results in your calling system (as shown in the code below):
255
+ If the response (containing a JobControl in JSON) returns with Running=True, you
256
+ can poll this instance until Running=False; then the instance will contain the
257
+ complete results (report, result message, ...).
200
258
#>
201
259
function Start-AsrWebSvcConnector {
202
260
[CmdletBinding ()]
0 commit comments