首页 > 解决方案 > 变量中的 Azure Powershell VM 停止状态

问题描述

我正在尝试编写一个 PowerShell 脚本,该脚本将在我的资源组中查找关闭的虚拟机并取消分配它们。当我尝试将以下脚本分配为变量时,以下脚本的输出没有给我 VM 名称“clean”。最终结果是执行 Stop-AzureRmVM -ResourceGroupName LAB -Name $VM -force

因此,对于更多上下文,假设 AVGJOE-DC1 处于停止状态,我在 Azure Powershell 中运行以下行,它将显示

Name
----
AVGJOE-DC1

如果我尝试使用 $VM 在

Stop-AzureRmVM -ResourceGroupName LAB -Name $VM -force 

由于变量被设置为更长的字符串(例如 MicroSoftComputerResource\Resourcegroup[@Name=AVGJOE-DC1].
希望这是有道理的。

  $VM = Get-AzureRmVM -ResourceGroupName LAB | get-azurermvm -Status | ?{$_.statuses.displaystatus -eq "VM stopped"} | select name 

标签: powershellazure-functions

解决方案


就像@Theo 在评论中说的那样,select name给你一个带有 property 的对象name。如果你想要valuename 属性的字符串,你可以使用Select-Object -ExpandProperty name代替select name.

在此处输入图像描述


推荐阅读