13
我正在编写一个脚本,它将查看父VHD文件的目录,然后评估哪些VM正在使用这些父VHD。
我有它的工作机制,但我遇到了一个问题,我真的需要从嵌套管道
的情况下引用管道自动变量($ _)须藤代码会是这样的:
For each File in Files
Iterate over all VMs that have differencing disks
and return all the VMs that have a disk whose parent disk is File
下面是实际的PowerShell代码我已经实现了迄今为止做到这一点:
$NAVParentFiles = get-childitem '\\hypervc2n2\c$\ClusterStorage\Volume1\ParentVHDs' | where {$_.Name -notLike "*diff*"} | select name
$NAVParentFiles | % { Get-VM | where {$_.VirtualHardDisks | where {$_.VHDType -eq "Differencing" -and ($_.ParentDisk.Location | split-path -leaf) -like <$_ from the outer for each loop goes here> } }
感谢您的帮助,您可以给我提供如何从嵌套管道优雅地访问外部管道变量。
有时nestead'foreach'es比嵌套管道更具可读性。 – stej