简体   繁体   English

powercli Get-VM 不匹配标签

[英]powercli Get-VM NOT matching Tag

I am using Tags to filter VM / identify special configuration.我正在使用标签来过滤虚拟机/识别特殊配置。

Get-VM -Tag TEST

Now I want to get all VMs NOT matching a tag .... Is there a way to do that without an extra loop to remove all items matching the Tag ?现在我想让所有不匹配标签的虚拟机......有没有办法在没有额外循环的情况下删除与标签匹配的所有项目?

As it seems, no.看起来,没有。 But using another loop is not a bad solution.但是使用另一个循环并不是一个糟糕的解决方案。

Should be something like:应该是这样的:

Get-VM -Name * | ?{$_.Tag -ne "Test"}

Or if VirtualMachine doesn't have a Tag property, you should try with Get-View :或者,如果VirtualMachine没有Tag属性,您应该尝试使用Get-View

Get-View -Name * | ?{$_ .Tag -ne "Test"} | Get-VM

I was looking at this, and I used the -contains / -notcontains comparison operator instead of -eq / -ne , as it always returns the a boolean expression.我看着这一点,我用了-contains / -notcontains比较操盘-eq / -ne ,因为它总是返回一个布尔表达式。 If you have more than one tag, it might not work properly.如果您有多个标签,它可能无法正常工作。

$VMs = get-vm 

foreach ($VM in $VMs){
    If (((Get-Tagassignment $VM).Tag.Name -notcontains "Prod"){
         Whatever you want if it doesn't contain prod.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM