Simple script is running too long using Invoke-AzVMRunCommand

1.1k Views Asked by At

I have some Azure VMs without public IPs. I need to run some custom powershell scripts on them from Azure pipelines. I cannot use predefined YAML tasks here as they work only for VMs with Public IPs.

I am using Invoke-AzVMRunCommand powershell command. Unfortunately, sometimes it runs up to 30 mins or in the worst cases until its timeout. I notice that issue from time to time randomly and I cannot see here any correlations between each case.

Invoke-AzVMRunCommand 
    -ResourceGroupName $vmResourceGroup `
    -VMName $vmName `
    -CommandId 'RunPowerShellScript' `
    -ScriptPath $scriptPath `
    -Parameter $parameters

Example simple script under specified script path:

netsh interface portproxy add v4tov4 listenport=$port connectport=$cPort connectaddress="127.0.0.1"

netsh advfirewall firewall add rule name="Test" dir=in action=allow protocol=TCP localport=$port

It usually runs up to 3 mins.

Are there any other alternatives for running scripts remotely on an Azure VM without public IP? Maybe there is a better approach for such scenarios that I am not aware of.

1

There are 1 best solutions below

1
On

It could be many things here are 3 possibilities:

  1. There is a hung process on the VM. Try rebooting.

  2. There are several pipelines starting powershell scripts that are blocking each other. Try disabling some pipelines so this does not happen.

  3. The script has problems talking to storage. Try enabling Service endpoint to Azure Storage. See: https://learn.microsoft.com/en-us/azure/virtual-network/tutorial-restrict-network-access-to-resources