I have a script that suppose to convert 400 vms unmanaged disks into managed disks. Our azure infrastructure is built in such a way that naming conventions almost matches the names of the virutal machine or same how are likely the same, for example if I have a vm named E1PrGrp19VFe01 it resides in E1PrGrp19Rg resource group hence I am using below statement to store the name of the RG inside a variable like below:
$VmCode = Read-Host "Partner/VM Code" (Will give a name of the VM)
$Rg = Get-AzureRmResourceGroup | Where-Object {$_.ResourceGroupName -like "*$VmCode*"} (this will store the name of the resource group)
Problem is when I try to execute $Rg I didnt get anything, after this when I run the for loop to stop all the vms in an RG I get the below error:
Stop-AzVM : Cannot validate argument on parameter 'ResourceGroupName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:1 char:35
+ Stop-AzureRmVM -ResourceGroupName $Rg.ResourceGroupName -Name $Vm.Nam ...
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Stop-AzVM], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Compute.StopAzureVMCommand
I am not sure what am I doing wrong can anyone help me out with this?
For you, the error shows the message clearly that the resource group is empty. And it's a bad way to get the resource group which the VM in. If the group name is completely different from the VM name, then you cannot get the group name.
I will suggest you use the way below, it gets the group name from the information of the VM: