API calls with parameters

I’m toying around the UA in my lab to verify and understand being able to make the API calls to start scripts. I’ve got my auth and calling the script working properly, but I’ve been banging my head against a wall figuring out the proper structure for passing the arguement parameters into the script.

Right now I’m working with just Invoke-RestMethod and formatting as application/json content type.

Any input or direction would be appreciated, APIs never seem to be my strongsuit.

1 Like

I’d be very much interested as well to get some hints about how to pass params to a script, I’ve been trying it all day yesterday and didn’t had a chance to make it work with the post method /api/v1/script/{id}.

I’d be really glad to have some help on this :slight_smile:

Many thanks in advance for your help on that !

Cheers !

No one ever done or willing to do that ? :smiley:

i do that all day long xD
so for example, if you have a route like that : /api/v1/script/{1}

the ud code should look like this :

$yourRoute = New-UDEndpoint -Url "/v1/script/:VarName" -Method GET -Endpoint {
    param($VarName)
    # do something with $varname
    # return some json
}

For Post/delete routes

$yourRoute = New-UDEndpoint -Url "/v1/script/:VarName" -Method POST -Endpoint {
    param($VarName,$Body)
    # do something with $varname
    # $body contains some json, so you can convertit to work with a psobject !
}

hope this helps

Hey @LxLechat !

Well, I know how to do it using UDashboard the question here is about UniversalAutomation and how to pass params to scripts that are triggered by the POST method predefined on the UAServer corresponding to /api/v1/script/{1} as explained on the swagger UI.

Thanks for your feedback though :smiley:

Oops sorry, did not see “Universal automation” xD

1 Like

Hey @Speegel,

I just tried this myself and it’s way harder than it needs to be. I’m going to work on simplifying this API so you can call it similar to UD.

The problem is that the body of the request needs to be a very specific format. UA stores all the pipeline output and arguments as CliXml. Right now, it’s expecting a payload that looks like this.

{"powerShellVersion":"6.2.3","jobParameters":[
{"name":"Test","type":"System.Object","value":"<Objs Version=\"1.1.0.1\" xmlns=\"http://schemas.microsoft.com/powershell/2004/04\"><S>asdfasf</S></Objs>"},
{"name":"Test2","type":"System.Object","value":"<Objs Version=\"1.1.0.1\" xmlns=\"http://schemas.microsoft.com/powershell/2004/04\"><S>asdfadsf</S></Objs>"}
]}

I’ve generated the values like this. You’ll need to do it with even very basic types like strings.

 [System.Management.Automation.PSSerializer]::Serialize('456')

After you have the JSON built, you’ll be able to send it like this.

invoke-webrequest http://localhost:10000/api/v1/script/2 -body '{"powerShellVersion":"6.2.3","jobParameters":[{"name":"Test","type":"System.Object","value":"<Objs Version=\"1.1.0.1\" xmlns=\"http://schemas.microsoft.com/powershell/2004/04\"><S>asdfasf</S></Objs>"},{"name":"Test2","type":"System.Object","value":"<Objs Version=\"1.1.0.1\" xmlns=\"http://schemas.microsoft.com/powershell/2004/04\"><S>asdfadsf</S></Objs>"}]}' -Method post -ContentType 'application/json'

Expect a much more simple version of this in 1.0.1. I’ll update this post when that’s available.

1 Like

Thanks a lot for that answer ! I’ll give it a shot and get back to you ! :slight_smile:

I’ve release version 1.0.1. It contains the ability to just add query parameters.

Invoke-RestMethod http://localhost:10000/api/v1/script/1?test=123&test2=345
1 Like

Hey @adam !

Thanks a lot ! Do you think it would be possible to provide params through the body of the POST ?

In like JSON format, something like:

[
    {
        "name":  "param1",
        "value":  "test1"
    },
    {
        "name":  "param2",
        "value":  "test2"
    }
]

or maybe easier

{
            "param1":  "test1",
            "param2":  "test2"
}

thx a lot though for the quick update with the query string solution !

Sure! I can look at getting some structure like that added. It would provide the ability to offer more functionality in the future anyways.

This is much better, as I said on Twitter last night.

I agree with Speegel that JSON format would be a nice bonus. I went ahead and submitted it as enhancement request on the github repo.

1 Like

Hi there !

As there any improvements, additions or modifications to the way we can invoke / trigger a script with parameter from UA using the REST API ?

Many thanks

Btw, I’m as well struggling with feeding params to scripts using Invoke-UAScript, can anyone help me with this ? I’d be very grateful.

Thanks a lot in advance !

Edit: I’ve been able to feed script params using the Set-UAJobFeedback but, in this situation, my script have 2 parameters and the Get-UAJobFeedback cmdlets only returns the first params of the script. If I try to start the script from the UADashboard the modal ask me for 2 params, but if I start the script, using Invoke-UAScript, the script is in Waiting Feedback states and same behaviour, it only asks for one params which, in this case, is the first one.

Any Ideas ?

Edit 2: Found out that you can splat script’s params and pass them to the Invoke-UAScript, tho it seems like the cmdlet automatically reformat the variable adding prefix and suffix and the variable looks like that:

<Objs Version=“1.1.0.1” xmlns=“http://schemas.microsoft.com/powershell/2004/04”>x000D__x000A <S>Ora</S>x000D__x000A</Objs>

is that normal or am I making somethin wrong.

Thx in advance !

Hey @Speegel - Can you please try this on the latest version oh PowerShell Universal? https://ironmansoftware.com/downloads/

It now contains UA and the Invoke-UAScript cmdlet should be fixed. We will be publishing the UA cmdlets to the Gallery soon but they are currently just in the PSU zip.

Hey @adam,

thanks, it works well as you said, using cmdlets with the new version.

did you guys added a way to call script passing params with a POST ?

Hello @adam ,

Pinging you back about invoking scripts from the Management API with parameters, is there any simplified solution available with a POST payload ?

I’ve been trying the 1.5.9 and the -UseDefaultCredentials use very well but still trying to find a simple way to invoke scripts, with params, from the API.

Many thanks in advance

I’ve been starting scripts with parameters for a while now. Here’s an example with splatting:

(The script “some_script_in_automation_section.ps1” accepts 3 parameters: customer, email and user. I get the values from a user than goes through a stepper)

$form_data = $Body | ConvertFrom-Json
$params = @{
    Name  = "some_script_in_automation_section.ps1"
    AppToken  = $AppTokenPU
    customer  = $form_data.context.customer
    email  = $form_data.context.email
    user = $user
}
$job = Invoke-UAScript @params

Edit: sorry you probably mean an API call from another system without powershell universal installed)

hey @Mathijs,

Thx for your answer ! Yes I’m talking about the possibility to pass params to API calls using Invoke-RestMethod like the following example :

irm -Uri https://psu.mydomain.com/api/v1/script/Get-ADUser.ps1 -Method post -UseDefaultCredentials -Body ‘{“ADUserName” : “myUserName”, “UserDomain” : “myDomain”}’ -ContentType ‘application/json’

this would my the API able to be called from any source with or without PSU.

thx in advance