I’m running couple of PowerShell scripts as part of our DevOps pipelines on Windows Azure VMs with az vm run-command create ..
tool. Sometimes the commands freeze and won’t complete in decent time. When this happens I’m not able to execute those commands anymore and it starts to block the devops pipeline runs. …and also if I try to delete the run-command with az vm run-command delete ..
it also freezes.
Example of run-command execution (and delete) as part of devops pipeline:
# Execute the script on VM with run-command
az vm run-command create
--name RecreateBinariesFolder$(Build.BuildId)
--vm-name ${{ parameters.vmName }}
--resource-group my-group-${{ parameters.environment }}
--timeout-in-seconds 600
--script "if(Test-Path C:\temp\packages\){ Remove-Item -Path c:\temp\packages -Force -Recurse } ; if(!(Test-Path C:\temp\packages\)){ mkdir c:\temp\packages }"
# Delete the run-command from VM
az vm run-command delete
--name RecreateBinariesFolder$(Build.BuildId)
--vm-name ${{ parameters.vmName }}
--resource-group my-group-${{ parameters.environment }}
--yes
It works well usually on fresh VM but then executions start to freeze after awhile in later pipeline runs.
Any way to execute run-commands in more stabile way or are there any other handy ways to run commands on Windows VMs more easily and in more stabile manner?
Update 2023-01-23: After re-creating the VMs and starting to run the delete
command with --no-wait
option we haven’t noticed similar freezing anymore after tens of pipeline runs. There was most likely some unexpected issue with the VM itself or the CustomScriptExtension that runs the commands on the VM.
2
Answers
If your requirement is to run a powershell script, why simply don’t you use the default command
RunPowerShellScript
as described in the third example ofaz vm run-command invoke
documentation?The
RunPowershellScript
is part of available commands for Windows VMs as described in Run scripts in your Windows VM by using action Run Commands.This could be the command in your case:
In this way you doesn’t need to create a new command type and remove it.
In addition, in your code you create a new command but never invoke it. Did you miss the code to invoke it?
It seems that the maximum number of commands that could be created in a vm is 25 (see docs, so if you try to create more commands probably the system doesn’t work.
Solution: After re-creating the VMs and starting to run the delete command with
--no-wait
option, they haven’t noticed similar freezing anymore.If you have the same issue, you can try the followings to narrow down the issue.