Product: PowerShell Universal
Version: 5.6.7
I’m working with an App page that uses a custom module that calls a remote API but requires auth. I have this module set as a module that is imported in the Environment that the App runs and I have a Startup Script that sets the credential for the module (module name/functions have been generalized).
Relevant config lines:
New-PSUApp -Name "Server Management" -FilePath "Dashboards\Server Management\Dashboard.ps1" -BaseUrl "/" -Environment "Dedicated - App - Server Management" -Authenticated -SessionTimeout 120 -IdleTimeout 60 -AutoDeploy -Description "Server Automation Dashboard"
New-PSUEnvironment -Name "Dedicated - App - Server Management" -Modules @('MyModule1','MyModule2') -Variables @('*') -PersistentRunspace -StartupScript @('Dashboards\Server Management\Includes\Base.ps1') -Description "PS Environment for the Server Management environment." -Type "WindowsPowerShell"
#Snip from Dashboards\Server Management\Includes\Base.ps1
Set-MyModule1Credential -Environment $Global:ExecutionEnvironment.MyModule1 -Credential $Secret:CredModule1
This sets the credential within the module and is stored in a script variable inside the module so functions in that module can run without having to specify the credential every time (this module is used in manual work as well as this automation).
However, and it’s seemingly entirely random, it seems that sometimes on pages within the App that call this API, the thread/environment/PowerShell session that runs the API call has not ran the StartupScript and it prompts in the interface for a credential.
I have tried debugging this myself, to the extent of starting transcription in the Startup Script to capture verbose output:
if (!(Test-Path -Path "D:\PSU\ScriptLogs\ServerManagement\$([DateTime]::Now.ToString('yyyyMMdd'))")) { New-Item -ItemType Directory -Path "D:\PSU\ScriptLogs\ServerManagement\$([DateTime]::Now.ToString('yyyyMMdd'))" }
Start-Transcript -Path "D:\PSU\ScriptLogs\ServerManagement\$([DateTime]::Now.ToString('yyyyMMdd'))\$([DateTime]::Now.ToString('yyyyMMddhhmmss-fff')).Transcript.log"
Despite throwing verbose logging all over the module and the App code where it calls this module, the transcript logs never show the verbose output that is logged before the module would manually ask for credentials (specifically because they’re not pre-set). With this in mind, are there situations where the Startup Script isn’t called?
The example code where I’m using a Module function where it works and sometimes it prompts for auth (note it’s even as random as, I could type 3 letters, and it works, and type the 4th letter and it prompts for auth - but most common is refresh the page and it will work or won’t work):
New-UDAutocomplete -Id 'RequestUser' -Label 'Request User' -Value $CurrentValue -OnLoadOptions {
try {
if ($Body.Length -ge 3 -and $Body -match '^[A-Za-z0-9 ]*$') {
"MyModule1 Loaded: $(Get-Module MyModule1 | Out-String)" | Write-Verbose -Verbose
"EnvironmentMyModule1: $($Global:ExecutionEnvironment.MyModule1)" | Write-Verbose -Verbose
"Request User search string: $($Body)" | Write-Verbose -Verbose
(Get-MyModule1User -Environment $Global:ExecutionEnvironment.MyModule1 -DisplayName $Body).Value.DisplayName | ConvertTo-Json
}
} catch {}
}
Any guidance or help figuring this out or if I’m misunderstanding something in how this works would be appreciated.