Skip to content

Commit 19c4700

Browse files
committed
adding API description and examples
1 parent 4b2fc89 commit 19c4700

File tree

1 file changed

+69
-11
lines changed

1 file changed

+69
-11
lines changed

ScriptRunner/WebSvcConnector/WebSvcConnector.ps1

+69-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
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+
121
''
222
'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.'
324
''
425
# 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.
628
$defaultserver = 'localhost:8091'
729
$defaultendpoint = "http://$defaultserver/ScriptRunner/"
830

@@ -13,8 +35,8 @@ $defaultendpoint = "http://$defaultserver/ScriptRunner/"
1335
''
1436
'Use the Start-AsrWebSvcConnector, Start-AsrWebhook, and Start-AsrWebSvcConnector2 function respectively'
1537
'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+
''
1840

1941
#################################################################
2042
#################### Action Webhook API ####################
@@ -32,6 +54,8 @@ $defaultendpoint = "http://$defaultserver/ScriptRunner/"
3254
3355
.PARAMETER BodyString
3456
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).
3559
To send structured data, like a Hashtable, send it as a JSON string, and decode it in your script
3660
using ConvertFrom-Json.
3761
@@ -53,10 +77,34 @@ $defaultendpoint = "http://$defaultserver/ScriptRunner/"
5377
and the ID of the Action.
5478
You find the Webhook URI of an Action in the Admin App, on the first card of the Action Edit Wizard.
5579
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+
5684
.PARAMETER BasicAuthCreds
5785
Optional: Basic Auth credentials, to use Basic Authentication (on the ScriptRunner STS port).
5886
Without this parameter, the call will use Windows Integrated Auth for the current user
5987
(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.
60108
#>
61109

62110
function Start-AsrWebhook {
@@ -71,7 +119,7 @@ function Start-AsrWebhook {
71119
[string]$ActionID,
72120
[Parameter(ParameterSetName="ID")]
73121
[string]$Endpoint,
74-
[switch]$waitForResult,
122+
[switch]$WaitForResult,
75123
[PSCredential]$BasicAuthCreds = $null
76124
)
77125

@@ -129,7 +177,7 @@ function Start-AsrWebhook {
129177
if ($jcuri) {
130178
# Poll this URI until the returned JobControl structure has Running=False, at which point the script has finished.
131179
# Then the JobControl structure will contain the results along with the script execution report:
132-
if ($waitForResult.IsPresent) {
180+
if ($WaitForResult.IsPresent) {
133181
$jc = $null
134182
_WaitForResultFromUri -Uri $jcuri -basicAuthHeader $Headers -jc ([ref]$jc)
135183
# if the job has finished, we can access the results.
@@ -155,7 +203,13 @@ function Start-AsrWebhook {
155203
156204
.DESCRIPTION
157205
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.
159213
160214
.PARAMETER Endpoint
161215
Optional: The ScriptRunner endpoint for the WebService connector, like 'http://server:port/ScriptRunner/' or
@@ -192,11 +246,15 @@ function Start-AsrWebhook {
192246
Optional: String describing the root cause why the Action was executed
193247
194248
.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, ...).
200258
#>
201259
function Start-AsrWebSvcConnector {
202260
[CmdletBinding()]

0 commit comments

Comments
 (0)