I’m working to finally put PSU into real use on this end.
I’m adding scripts that are parameterized and will mainly be invoked via API endpoints.
I have an existing script in PSU, living in automation>scripts and working great.
I have created a hello world API endpoint with token authentication and that is working fine,
The issue is having an endpoint invoke an existing script.
This is what I’m trying and the response:
$script = Get-UAScript -Name helloworld.ps1
Invoke-UAScript -Id $script.Id
The term ‘Get-UAScript’ is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
The term ‘Invoke-UAScript’ is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
I really hope there is an easy way to invoke an existing script in PSU with an API endpoint. This seems to be an obvious use case and could even be the primary one for adding API endpoints to begin with.
Product: PowerShell Universal Version: 3.5.5
Recieved help from Adam. Thanks!!
Two key items were provided:
Also adding -Integrated was key.
Still working this out as I’m not getting the output from my hellow world script.
Here is what I have now:
$script = Get-PSUScript -Name helloworld.ps1 -Integrated $sid = $script.Id Write-Output "I'm going to invoke the script named $script with id: $sid" $ScriptResponse = Invoke-PSUScript -Id $script.Id -Integrated New-PSUApiResponse -Body $ScriptResponse -StatusCode 200
Here is what I get:
I'm going to invoke the script named helloworld.ps1 with id: 6
StatusCode : 200
Body : PowerShellUniversal.Job
Cookies : {}
Data : {}
ContentType : text/plain
Headers : {}
File :
I’m sure I just need to figure out what is the correct method for invoking a script while capturing its output
It’s returning the job being run. If you want to invoke the script and wait for the output, use -Wait.
$ScriptResponse = Invoke-PSUScript -Id $script.Id -Integrated -Wait
Adam Driscoll
PowerShell Expert and Developer at Devolutions
That worked great! My script also ran great!
Only issue is the following (sure it is dead simple)
New-PSUApiResponse -Body $ScriptResponse
Causes:
Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.
You shouldn’t have to use New-PSUApiResonse if you don’t need it.
Just reutrn this directly:
Invoke-PSUScript -Id $script.Id -Integrated -Wait
Adam Driscoll
PowerShell Expert and Developer at Devolutions
That did the trick.
Was bitten by changing more than one thing at a time. I well know better than that!
Thanks for the help!!
Hi!
I am trying to do the same. I have a script: HPA-Access-manager.ps1
I try to call this from an endpoint:
$script = Get-PSUScript -Name "HPA-Access-Manager.ps1" #-Integrated
$sid = $script.Id
Write-Output "I'm going to invoke the script named $script with id: $sid"
log says:
[10/24/2024 9:48:50 AM] [Error] [Api-11] Specify a computer name or use the Connect-PSUServer command. [10/24/2024 9:48:50 AM] [Information] [Api-11] Status: 400 Content-Type: application/json Length: 138
Do I need to connect and authenticate from within the API?
(I have also tried setting authentication on the endpoint and sending API token within the headers, in which case I get this:
PS C:\Users\admin-fsemti> $Apptoken = "eyJhbGciO...."
$Headers = @{Authorization = "Bearer $Apptoken"}
Invoke-RestMethod http://localhost:5000/hpa/grantaccess -Method Post -Headers $Headers -Body (@{
} | ConvertTo-Json) -ContentType 'application/json'
I'm going to invoke the script named with id:
PS C:\Users\admin-fsemti>