Issues creating or updating UAVariables

Product: PowerShell Universal
Version: 2.0

Its me again. Not able to create/update variables using with either New-UAVariable or Set-UAVariable

#New 
New-UAVariable -Name 'MyVar' -Value $SomeString

#Update
$MyVariable = Get-UAVariable -Name "MyVar"
Set-UAVariable -Variable $MyVariable -Value $SomeString

I receive the following error when using PS Version 5.1.*

Jun 4, 2021 3:38 PM  Updating token 
Jun 4, 2021 3:38 PM [error] Call failed with status code 500 (Internal Server Error): PUT http://localhost:5000/api/v1/Variable

I attempted adding Connect-UAServer -ComputerName “https://myserver/” -AppToken $AppToken but that didnt seem to change much. Any ideas?

Can you look in the log? It’ll have a more clear error: %ProgramData%\PowerShellUniversal

I think this is correct. Let me know if you need anything else.

2021-06-04T16:05:08.8046500-05:00 0HM96Q0GSA7M1:00000004 [INF] Executing endpoint '"UniversalAutomation.VariableController.Post (UniversalAutomation)"' (500cc934)
2021-06-04T16:05:08.8046792-05:00 0HM96Q0GSA7M1:00000004 [INF] Route matched with "{action = \"Post\", controller = \"Variable\"}". Executing controller action with signature "System.Threading.Tasks.Task`1[PowerShellUniversal.Variable] Post(PowerShellUniversal.Variable)" on controller "UniversalAutomation.VariableController" ("UniversalAutomation"). (122b2fdf)
2021-06-04T16:05:08.8062655-05:00 0HM96Q0GSA7M1:00000004 [INF] Executed action "UniversalAutomation.VariableController.Post (UniversalAutomation)" in 1.5711ms (afa2e885)
2021-06-04T16:05:08.8063059-05:00 0HM96Q0GSA7M1:00000004 [INF] Executed endpoint '"UniversalAutomation.VariableController.Post (UniversalAutomation)"' (99874f2b)
2021-06-04T16:05:08.8077837-05:00 0HM96Q0GSA7M1:00000004 [ERR] An unhandled exception has occurred while executing the request. (48a46595)
System.InvalidOperationException: Sequence contains no elements
   at System.Linq.ThrowHelper.ThrowNoElementsException()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at UniversalAutomation.IdentityService.GetIdentityAsync() in D:\a\universal\universal\src\UniversalAutomation\Services\IdentityService.cs:line 97
   at UniversalAutomation.VariableController.Post(Variable variable) in D:\a\universal\universal\src\UniversalAutomation\Controllers\VariableController.cs:line 108
   at lambda_method1534(Closure , Object )
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Universal.Server.Middleware.RoutingMiddleware.Invoke(HttpContext httpContext, IPolicyEvaluator policyEvaluator) in D:\a\universal\universal\src\Universal.Server\Middleware\RoutingMiddleware.cs:line 113
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at AspNetCoreRateLimit.RateLimitMiddleware`1.Invoke(HttpContext context) in D:\a\universal\universal\src\AspNetCoreRateLimit\Middleware\RateLimitMiddleware.cs:line 109
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
2021-06-04T16:05:08.8087655-05:00 0HM96Q0GSA7M1:00000004 [WRN] No exception handler was found, rethrowing original exception. (61fc7fab)
2021-06-04T16:05:08.8104333-05:00 0HM96Q0GSA7M1:00000004 [ERR] Connection id ""0HM96Q0GSA7M1"", Request id ""0HM96Q0GSA7M1:00000004"": An unhandled exception was thrown by the application. (560e7d32)
System.InvalidOperationException: Sequence contains no elements
   at System.Linq.ThrowHelper.ThrowNoElementsException()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at UniversalAutomation.IdentityService.GetIdentityAsync() in D:\a\universal\universal\src\UniversalAutomation\Services\IdentityService.cs:line 97
   at UniversalAutomation.VariableController.Post(Variable variable) in D:\a\universal\universal\src\UniversalAutomation\Controllers\VariableController.cs:line 108
   at lambda_method1534(Closure , Object )
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Universal.Server.Middleware.RoutingMiddleware.Invoke(HttpContext httpContext, IPolicyEvaluator policyEvaluator) in D:\a\universal\universal\src\Universal.Server\Middleware\RoutingMiddleware.cs:line 113
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at AspNetCoreRateLimit.RateLimitMiddleware`1.Invoke(HttpContext context) in D:\a\universal\universal\src\AspNetCoreRateLimit\Middleware\RateLimitMiddleware.cs:line 109
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.HandleException(HttpContext context, ExceptionDispatchInfo edi)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
   at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.ResponseCompression.ResponseCompressionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
