我需要获取VM列表,该列表将按provisionedspace进行排序,并且这些VM不应该具有RawPhysical。我已经尝试了以下代码
Get-Datastore -Name "$DSName" | Get-VMHost | get-vm | Select-Object -Property Name, Provisionedspacegb | sort -Property Provisionedspacegb | select -First 3 | Select Name
上面的用于通过Provisionedpagegb获取VM列表排序
Get-Datastore -Name "$DSName" | Get-VMHost | Get-VM | Get-HardDisk | Where-Object {$_.DiskType -eq "RawPhysical" } | Select Parent
以上用于获取没有物理磁盘的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
实际的语法可能有点不同。