PowerShell global variable not retrieved correctly
See original GitHub issueVariables are defined at the beginning of the PowerShell script; without the “global” keyword Assuming these variables are in global scope.
$accessTokenTest = ""`
$accessToken = ""`
Here is a function to renew the SQL access token and persist it to a JSON file.
In Get-SQLToken function, global variables were saved differently, with and without “global” keyword.
This was the workaround for the issue:
$Global:accessToken = $tokenResponse.access_token
function Get-SQLToken {
WriteToLog -message "Token Start..."
try {
$resourceURI = "https://database.windows.net/"
$tokenAuthURI = $env:MSI_ENDPOINT + "?resource=$resourceURI&api-version=2017-09-01"
$tokenResponse = Invoke-RestMethod -Method Get -Headers @{"Secret" = "$env:MSI_SECRET" } -Uri $tokenAuthURI
WriteToLog -message "Create new SQLSetting file"
$tokenResponse | ConvertTo-Json | Out-File SQLSettings.json
$Global:accessToken = $tokenResponse.access_token
$accessTokenTest = $tokenResponse.access_token
if ($null -eq $accessTokenTest || [string]::IsNullOrEmpty($accessTokenTest)) {
WriteToLog -message "[DBG] Failed to save access token value to a global variable. accessTokenTest is $($accessTokenTest)"
}
else {
WriteToLog -message "[DBG] Successfully save access token value to a global variable accessTokenTest. accessTokenTest is $($accessTokenTest)"
}
}
catch [System.Exception] {
WriteToLog -message "Error occured $($_.Exception.Message)" -Exception $_
$tokenResponse = ""
}
WriteToLog -message "Token end"
}
We retrieve the value of the global variables later on.
However, the variable $accessTokenTest was not retrieved correctly
$SQLSettings = Get-Content SQLSettings.json | ConvertFrom-Json
if ([datetime]$SQLSettings.expires_on -gt ((Get-Date).AddHours(23))) { # DBG AddMinutes(30)
$accessToken = $SQLSettings.access_token
WriteToLog -message "Token from SQLSettings"
}
else {
WriteToLog -message "Old Token need a new Token... create new SQLSettings"
Get-SQLToken
if ($null -eq $accessTokenTest || '' -eq $accessTokenTest || [string]::IsNullOrEmpty($accessTokenTest)) {
WriteToLog -message "[DBG] Failed to retrieve token value from a global variable. accessTokenTest is $($accessTokenTest)"
}
else {
WriteToLog -message "[DBG] Successfully retrieved token value from a global variable accessTokenTest. accessTokenTest is $($accessTokenTest)"
}
$accessToken = $Global:accessToken
}
In order to work around the hurdle, we applied the work around which seems redundant.
Save the value to the global variable $accessToken with global keyword
$Global:accessToken = $tokenResponse.access_token
Retrieve the value of the global variable $accessToken with global keyword
$accessToken = $Global:accessToken
From the logs,
2022-08-06T23:42:54.398 [Information] INFORMATION: [DBG] Successfully save access token value to a global variable accessTokenTest. accessTokenTest is<<value is redacted>>
no value got retrieved, or no printable value got retrieved
2022-08-06T23:42:54.399 [Information] INFORMATION: [DBG] Successfully retrieved token value from a global variable accessTokenTest. accessTokenTest is<<end of line>>
We have been running this script without work around since August 2021 under PowerShell 7 kernel until the issue started surfacing in early March this year. We would like to learn:
- What has been changed in PowerShell 7 kernel in function app?
- Is it required to declare global variables with 'global' keyword?
Issue Analytics
- State:
- Created a year ago
- Comments:7 (3 by maintainers)
Top GitHub Comments
Hi @Francisco-Gamino,
Thank you for the reply, I will create a support ticket and provide the info as requested.
For those lines of code that you pointed out, were added just for providing the Support additional info. Not in the production code.
@yihchuang – Sounds good, thank you. I will follow up with you via the support ticket. Closing issue.