VMware Cloud Community
kakekmuda
Enthusiast
Enthusiast
Jump to solution

how to filter according to the sentence for example filtering xyz does not appear

Dear All

please help how to filter certain vm names that are not taken? example I don't want to take vm with the name xyz .

vm name
abc
xyz
def-xyz
xyz-abc

thx u so much

$dcName = 'KP1 Menara BCA'

$vms = Get-Datacenter -Name $dcName | Get-VM

$maxIP = ($vms | % { ($_.Extensiondata.Guest.Net.IPAddress | Where { $_ -and ($_.Split(".")).length -eq 4 } | Sort-Object -Unique).Count } | Measure-Object -Maximum).Maximum

$maxGateway = ($vms | % { ($_.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute.Gateway.IpAddress | Where { $_ -and ($_.Split(".")).length -eq 4 } | Sort-Object -Unique).Count } | Measure-Object -Maximum).Maximum

$maxMask = ($vms | % { ($_.ExtensionData.Guest.Net.IpConfig.IpAddress.PrefixLength | where { $_ -le 32 } | Sort-Object -Unique).Count } | Measure-Object -Maximum).Maximum

$vms | ForEach-Object -Process {

   $vm = $_

   $vm.ExtensionData.Guest.Net |

   ForEach-Object -Process {

   $obj = [ordered]@{

   VM = $vm.Name.split('(')[0].TrimEnd(' ')

   State = $vm.PowerState

   'PIC Aplikasi' = $vm.Name.split('()')[1]

   OS = $vm.Guest.OSFullName

   NumCPU = $vm.NumCPU

   MemoryMB = $vm.MemoryMB

   }

   1..$maxIP | % { $obj.Add("IP$($_)", '') }

   1..$maxGateway | % { $obj.Add("GW$($_)", '') }

   1..$maxMask | % { $obj.Add("Mask$($_)", '') }

   $i = 1

   $vm.Extensiondata.Guest.Net.IPAddress | Where { $_ -and ($_.Split(".")).length -eq 4 } |

   Sort-Object -Unique |

   ForEach-Object -Process {

   $obj.Item("IP$($i)") = $_

   $i++

   }

   $i = 1

   $vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute.Gateway.IpAddress | Where { $_ -and ($_.Split(".")).length -eq 4 } |

   Sort-Object -Unique |

   ForEach-Object -Process {

   $obj.Item("GW$($i)") = $_

   $i++

   }

   $j = 1

   $vm.ExtensionData.Guest.Net.IpConfig.IpAddress.PrefixLength | where { $_ -le 32 } |

   Sort-Object -Unique |

   ForEach-Object -Process {

   $dec = [Convert]::ToUInt32($(('1' * $_).PadRight(32, '0')), 2)

   $DottedIP = $( For ($i = 3; $i -gt -1; $i--)

   {

   $Remainder = $dec % [Math]::Pow(256, $i)

   ($dec - $Remainder) / [Math]::Pow(256, $i)

   $dec = $Remainder

   })

   $obj.Item("Mask$($j)") = [String]::Join('.', $DottedIP)

   $j++

   }

   New-Object PSObject -Property $obj

   }

}

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The -match and -notmatch operators expect a RegEx expression.
So you can use the RegEx 'or' symbol (|).

$vms | where{$_.Name -notmatch "xyz|abc"}


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

View solution in original post

3 Replies
LucD
Leadership
Leadership
Jump to solution

You could use the -notmatch operator in a where-clause.

$vms | where{$_.Name -notmatch 'xyz'}


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

kakekmuda
Enthusiast
Enthusiast
Jump to solution

Dear LucD

how about more than one

i try like this $vms | where{$_.Name -notmatch 'xyz'.'abc'} its no impact

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The -match and -notmatch operators expect a RegEx expression.
So you can use the RegEx 'or' symbol (|).

$vms | where{$_.Name -notmatch "xyz|abc"}


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