Issue running "Invoke-UAScript" with Credential via endpoint. Issue with loading Modules from api calls?

Good Afternoon,

Been troubleshooting a weird bug after upgrading to 2.9.1 where I have an API/Endpoint that basically calls another script with a few params.

I think its a two part problem. One being with the #Requires -Module ActiveDirectory and or using Invoke-UAScript -Credential

New-PSUEndpoint -Url "/offboard_broken" -Method "Get" -Endpoint {
    Connect-UAServer -ComputerName 'http://localhost:5000' -AppToken $AdminToken

    #region Offboard script section
    $OffboardScriptParams = [ordered]@{
        Script              = 'Offboard.ps1'
        UserAccount         = "Dummy.Account@email.com"
        Credential          = $(Get-UAVariable -Name "SomeCreds")
    }
    Invoke-UAScript @OffboardScriptParams | Tee-Object -Variable OffboardJob | Wait-UAJob

    $CurrentJob = Get-UAJob -Id $OffboardJob.Id
    $JobOutput = $(Get-UAJobOutput -Job $CurrentJob).Data 

    $ReturnData = @{ 
        JobNumber = $($CurrentJob.id)
        data      = $JobOutput
    } | ConvertTo-Json 
    
    $ReturnData

} -Authentication -Role @('Administrator') 

Hitting that endpoint returns the following:

{
    "data": "The script 'Offboard.ps1' cannot be run because the following modules that are specified by the \"#requires\" statements of the script are missing: ActiveDirectory.",
    "JobNumber": 80
}

After commenting out the credential in the dictionary and removing the #Requires -Modules ActiveDirectory from the script that the api is invoking.

New-PSUEndpoint -Url "/offboard_working" -Method "Get" -Endpoint {
    Connect-UAServer -ComputerName 'http://localhost:5000' -AppToken $AdminToken

    #region Offboard script section
    $OffboardScriptParams = [ordered]@{
        Script              = 'Offboard.ps1'
        UserAccount         = "Dummy.Account@email.com"
        #Credential          = $(Get-UAVariable -Name "SomeCreds")
    }
    Invoke-UAScript @OffboardScriptParams | Tee-Object -Variable OffboardJob | Wait-UAJob

    $CurrentJob = Get-UAJob -Id $OffboardJob.Id
    $JobOutput = $(Get-UAJobOutput -Job $CurrentJob).Data 

    $ReturnData = @{ 
        JobNumber = $($CurrentJob.id)
        data      = $JobOutput
    } | ConvertTo-Json 
    
    $ReturnData

} -Authentication -Role @('Administrator') 

Hitting the working endpoint it returns the following module (which is expected) and you can see it returned the correct modules (get-module).

Another weird thing that I noticed was if the script has a #Requires -Module ActiveDirectory at the top I get the following even when I comment out the #Credentials from the api/endpoint

This has been working since version 1.5.4 - There is no error messages in the logs.

Product: PowerShell Universal
Version: 2.9.1
1 Like

Can you let me know what you upgraded from? That might help track it down.

This was my production box which was on 1.5.4 at the time.

However I was able to replicate it on my test box as well which was on 2.8.*

Both are running as a service.

Hope this helps.

I reverted back to 2.7.4 which seems to work fine…

We reverted a change that caused this to happen. The fix will be in 2.9.2.

Best news ive heard all day. Appreciate the follow up.