New-UDScript from within another script ran as a job or on demand

I am trying to run another script after a scheduled job finishes. I used

Invoke-UDScript -Script 'myScript.ps1' 

Output is showing unauthorized. According to the documentation I should not need to use an app token in this case? Maybe someone can help me out, I can generate a token but would rather not have to renew it every year.

Call Scripts from Scripts

You can also call UA scripts from UA scripts. When running a job in UA, you don’t need to define an app token or the computer name manually. These will be defined for you. You can just call Invoke-UAScript within your script to start another script. Both jobs will be shown in the UI. If you want to wait for the script to finish, use Wait-UAJob .

Product: PowerShell Universal
Version: 2.2.0

This seems like a bug. I’ll open an issue for it. You might be able to work around that with triggers: Triggers - PowerShell Universal

1 Like

Thank you @adam!

I should just be able to add this to the end of my script that is scheduled (ScheduledJob.ps1)?

New-PSUTrigger -Name 'Trigger' -EventType JobCompleted -TriggerScript MyScript.ps1


I also noticed the ‘Triggers’ option on the side of the UA that I have not explored. I went ahead and added the trigger there, I do not need to see the output of the script, but will I see some kind of notice that the trigger indeed did work?

You can actually configure it in the UI. Go to Automation \ Triggers and then setup a new trigger in there.

The New-PSUTrigger will show up in triggers.ps1 within the .universal folder.

1 Like

Thank you! I figured it out.
I got confused (too early, not enough coffee) when I was reading the dialog for adding a new triggered. I figured it was setup like 'When this script, does this, execute this" and not vice versa. I seen the trigger in my log. Thank you again!

job was triggered
1 Like

Hi Adam, I am not keen on the idea of New-PsuTrigger at the end of a script due to error handling (although i see there are triggers to help with this), ideally i would have a Trigger setup for JobComplete that triggers the same job again, so no matter how long the job takes, it then runs again (i use this for REST calls which can take 5 seconds, can take 1 min).

When I try this, I manually trigger the script, On completion it then indeed does trigger the script again, but when that script finishes it does not continue to trigger.

Any thoughts?