Reputation: 528
I'm trying to delete virtual machine and it's associated resources in Azure using azcli from a shell script. I've succeeded in deleting the machine, then deleted its disks, and then facing issues deleting the network interface (NIC), the Public IP of the machine and its network security group (NSG) from the script. When I run the azcli commands that delete the NIC, the public IP and the NSG from a cmd, manually, one by one - it works and deletes them. However, the same commands not work when running inside a shell script.
I'm attaching the script with all the commands:
#!/bin/bash
AZURE_RESOURCE_GROUP="myRG"
VM_NAME="myVM"
echo "Gathering network interface connector information"
az vm nic list --vm-name "$VM_NAME" --resource-group $AZURE_RESOURCE_GROUP
echo $'\nStopping $VM_NAME virtual machine, it may take up to 90 seconds'
az vm deallocate -n "$VM_NAME" -g $AZURE_RESOURCE_GROUP --no-wait
sleep 90
echo "Deleting $VM_NAME virtual machine, it may take up to 3 minutes"
az vm delete -n "$VM_NAME" -g $AZURE_RESOURCE_GROUP --yes --no-wait
sleep 180
echo "Deleting data disks, it may take up to 30 seconds"
az disk delete --name "$VM_NAME-disk01" --resource-group $AZURE_RESOURCE_GROUP --yes --no-wait
sleep 30
echo "Dissociating public IP, it may take up to 30 seconds"
# having problem with that command inside the script
az network nic ip-config update --resource-group $AZURE_RESOURCE_GROUP --name "$VM_NAMEPublicIP" --nic-name "$VM_NAMEVMNic" --remove PublicIpAddress
sleep 30
echo "Deleting network interface, it may take up to 45 seconds"
# having problem with that command inside the script
az network nic delete -g $AZURE_RESOURCE_GROUP -n "$VM_NAMEVMNic"
sleep 45
echo "Deleting public IP, it may take up to 30 seconds"
# having problem with that command inside the script
az network public-ip delete -g $AZURE_RESOURCE_GROUP -n "$VM_NAMEPublicIP"
sleep 30
echo "Deleting network security group, it may take up to 30 seconds"
# having problem with that command inside the script
az network nsg delete -g $AZURE_RESOURCE_GROUP -n "$VM_NAMENSG"
echo $'\nResources of $VM_NAME deleted successfully'
when running the above script, my VM and its disks successfully deleted, but the last commands output the above error:
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
Can you help me solve that problem? Thanks :)
Upvotes: 0
Views: 334
Reputation: 528
Solved! I had problems with my 3 variables "$VM_NAMEPublicIP", "$VM_NAMEVMNic", "$VM_NAMENSG" - so I created 3 variables from the above way: NIC=$VM_NAME$VMNic, etc. It worked!
Upvotes: 1