I am currently writing a powershell script with XAML GUI, where I can get Azure Virtual Desktops details with a button click. The script worked fine until I added the Set-AzContext cmdlet.
This cmdlet is super important since I need to be able to target machines on different resource groups, hosted on different subscriptions (eg : EMEA, SEAS, AMER…). So I created a function that, based on the machine hostname, will determine its resource group, hostpool, and the associated subscription ID.
function Get-AVDIdentity {
param(
[String]$ComputerName
)
switch -wildcard ($ComputerName){
"vmavd-fc*" { $result_ResourceGroupName = "rg-france-00001" ; $result_HostpoolName = "vdpool-france-00001" ; $result_SubscriptionID = "11111111-1111-1111-1111-111111111111" }
"vmavd-bs*" { $result_ResourceGroupName = "rg-brasil-00001" ; $result_HostpoolName = "vdpool-brasil-00001" ; $result_SubscriptionID = "22222222-2222-2222-2222-222222222222" }
default { $result_ResourceGroupName, $result_HostpoolName, $result_SubscriptionID = $null }
}
try {
$tenantID = "00000000-0000-0000-0000-000000000000"
write-host "Connecting to tenant: $tenantID, subscription : $result_SubscriptionID"
Set-AzContext -Tenant $tenantID -Subscription $result_SubscriptionID -scope Process -Force -errorAction Inquire | Out-Null
write-host "Connected"
} catch {
Throw
}
return $result_ResourceGroupName, $result_HostpoolName, $result_SubscriptionID
}
This function works fine without the Set-AzContext line ; if I execute the function…
Get-AVDIdentity -Computername "vmavd-fc-710"
… it successfully returns the data I want, since my default subscription is the French one.
If I add the Set-AzContext line, it works for a first time, but if I re-click the button, it just loops infinitely.
Here’s a description of what’s happening, with the -Confirm parameter :
The “Connected” line is just a Write-Host I added for debugging.
When “O” is entered, the GUI and the prompt freezes.
This behavior is really weird. It only happens in a WPF context ; I tested this function in Powershell ISE, it always executes immediatly.
Any idea about why it freezes ? (Can’t really post the whole script, it’s super long and split in two .ps1 files)
1