首页 > 解决方案 > Azure/PowerShell:如何查询磁盘状态?(Azure 托管磁盘)

问题描述

在 Azure 门户中,在磁盘(托管磁盘)边栏选项卡下,有一个名为“磁盘状态”的列标题

这显示为已附加或未附加

我想运行一个脚本来显示我订阅中的所有未附加磁盘,以便我可以删除它们。

运行 cmdlet

get-azurermdisk

这给了我所有的磁盘,但是如何获得显示磁盘状态的参数?

Get-member显示一堆属性,但不显示磁盘状态。

TypeName: Microsoft.Azure.Commands.Compute.Automation.Models.PSDiskList
Name               MemberType Definition
----               ---------- ----------
Equals             Method     bool Equals(System.Object obj)
GetHashCode        Method     int GetHashCode()
GetType            Method     type GetType()
ToPSDisk           Method     Microsoft.Azure.Commands.Compute.Automation.Models.PSDisk ToPSDisk()
ToString           Method     string ToString()
Validate           Method     void Validate()
CreationData       Property   Microsoft.Azure.Management.Compute.Models.CreationData CreationData {get;set;}
DiskSizeGB         Property   System.Nullable[int] DiskSizeGB {get;set;}
EncryptionSettings Property   Microsoft.Azure.Management.Compute.Models.EncryptionSettings EncryptionSettings {get;set;}
Id                 Property   string Id {get;}
Location           Property   string Location {get;set;}
ManagedBy          Property   string ManagedBy {get;}
Name               Property   string Name {get;}
OsType             Property   System.Nullable[Microsoft.Azure.Management.Compute.Models.OperatingSystemTypes] OsType {get;set;}
ProvisioningState  Property   string ProvisioningState {get;}
ResourceGroupName  Property   string ResourceGroupName {get;}
Sku                Property   Microsoft.Azure.Management.Compute.Models.DiskSku Sku {get;set;}
Tags               Property   System.Collections.Generic.IDictionary[string,string] Tags {get;set;}
TimeCreated        Property   System.Nullable[datetime] TimeCreated {get;}
Type               Property   string Type {get;}
Zones              Property   System.Collections.Generic.IList[string] Zones {get;set;}

标签: azurepowershell

解决方案


根据https://docs.microsoft.com/en-us/azure/virtual-machines/windows/find-unattached-disks上的文档,未附加磁盘的 Get-AzureRmDisk 的 ManagedBy 属性设置为 $null

这意味着

Get-AzureRmDisk | Where-Object ManagedBy -ne $null

应该显示所有附加的磁盘和

Get-AzureRmDisk | Where-Object ManagedBy -eq $null

应该显示所有未连接的磁盘


推荐阅读