Az Powershell Module commands hang

66 views Asked by At

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.

1

There are 1 answers

1
Vinay B On

Azure PowerShell Commands Hanging Issue

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

  • Test the problematic Azure command in a separate, minimal PowerShell script without GUI components. This can help determine if the issue is specific to the command itself or its interaction with the GUI.
  • Implement robust logging and error handling around the Azure command to capture any exceptions or errors that might not be displayed in the console.

ref: https://github.com/Azure/azure-powershell/issues/17505

PowerShell Environment Compatibility

  • When integrating GUI elements, consider utilizing runspace or PowerShell jobs to execute asynchronous operations without blocking the UI thread.
  • Even though you've expressed concerns about using PowerShell 7 because of GUI compatibility issues, it might be beneficial to test the command in PowerShell 7 within a non-GUI environment. This approach could help determine whether the problem is specific to the version or related to the GUI framework compatibility.

ref: https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.runspace?view=powershellsdk-7.4.0

Alternative Execution Strategies

  • You might consider utilizing Invoke-Command to execute the Azure command within a new PowerShell process or session, which may help circumvent issues related to the environment.
  • Since running as a Job has been problematic, explore PowerShell Runspaces as an alternative for executing tasks asynchronously without blocking the main thread, especially important in GUI applications

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 -AsJob Parameter

Since you mentioned that using PowerShell jobs didn't help as the console becomes unresponsive, consider using the -AsJob parameter with the Get-AzVM cmdlet to run the command as a background job. This might behave differently than manually creating a job and could offer better responsiveness.

`$azurevmdetailsJob = Get-AzVM -Name $serverName -AsJob
$azurevmdetails = Receive-Job -Job $azurevmdetailsJob -Wait`

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.