这是一种获取集群中的 VM 磁盘(VHDX)列表并通过 ID 或其他一些愚蠢的方法将它们与来宾 VM 磁盘(来宾所见)进行匹配的简单方法吗?
我可以通过以下方式获取虚拟磁盘信息
$vm = get-vm MYVM
get-vhd -VMId $vm.VMId | ft VhdFormat,VhdType,FileSize,Size
我可以像这样获取磁盘空间和使用情况
get-WmiObject win32_logicaldisk -Computername MYVM | ft FreeSpace,Size
我对我们所有的虚拟机进行了查询,并根据磁盘大小对它们进行了粗略的匹配 - 但是是否有任何磁盘 GUIID 或类似的东西暴露给主机或客户机,例如通过集成服务,从而可以明确地匹配它们?
我想要的是查看客户操作系统的大小和使用情况,并将其与 VHDX 的空间使用情况和最大大小进行比较。
例如,一个最大大小为 50GB 的空驱动器在动态 VHDX 中占用了主机上的 25GB。如果客户机上安装的磁盘中有未分配的空间,它的大小甚至可以达到最大 100GB...
编辑
这永远不会简单的原因可能是因为我想匹配逻辑磁盘与“物理”(主机 VHDX)磁盘的映射,如果客户机上存在多个逻辑/分区映射到一个磁盘,那么这并不总是可行的。但挑战仍然存在……
找到一个似乎可以解决该问题的脚本,但它针对的是 VMWare,而不是 Hyper-V: https://4sysops.com/archives/map-vmware-virtual-disks-and-windows-drive-volumes-with-a-powershell-script/
答案1
你不需要匹配vhd 到 vm 查看所有成员get-vm
get-vm | get-member
TypeName: Microsoft.HyperV.PowerShell.VirtualMachine
Name MemberType Definition
---- ---------- ----------
CheckpointFileLocation AliasProperty CheckpointFileLocation = SnapshotFileLocation
ParentCheckpointId AliasProperty ParentCheckpointId = ParentSnapshotId
ParentCheckpointName AliasProperty ParentCheckpointName = ParentSnapshotName
VMId AliasProperty VMId = Id
VMName AliasProperty VMName = Name
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
AutomaticCriticalErrorAction Property Microsoft.HyperV.PowerShell.CriticalErrorAction AutomaticCriticalErrorAction {get;}
AutomaticCriticalErrorActionTimeout Property int AutomaticCriticalErrorActionTimeout {get;}
AutomaticStartAction Property Microsoft.HyperV.PowerShell.StartAction AutomaticStartAction {get;}
AutomaticStartDelay Property int AutomaticStartDelay {get;}
AutomaticStopAction Property Microsoft.HyperV.PowerShell.StopAction AutomaticStopAction {get;}
CheckpointType Property Microsoft.HyperV.PowerShell.CheckpointType CheckpointType {get;}
CimSession Property CimSession CimSession {get;}
ComPort1 Property Microsoft.HyperV.PowerShell.VMComPort ComPort1 {get;}
ComPort2 Property Microsoft.HyperV.PowerShell.VMComPort ComPort2 {get;}
ComputerName Property string ComputerName {get;}
ConfigurationLocation Property string ConfigurationLocation {get;}
CPUUsage Property int CPUUsage {get;}
CreationTime Property datetime CreationTime {get;}
DVDDrives Property Microsoft.HyperV.PowerShell.DvdDrive[] DVDDrives {get;}
DynamicMemoryEnabled Property bool DynamicMemoryEnabled {get;}
FibreChannelHostBusAdapters Property System.Collections.Generic.List[Microsoft.HyperV.PowerShell.VMFibreChannelHba] FibreChannelHostBusAdapters {get;}
FloppyDrive Property Microsoft.HyperV.PowerShell.VMFloppyDiskDrive FloppyDrive {get;}
Generation Property int Generation {get;}
Groups Property System.Collections.Generic.IReadOnlyList[Microsoft.HyperV.PowerShell.VMGroup] Groups {get;}
GuestControlledCacheTypes Property bool GuestControlledCacheTypes {get;}
HardDrives Property Microsoft.HyperV.PowerShell.HardDiskDrive[] HardDrives {get;}
Heartbeat Property System.Nullable[Microsoft.HyperV.PowerShell.VMHeartbeatStatus] Heartbeat {get;}
HighMemoryMappedIoSpace Property uint64 HighMemoryMappedIoSpace {get;}
Id Property guid Id {get;}
IntegrationServicesState Property string IntegrationServicesState {get;}
IntegrationServicesVersion Property version IntegrationServicesVersion {get;}
IsClustered Property bool IsClustered {get;}
IsDeleted Property bool IsDeleted {get;}
LockOnDisconnect Property System.Nullable[Microsoft.HyperV.PowerShell.OnOffState] LockOnDisconnect {get;}
LowMemoryMappedIoSpace Property uint32 LowMemoryMappedIoSpace {get;}
MemoryAssigned Property long MemoryAssigned {get;}
MemoryDemand Property long MemoryDemand {get;}
MemoryMaximum Property long MemoryMaximum {get;}
MemoryMinimum Property long MemoryMinimum {get;}
MemoryStartup Property long MemoryStartup {get;}
MemoryStatus Property string MemoryStatus {get;}
Name Property string Name {get;}
NetworkAdapters Property System.Collections.Generic.List[Microsoft.HyperV.PowerShell.VMNetworkAdapter] NetworkAdapters {get;}
Notes Property string Notes {get;}
NumaAligned Property System.Nullable[bool] NumaAligned {get;}
NumaNodesCount Property int NumaNodesCount {get;}
NumaSocketCount Property int NumaSocketCount {get;}
OperationalStatus Property Microsoft.HyperV.PowerShell.VMOperationalStatus[] OperationalStatus {get;}
ParentSnapshotId Property System.Nullable[guid] ParentSnapshotId {get;}
ParentSnapshotName Property string ParentSnapshotName {get;}
Path Property string Path {get;}
PrimaryOperationalStatus Property System.Nullable[Microsoft.HyperV.PowerShell.VMOperationalStatus] PrimaryOperationalStatus {get;}
PrimaryStatusDescription Property string PrimaryStatusDescription {get;}
ProcessorCount Property long ProcessorCount {get;}
RemoteFxAdapter Property Microsoft.HyperV.PowerShell.VMRemoteFx3DVideoAdapter RemoteFxAdapter {get;}
ReplicationHealth Property Microsoft.HyperV.PowerShell.VMReplicationHealthState ReplicationHealth {get;}
ReplicationMode Property Microsoft.HyperV.PowerShell.VMReplicationMode ReplicationMode {get;}
ReplicationState Property Microsoft.HyperV.PowerShell.VMReplicationState ReplicationState {get;}
ResourceMeteringEnabled Property bool ResourceMeteringEnabled {get;}
SecondaryOperationalStatus Property System.Nullable[Microsoft.HyperV.PowerShell.VMOperationalStatus] SecondaryOperationalStatus {get;}
SecondaryStatusDescription Property string SecondaryStatusDescription {get;}
SizeOfSystemFiles Property long SizeOfSystemFiles {get;}
SmartPagingFileInUse Property bool SmartPagingFileInUse {get;}
SmartPagingFilePath Property string SmartPagingFilePath {get;}
SnapshotFileLocation Property string SnapshotFileLocation {get;}
State Property Microsoft.HyperV.PowerShell.VMState State {get;}
Status Property string Status {get;}
StatusDescriptions Property string[] StatusDescriptions {get;}
Uptime Property timespan Uptime {get;}
Version Property string Version {get;}
VirtualMachineSubType Property Microsoft.HyperV.PowerShell.VirtualMachineSubType VirtualMachineSubType {get;}
VirtualMachineType Property Microsoft.HyperV.PowerShell.VirtualMachineType VirtualMachineType {get;}
VMIntegrationService Property System.Collections.Generic.List[Microsoft.HyperV.PowerShell.VMIntegrationComponent] VMIntegrationService {get;}
它非常广泛并且包括“硬盘”作为对象属性。
要查看实际操作,请尝试
$vm = get-vm -name name
您可以使用 powershells 点符号和 intellisense 来导航对象的属性,以便......
$vm.harddrives
类似地,你可以将所有虚拟机 ID 传输到 get-vhd
Get-VM –VMName * | Select-Object VMid | Get-VHD
请参阅这个帖子以获得一个好的解决方案: https://social.technet.microsoft.com/Forums/en-US/8696598e-dbf6-4a47-9873-a490ecf0f737/get-storage-drives-info-for-all-vms-in-a-given-cluster?forum=winserverhyperv
为清晰起见进行编辑 以下是所引用示例中的脚本:
$ClusterVMs = Get-ClusterGroup -Cluster *clustername* | where grouptype -eq 'virtualmachine' | Get-VM
foreach ($vm in $ClusterVMs) { Get-VHD $vm.harddrives.path | select @{N="Name";E={$VM.Name}},@{N="VHDPath";E={$VM.harddrives.path}},@{N="Capacity(GB)";E={[math]::Round($_.Size/ 1GB)}},@{N="Used Space(GB)";E={[math]::Round($_.FileSize/ 1GB)}}}
只需将上面的内容粘贴到 powershell 中即可群集名称到您的 hyper-v 集群的名称。
对于单个主机使用以下命令:
$ClusterVMs = get-vm
foreach ($vm in $ClusterVMs) { Get-VHD $vm.harddrives.path | select @{N="Name";E={$VM.Name}},@{N="VHDPath";E={$VM.harddrives.path}},@{N="Capacity(GB)";E={[math]::Round($_.Size/ 1GB)}},@{N="Used Space(GB)";E={[math]::Round($_.FileSize/ 1GB)}}}