I am running into an issue where running various azure module commands will hang. This appears to only really happen when launching outside of ISE.
$azurevmdetails = Get-AzVM -Name $serverName
When ran in ISE the results return fine each time. When executed in normal Powershell 5.1 window inline with my script that contains GUI/Forms it will just hang as soon as it gets to this part with no errors indicating in the console. This happens about 90% of the time.
I have tried an alternate approach below which has the same result.
$command = "Get-AzVM -Name $serverName"
$azurevmdetails = Invoke-Expression $command
I have also tried running as a Job but the problem is the whole powershell console seems to go unresponsive and is not able to recover.
I have also tried updating modules to the latest version. Trying to avoid Powershell 7 as it does not work out of the box with the GUI and the main script this ties into is thousands of lines of code.
Azure PowerShell commands may hang, particularly when executed outside the PowerShell ISE or within a PowerShell script containing GUI elements. This issue could stem from various factors such as thread blocking, module compatibility issues, or the distinct ways in which ISE manages sessions compared to the standard PowerShell console.
Here I will share some trouble shoot the issues which need to be followed under these cases.
Debugging and Isolation
ref: https://github.com/Azure/azure-powershell/issues/17505
PowerShell Environment Compatibility
ref: https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.runspace?view=powershellsdk-7.4.0
Alternative Execution Strategies
Invoke-Commandto execute the Azure command within a new PowerShell process or session, which may help circumvent issues related to the environment.ref: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-7.4
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_jobs?view=powershell-7.4
Use
-AsJobParameterSince you mentioned that using PowerShell jobs didn't help as the console becomes unresponsive, consider using the
-AsJobparameter with theGet-AzVMcmdlet to run the command as a background job. This might behave differently than manually creating a job and could offer better responsiveness.Debugging such issues can be challenging, especially when the script involves complex interactions between GUI elements and asynchronous operations. Incrementally applying the above suggestions could help isolate and hopefully resolve the problem
Link for trouble shooting issues.