Skip to content

Commit 83c8f09

Browse files
committed
[Identity] Update AzurePowerShellCredential script
- Only add the `-AsSecureString` argument when available and needed. This argument is no longer needed for `Az.Accounts` versions 5.0.0 and above. - Update secure string parsing logic to allow it to work if a user is using Windows PowerShell instead of PowerShell 7+. Signed-off-by: Paul Van Eck <[email protected]>
1 parent 05b716d commit 83c8f09

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

sdk/identity/azure-identity/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bugs Fixed
1010

11+
- Fixed an issue with `AzurePowerShellCredential` not working correctly for users still using older versions of PowerShell (e.g., Windows PowerShell 5.1) where `-AsPlainText` is not supported in the `ConvertFrom-SecureString` cmdlet. ([#41675](https://github.com/Azure/azure-sdk-for-python/pull/41675))
12+
1113
### Other Changes
1214

1315
## 1.23.0 (2025-05-13)

sdk/identity/azure-identity/azure/identity/_credentials/azure_powershell.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,25 @@
4141
$params['TenantId'] = $tenantId
4242
}}
4343
44-
$useSecureString = $m.Version -ge [version]'2.17.0'
45-
if ($useSecureString) {{
44+
if ($m.Version -ge [version]'2.17.0' -and $m.Version -lt [version]'5.0.0') {{
4645
$params['AsSecureString'] = $true
4746
}}
4847
4948
$token = Get-AzAccessToken @params
5049
$tokenValue = $token.Token
51-
if ($useSecureString) {{
52-
$tokenValue = $tokenValue | ConvertFrom-SecureString -AsPlainText
50+
if ($tokenValue -is [System.Security.SecureString]) {{
51+
if ($PSVersionTable.PSVersion.Major -lt 7) {{
52+
try {{
53+
$ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($tokenValue)
54+
$tokenValue = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr)
55+
}}
56+
finally {{
57+
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
58+
}}
59+
}}
60+
else {{
61+
$tokenValue = $tokenValue | ConvertFrom-SecureString -AsPlainText
62+
}}
5363
}}
5464
Write-Output "`nazsdk%$($tokenValue)%$($token.ExpiresOn.ToUnixTimeSeconds())`n"
5565
"""

0 commit comments

Comments
 (0)