2021-06-04T16:05:08.8104864-05:00 0HM96Q0GSA7M1:00000004 [INF] Request finished HTTP/1.1 POST http://localhost:5000/api/v1/Variable application/json;+charset=utf-8 193 - 500 0 - 7.3666ms (791a596a)
2021-06-04T16:05:08.8604174-05:00  [INF] Job 110 completed (05906f41)
2021-06-04T16:05:08.8678513-05:00 0HM96Q0GSA7M0:00000004 [INF] Request starting HTTP/1.1 GET http://localhost:5000/api/v1/job - - (ca22a1cb)
2021-06-04T16:05:08.8686050-05:00 0HM96Q0GSA7JU:0000002D [INF] Request starting HTTP/1.1 GET http://localhost:5000/api/v1/script/NewVar.ps1/job - - (ca22a1cb)
2021-06-04T16:05:08.8692273-05:00 0HM96Q0GSA7M0:00000004 [INF] Authorization was successful. (0d575a54)

That’s exactly what I needed. Thanks.

1 Like

For the app token you are using, do you also have an identity with the same name?

Ive just been using the default admin account in this case.

Ok. Thank you.

Let me know if you need anything else. :slight_smile:

One last thing :smiley:

To be clear, when you created the app token, you entered Admin in the Identity text box? I’ll check out this issue later today but just want to make sure I’m understanding it exactly correct.

Im not even using an apptoken in this case. Am I required to?

We generate one internally but maybe something isn’t working there.

You can do this:

  1. Generate an Admin app token with Admin privileges
  2. Create a new secret variable called AppToken and store the token value.
  3. Call Connect-PSUServer -ComputerName http://localhost:5000 -AppToken $AppToken at the beginning of your script

Ok, oddly that worked with both set/new uavariable. Im not sure if the AuthToken I was using was causing the issues. I usually just have the token as a string in the script rather than reference it via automation/variables $AuthToken. Guessing that is “best practice”.

If this was a one-off issue we can mark this as resolved and ill do some more testing. If I run into another issue related to this ill let you know.

As always, thanks for walking me through this.

1 Like

@adam

Do we need to use a API token when running this from a script inside UA?
I am getting a 401 when trying to do:

Get-UAVariable -Name 'fte_RanToday'

I have a script that I want to run multiple times a day, but as the files get lower and lower, I want to set a variable inside UA that will stop the script from running for today.

You’ll need an app token to call those cmdlets since it uses the REST API to do so.

You should have access to that variable directly by referencing it like $fte_RanToday but won’t be able to update that value without an app token.

1 Like

@adam

Would it possible to at least update the variable without an AppToken or at least create an AppToken for a script that does not expire? I run a script that runs first, sets a variable to know it ran for today, and once it has completed all tasks it will mark that is has completed successfully for today. After that, at midnight, I have another script that sets the variables back to N.

Currently, you can only create an app token that is valid for a year.

What I’ve suggested to others is to create an PSU variable that contains the app token value. Then use the variable within your jobs to manage PSU.

Create a separate job that updates the PSU variable for the app token to a new app token when the token is reaching expiration (once a year).

You can use Grant-PSUAppToken to generate new app tokens.

1 Like

Thank you! I will take a look at doing this.
Would be awesome in the future to be able to generate an AppToken that does not expire, but definitely like the method you posted. I am using it to flip a variable from N to Y that the script compares to determine what needs to be done, since it is ran continuously.

1 Like