Hello, I was running into this problem as well and I think I may have partially solved it by doing additional queries and "cleaning" out the "stuck" commands. Now, I run this "cleaning" cycle before I run new commands. ;-) It worked for me, so I thought I would post it here. Also, please note my question at the bottom.
First, see which VMs are currently in an "Updating" Provisioning state by running Get-AzVM. An Updating state indicates that the VM is still running (or stuck running) one or more commands which is what the problem was for me.
Get-AzVM -ResourceGroupName 'ResourceGroup' -Status | Where-Object { $_.PowerState -eq "VM Running" }
Next, optionally run Get-AzVMRunCommand against the target VM(s) that are in an Updating Provisioning state to see which commands are running/stuck:
Get-AzVMRunCommand -ResourceGroupName 'SampleResourceGroup' -VMName 'SampleVM'
Next, remove/clean all of the running/stuck commands by running Remove-AzVMRunCommand - optionally use nowait and then use Get-AzVM to see if/when the Provisioning status updates to Succeeded. This can take several minutes to run, so I recommend using the nowait parameter.
Get-AzVMRunCommand -ResourceGroupName 'SampleResourceGroup' -VMName 'SampleVM'
| ForEach-Object {Remove-AzVMRunCommand -VMName 'SampleVM' -ResourceGroupName 'SampleResourceGroup' -RunCommandName $_.Name -nowait}
Once Get-AzVM shows the VMs in a Succeeded Provisioning state, the VMs are now open for new commands. Now/Finally, try running/re-running your Get-AzVMRunCommand against the VM(s) and this time, it "should" work consistently. It certainly made my commands work more reliably.
Question: After I know that my script has completed successfully (my script provides me updates on its progress) it still takes several minutes for the Provisioning state to go from Updating (it thinks it is still running the command) to Succeeded, therefore I'm looking for advice that might help make it faster. Any help is appreciated. Thank you!