VMware Cloud Community
AGFlora
Enthusiast
Enthusiast
Jump to solution

How to Filter out specific Operating Systems

Hi,

I'm trying to filter out specific Operating systems. This is what I have so far but it's not working:

New-VIProperty -Name ToolsStatus -ObjectType VirtualMachine -ValueFromExtensionProperty 'Guest.ToolsStatus' -Force
New-VIProperty -Name GuestOS -ObjectType VirtualMachine -ValueFromExtensionProperty 'Config.GuestFullName' -Force


Get-VM | Where-Object {$_.Config.GuestFullName -Notlike "Microsoft Windows Server*" -or $_.Name.Config.GuestFullName -Notlike "Linux*"} |
Select Name,PowerState,ToolsStatus, GuestOS

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I suspect that should be and -and instead of an -or.

I also changed the NotLike to NotMatch, avoids the asterisk, and the text can appear anywhere in the string

Try like this

New-VIProperty -Name ToolsStatus -ObjectType VirtualMachine -ValueFromExtensionProperty 'Guest.ToolsStatus' -Force

New-VIProperty -Name GuestOS -ObjectType VirtualMachine -ValueFromExtensionProperty 'Config.GuestFullName' -Force

Get-VM | Where-Object {$_.GuestOS -NotMatch "Microsoft Windows Server" -and $_.GuestOS -NotMatch "Linux"} |

Select Name,PowerState,ToolsStatus, GuestOS


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

I suspect that should be and -and instead of an -or.

I also changed the NotLike to NotMatch, avoids the asterisk, and the text can appear anywhere in the string

Try like this

New-VIProperty -Name ToolsStatus -ObjectType VirtualMachine -ValueFromExtensionProperty 'Guest.ToolsStatus' -Force

New-VIProperty -Name GuestOS -ObjectType VirtualMachine -ValueFromExtensionProperty 'Config.GuestFullName' -Force

Get-VM | Where-Object {$_.GuestOS -NotMatch "Microsoft Windows Server" -and $_.GuestOS -NotMatch "Linux"} |

Select Name,PowerState,ToolsStatus, GuestOS


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
MKguy
Virtuoso
Virtuoso
Jump to solution

What Luc said. Just adding to that, you can shorten it to a single (not)match statement by grouping the regex with alternations like this:

Get-VM | Where-Object {$_.GuestOS -NotMatch "(Microsoft Windows Server|Linux)"} |
Select Name,PowerState,ToolsStatus, GuestOS

-- http://alpacapowered.wordpress.com
Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Thanks for the additional input.

Reply
0 Kudos