Endpoint: Send variables

How do i actually send variables within the REST API that could trigger a script in the backend to create a structure based on those variables sent in the API.
I have an Endpoint with GET and PUT and have tried numerous times without success yet.

What i have done:

Invoke-RestMethod http://localhost:5000/Testing123 -Method Post -Body (@{
Variable1 = “Setting1”
Variable2 = “Setting2”
Variable3 = “Setting3”
} | ConvertTo-Json) -ContentType ‘application/json’

And the script in the API is running: Write-Output " Displaying $Variable1 + $Variable2 + $Variable3"

Any tip is welcome how to acomplish this.

Can you share your endpoint code?


Right now im testing this one just to get the logic to work in my head:

Rest API:
Invoke-RestMethod http://localhost:5000/scripts123 -Method POST UseDefaultCredentials -Body ‘{“ADUserName” : “myUserName”, “UserDomain” : “myDomain”}’ -ContentType ‘application/json’

Endpoint code:
Get-Aduser -Identity $ADUserName

Found this thread and im not sure if it should even work like this :slight_smile:


This is my endpoint code:
Write-Host ($Headers[“Content-Type”])
Write-Host ($Body)
$Object = $Body | ConvertFrom-Json
“Country: ‘$($Object.Country)’”
“Function: ‘$($Object.Function)’”
“Parameter3: ‘$($Object.Parameter3)’”
Get-ADuser -Identity $Parameter3

This is my API call:
Invoke-RestMethod http://localhost:5000/api/v1/json -Method POST -Body (@{
Country = ‘Sweden’
Function = ‘FunctionName123’
Parameter3 = ‘UserName123’
} | ConvertTo-Json) -ContentType ‘application/json’

How can i actually use the variables sent to be utlized in backend code of powershell ?

Not very much for answers here but this is working now:

This is my endpoint code:
[Parameter(Position=1, mandatory=$true)]

#Execution User
New-ADUser -Name $ADUserName -SamAccountName $Parameter1 -DisplayName $Parameter1
#Tagging Resource
Set-ADUser -Identity $Parameter1 -Add @{msExchExtensionAttribute16=“$Parameter4”}

#Execution Group
New-ADGroup -Name $ADGroupName -GroupScope Global

#Tagging Resource
Set-ADgroup -Identity $ADUserName -Add @{msExchExtensionAttribute16=“$Parameter4”}

This is my API call:
Invoke-RestMethod http://localhost:5000/api/v1/json -Method POST -Body (@{
Parameter1 = 'Parameter1 ’
Parameter2 = 'Parameter2 ’
Parameter3 = 'Parameter3 ’
Parameter3 = ‘Parameter4’
} | ConvertTo-Json) -ContentType ‘application/json’

Now im after mandatory fields in the API to be used. Done and edited.

1 Like

With psu 4.2.5 you can tag mandatory fields but it’s a bug so when you do Body will stop working.
They have added a fix to 4.2.6 that will be released soon

Okey, in what purpose are you using that if i may ask? =)
Would be interesting if the API could respond back exactly what mandatory field is missing an input as an reply to requester, maybe tags can help here.

I use to set the variable in the url.
Then send the body as json so I can convert it to an object and populate more things.