资源组名称不能为Null? [英] Resource Group Name cannot be Null?

查看:147
本文介绍了资源组名称不能为Null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,该脚本可以将400 vms的非托管磁盘转换为托管磁盘.我们的azure基础架构的构建方式使命名约定几乎与虚拟机的名称匹配,或者几乎相同,例如,如果我有一个名为E1PrGrp19VFe01的虚拟机,它位于E1PrGrp19Rg资源组中,因此我在下面的语句中使用将RG的名称存储在如下所示的变量中:

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)

问题是当我尝试执行$ Rg时我什么都没得到,在此之后,当我运行for循环以停止RG中的所有虚拟机时,出现以下错误:

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.

我建议您使用以下方式,它从VM的信息中获取组名:

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

这篇关于资源组名称不能为Null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