Resource Group Name cannot be Null?

北战南征 提交于 2019-12-13 09:01:47

问题


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?


回答1:


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:

$Rg = (Get-AzVM | Where-Object {$_.Name -match "$VmCode"}).ResourceGroupName


来源:https://stackoverflow.com/questions/56479358/resource-group-name-cannot-be-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!