Invoke-UAScript with parameter

Product: Powershell Universal
Version: 1.5.14
Running in IIS

Having issues executing scripts when they have a parameter.

I’ve made a simple script without parameters, just to make sure everything is working properly. This works as expected.

Working script contents…

‘hello world’

Called via…

$script = Get-UAScript -Name emptyScript.ps1
Invoke-UAScript -Script $script

However, when the content is changed to include a simple parameter, it throws an error.

Param([string]$test)

$test

:heart: PS 80 [4/9/2021 11:37:46 PM] C:\

$script = Get-UAScript -Name emptyScript.ps1

:heart: PS 81 [4/9/2021 11:38:06 PM] C:\

Invoke-UAScript -Script $script -test ‘hello’

Invoke-UAScript : Sequence contains no matching element
At line:1 char:1

  • Invoke-UAScript -Script $script -test ‘hello’
  •   + CategoryInfo          : NotSpecified: (:) [Invoke-UAScript], InvalidOperationException
      + FullyQualifiedErrorId : System.InvalidOperationException,UniversalAutomation.InvokeScriptCommand
    

The log file doesn’t appear to show a problem…

2021-04-09T23:38:22.4449821-04:00 0HM7ROGMIULH2:00000003 [INF] Request starting HTTP/1.1 GET http://myServer.domain.local/api/v1/Script/emptyScript.ps1 (ca22a1cb)
2021-04-09T23:38:22.4467406-04:00 0HM7ROGMIULH2:00000003 [INF] Authorization was successful. (0d575a54)
2021-04-09T23:38:22.4467778-04:00 0HM7ROGMIULH2:00000003 [INF] Executing endpoint ‘“UniversalAutomation.ScriptController.Get (UniversalAutomation)”’ (500cc934)
2021-04-09T23:38:22.4468265-04:00 0HM7ROGMIULH2:00000003 [INF] Route matched with “{action = “Get”, controller = “Script”}”. Executing controller action with signature “UniversalAutomation.Script Get(System.String)” on controller “UniversalAutomation.ScriptController” (“UniversalAutomation”). (122b2fdf)
2021-04-09T23:38:22.4475167-04:00 0HM7ROGMIULH2:00000003 [INF] Executing ObjectResult, writing value of type ‘“UniversalAutomation.Script”’. (8a1b66c8)
2021-04-09T23:38:22.4476419-04:00 0HM7ROGMIULH2:00000003 [INF] Executed action “UniversalAutomation.ScriptController.Get (UniversalAutomation)” in 0.7939ms (afa2e885)
2021-04-09T23:38:22.4476642-04:00 0HM7ROGMIULH2:00000003 [INF] Executed endpoint ‘“UniversalAutomation.ScriptController.Get (UniversalAutomation)”’ (99874f2b)
2021-04-09T23:38:22.4477019-04:00 0HM7ROGMIULH2:00000003 [INF] Request finished in 2.7402ms 200 application/json; charset=utf-8 (791a596a)
2021-04-09T23:38:27.4558560-04:00 0HM7ROGMIULH2:00000004 [INF] Request starting HTTP/1.1 GET http://myServer.domain.local/api/v1/script/4/parameter (ca22a1cb)
2021-04-09T23:38:27.4576617-04:00 0HM7ROGMIULH2:00000004 [INF] Authorization was successful. (0d575a54)
2021-04-09T23:38:27.4576980-04:00 0HM7ROGMIULH2:00000004 [INF] Executing endpoint ‘“UniversalAutomation.ScriptController.GetParametersById (UniversalAutomation)”’ (500cc934)
2021-04-09T23:38:27.4577542-04:00 0HM7ROGMIULH2:00000004 [INF] Route matched with “{action = “GetParametersById”, controller = “Script”}”. Executing controller action with signature “UniversalAutomation.ScriptParameter[] GetParametersById(Int64)” on controller “UniversalAutomation.ScriptController” (“UniversalAutomation”). (122b2fdf)
2021-04-09T23:38:27.4584451-04:00 0HM7ROGMIULH2:00000004 [INF] Executing ObjectResult, writing value of type ‘“UniversalAutomation.ScriptParameter[]”’. (8a1b66c8)
2021-04-09T23:38:27.4585644-04:00 0HM7ROGMIULH2:00000004 [INF] Executed action “UniversalAutomation.ScriptController.GetParametersById (UniversalAutomation)” in 0.7814ms (afa2e885)
2021-04-09T23:38:27.4585921-04:00 0HM7ROGMIULH2:00000004 [INF] Executed endpoint ‘“UniversalAutomation.ScriptController.GetParametersById (UniversalAutomation)”’ (99874f2b)
2021-04-09T23:38:27.4586472-04:00 0HM7ROGMIULH2:00000004 [INF] Request finished in 2.8089ms 200 application/json; charset=utf-8 (791a596a)
2021-04-09T23:38:27.8399705-04:00 0HM7ROGMIULH2:00000005 [INF] Request starting HTTP/1.1 GET http://myServer.domain.local/api/v1/script/4/parameter (ca22a1cb)
2021-04-09T23:38:27.8417987-04:00 0HM7ROGMIULH2:00000005 [INF] Authorization was successful. (0d575a54)
2021-04-09T23:38:27.8418424-04:00 0HM7ROGMIULH2:00000005 [INF] Executing endpoint ‘“UniversalAutomation.ScriptController.GetParametersById (UniversalAutomation)”’ (500cc934)
2021-04-09T23:38:27.8418941-04:00 0HM7ROGMIULH2:00000005 [INF] Route matched with “{action = “GetParametersById”, controller = “Script”}”. Executing controller action with signature “UniversalAutomation.ScriptParameter[] GetParametersById(Int64)” on controller “UniversalAutomation.ScriptController” (“UniversalAutomation”). (122b2fdf)
2021-04-09T23:38:27.8425873-04:00 0HM7ROGMIULH2:00000005 [INF] Executing ObjectResult, writing value of type ‘“UniversalAutomation.ScriptParameter[]”’. (8a1b66c8)
2021-04-09T23:38:27.8426868-04:00 0HM7ROGMIULH2:00000005 [INF] Executed action “UniversalAutomation.ScriptController.GetParametersById (UniversalAutomation)” in 0.7663ms (afa2e885)
2021-04-09T23:38:27.8427092-04:00 0HM7ROGMIULH2:00000005 [INF] Executed endpoint ‘“UniversalAutomation.ScriptController.GetParametersById (UniversalAutomation)”’ (99874f2b)
2021-04-09T23:38:27.8427500-04:00 0HM7ROGMIULH2:00000005 [INF] Request finished in 2.8004ms 200 application/json; charset=utf-8 (791a596a)

Greatly appreciate your help, I will

I can’t reproduce this in my environment. I’m running 1.5.15.

PS C:\Users\adamr> Invoke-UAScript -Script $test -test 'test'

Id                : 1
CreatedTime       : 4/12/2021 2:10:26 PM
StartTime         : 1/1/0001 12:00:00 AM
EndTime           : 1/1/0001 12:00:00 AM

Can you please do the following for me?

try { 
Invoke-UAScript -Script $test -test 'test'
} catch {
   $_.Exception.StackTrace
}

It should help show where in the cmdlet we are throwing that exception.

This is thrown when you’re providing an invalid parameter (extra parameter) in the Invocation.

Try calling the UAScript like this:

$script = Get-UAScript -Name emptyScript.ps1
Invoke-UAScript -Id $script.Id

No go guys… this is what I get from the try/catch

♥ PS 23 [4/13/2021 7:34:41 AM] C:\
>> $s = Get-UAScript -Name emptyScript.ps1

♥ PS 24 [4/13/2021 7:35:58 AM] C:\
>> try { Invoke-UAScript -Id $s.Id -test 'hello'} catch { $_.exception.stacktrace } 

   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
   at UniversalAutomation.InvokeScriptCommand.ProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()
♥ PS 25 [4/13/2021 7:36:03 AM] C:\

Thanks. I’ve improved this error message so we will get back a better description of what is happening. What’s currently happening is that parameters are being returned from the script but they don’t match what is bound but cased on your description of your environment, that doesn’t make much sense.

I’ll provide a link to a PS Module with those updates once the build is done.

some additional information about the environment.

$PSVersionTable

Name Value


PSVersion 5.1.14393.3866
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
BuildVersion 10.0.14393.3866
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

PSU is hosted in IIS based on the instructions here: IIS - PowerShell Universal

The server runs on port 443 with a valid certificate

The server is configured for Windows Auth

My only deviation from the instructions would be I have the root directory as \Universal instead of \inetpub\wwwroot

Other notes:

AppTokens do not work, even when going to swagger page and performing the “Authorize” action. Only -UseDefaultCredentials works with scripts, and Windows auth on the Swagger page.

Using parameters with a script works fine when executing from the admin scripts page

https://server.domain.local/admin/script/emptyScript.ps1

Awesome! Thanks. I’ll look out for the update

Good to know. If you haven’t enabled Anonymous Authentication in IIS, this is why those don’t work.

This build will have the improved error message: https://imsreleases.blob.core.windows.net/universal-nightly/745236499/Universal.win7-x64.1.5.15.zip

You can extract that and just import the module from the cmdlets directory. No need to upgrade PSU.

This is what I get when running with the new module.

:heart: PS 13 [4/13/2021 4:26:42 PM] C:\

try { Invoke-UAScript -Id s.Id -test 'hello' -UseDefaultCredentials -ComputerName https://server.domain.local } catch { _.exception.stacktrace } at UniversalAutomation.InvokeScriptCommand.ProcessRecord()
at System.Management.Automation.CommandProcessor.ProcessRecord()

Can you do me a favor and run it without the catch? It should have a better error message now:

 Invoke-UAScript -Id s.Id -test 'hello' -UseDefaultCredentials -ComputerName https://server.domain.local 

Well that was unexpected…

Invoke-UAScript : Unknown parameter UseDefaultCredentials. Parameters for this script are test
At line:1 char:2

  • Invoke-UAScript -Id $s.Id -UseDefaultCredentials -ComputerName https …
  •  + CategoryInfo          : NotSpecified: (:) [Invoke-UAScript], Exception
     + FullyQualifiedErrorId : System.Exception,UniversalAutomation.InvokeScriptCommand
    

I tabbed out the params. Swear I know how to PowerShell…

Ah, ha! That’s the ticket. I see the issue. Will get this fixed.

Here’s another test kit: https://imsreleases.blob.core.windows.net/universal-nightly/746187455/Universal.win7-x64.1.5.15.zip

If this fixes your issue, I’ll make sure it’s integrated into the 1.5.16 release.

It’s alive!!

Thanks so much, Adam!

1 Like