How to use Invoke-PSUScript?

Product: PowerShell Universal
Version: 5.6.8

So I have never needed to use this cmdlet before now, but now I need to set up a PSU endpoint which invokes a script. Every time I attempt to invoke the script with a simple cmdlet usage like:

$Script = Get-PSUScript -Name 'ScriptName.ps1'
Invoke-PSUScript -Script $Script

I get an error like this:

{
    "message": "Failed: System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.\r\n ---> System.Security.Authentication.AuthenticationException: The remote certificate was rejected by the provided RemoteCertificateValidationCallback.\r\n   at System.Net.Security.SslStream.CompleteHandshake(SslAuthenticationOptions sslAuthenticationOptions)\r\n   at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](Boolean receiveFirst, Byte[] reAuthenticationData, CancellationToken cancellationToken)\r\n   at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Boolean async, Stream stream, CancellationToken cancellationToken)\r\n   --- End of inner exception stack trace ---\r\n   at PowerShellUniversal.GrpcCommand.Connect[T](ConnectionType connectionType) in D:\\a\\universal\\universal\\src\\Cmdlets\\Transports\\GrpcCommand.cs:line 531\r\n   at PowerShellUniversal.GrpcCommand.Connect[T](ConnectionType connectionType) in D:\\a\\universal\\universal\\src\\Cmdlets\\Transports\\GrpcCommand.cs:line 537\r\n   at PowerShellUniversal.GrpcCommand.Connect[T](ConnectionType connectionType) in D:\\a\\universal\\universal\\src\\Cmdlets\\Transports\\GrpcCommand.cs:line 537\r\n   at PowerShellUniversal.GrpcCommand.GetService[T]() in D:\\a\\universal\\universal\\src\\Cmdlets\\Transports\\GrpcCommand.cs:line 568\r\n   at UniversalAutomation.GetScriptCommand.EndProcessing() in D:\\a\\universal\\universal\\src\\Cmdlets\\Automation\\GetScriptCommand.cs:line 39\r\n   at System.Management.Automation.Cmdlet.DoEndProcessing()\r\n   at System.Management.Automation.CommandProcessorBase.Complete()"
}

Obviously I can tell there is some sort of SSL issue here, but the documentation isn’t clear to me what the requirements actually are. Am I supposed to use an app token? I have valid SSL certs, so I’m really confused about this.

Hi,
try use the parameter -TrustCertificate while using Get-PSUScript.
If there is still an error you probably need to connect to the psu server itself:

Thanks for the reply! Unfortunately I have already tried that too, which results with this error:

    "message": "Failed to execute Offboarding Main script: Grpc.Core.RpcException: Status(StatusCode=\"Unknown\", Detail=\"Bad gRPC response. HTTP status code: 500\")\r\n   at ProtoBuf.Grpc.Internal.Reshape.UnaryTaskAsyncImpl[TRequest,TResponse](AsyncUnaryCall`1 call, MetadataContext metadata, CancellationToken cancellationToken) in /_/src/protobuf-net.Grpc/Internal/Reshape.cs:line 554\r\n   at Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException[TResult](Task`1 task)\r\n   at Nito.AsyncEx.AsyncContext.<>c__DisplayClass16_0`1.<Run>b__0(Task`1 t)\r\n   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()\r\n   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n--- End of stack trace from previous location ---\r\n   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)\r\n--- End of stack trace from previous location ---\r\n   at Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException[TResult](Task`1 task)\r\n   at Nito.AsyncEx.AsyncContext.Run[TResult](Func`1 action)\r\n   at PowerShellUniversal.ValueTaskExtensions.ToResult[T](ValueTask`1 valueTask) in D:\\a\\universal\\universal\\src\\PowerShellUniversal\\ValueTaskExtensions.cs:line 73\r\n   at PowerShellUniversal.GrpcCommand.Connect[T](ConnectionType connectionType) in D:\\a\\universal\\universal\\src\\Cmdlets\\Transports\\GrpcCommand.cs:line 492\r\n   at PowerShellUniversal.GrpcCommand.Connect[T](ConnectionType connectionType) in D:\\a\\universal\\universal\\src\\Cmdlets\\Transports\\GrpcCommand.cs:line 537\r\n   at PowerShellUniversal.GrpcCommand.Connect[T](ConnectionType connectionType) in D:\\a\\universal\\universal\\src\\Cmdlets\\Transports\\GrpcCommand.cs:line 537\r\n   at PowerShellUniversal.GrpcCommand.GetService[T]() in D:\\a\\universal\\universal\\src\\Cmdlets\\Transports\\GrpcCommand.cs:line 568\r\n   at UniversalAutomation.GetScriptCommand.EndProcessing() in D:\\a\\universal\\universal\\src\\Cmdlets\\Automation\\GetScriptCommand.cs:line 39\r\n   at System.Management.Automation.Cmdlet.DoEndProcessing()\r\n   at System.Management.Automation.CommandProcessorBase.Complete()"

I dont have a shorter script but this is something we use which works:


The gRPC error normally occurs if you are not connected to the psu server

I don’t know why -TrustCertificate was giving me issues, but this seemed to work

$job = Invoke-PSUScript -Name "test_generic.ps1" -Integrated #-TrustCertificate

Thanks for the help!

1 Like

It depends on what security mode you’re running PSU with (integrated, permissive or strict).

gRPC errors usually happen if there’s permission / connectivity issues with the management API.

Having to supply -Integrated to get this to work might make sense if you’re in permissive mode.

I was wondering about that, and I was trying to recall where to find the security mode setting. Is that in the appsettings.json? Docs aren’t very clear to me

EDIT: Nevermind, once again I find what I am looking for only after I spent hours looking yesterday.

To be fair, the search box at the top only goes by indexed words on the pages, that article has ‘SecurityModel’ but doesnt have ‘Security Mode’ which is what I think most people would search for in this case. It also doesnt seem to index the codeblocks, so if you search permissive it doesnt come up under that either.

Anyway, glad you found what you were looking for!