如何获取没有原始物理磁盘的 VM 并按名称配置的空间对这些 VM 进行排序?

How to get VM which has not Raw physical disk and sort those VM by name provisionedspace?

我需要获取按 provisionedspace 排序的 VM 列表,而且那些 VM 不应该有 RawPhysical。我试过下面的代码

 Get-Datastore -Name "$DSName" |  Get-VMHost | get-vm | Select-Object -Property  Name, Provisionedspacegb | sort -Property Provisionedspacegb | select -First 3 | Select Name

以上用于获取 VM 列表排序 provisionedspacegb

Get-Datastore -Name "$DSName" | Get-VMHost | Get-VM | Get-HardDisk | Where-Object {$_.DiskType -eq "RawPhysical" } | Select Parent

以上用于获取没有Physical Disk

的VM列表

我需要将这两个代码放在一行 powershell 代码中..

每当与大量管道操作员合作时,退后一步并考虑 divide et impera 方法。也就是说,将脚本分解为更易于管理的部分。我没有可用的 VMWare,但请尝试以下想法:

# Get a list of all the VMs
$allVms= Get-Datastore -Name "$DSName" |  Get-VMHost | get-vm

# Array for those we actually want
$rawVms = @()

# Iterate the VM collection. Look for such VMs that have whatever disk config    
foreach($vm in $allVms) {
    # This filtering statement is likely to be incorrect. Tune accordingly
    if($vm | get-harddisk | ? { $_.DiskType -eq "RawPhysical" }).Count -gt 0 {

        # If the VM has raw disk, add it into desired VMs list
        $rawVms += $vm
    }
}

# Do stuff to the filtered collection
$rawVms | Select-Object -Property  Name, Provisionedspacegb | ` # ` = line break for readability
    sort -Property Provisionedspacegb | select -First 3 | Select Name

实际语法可能有点不同。