VMware Cloud Community
np123
Enthusiast
Enthusiast
Jump to solution

specify vm name precisely with get-view -filter hash

I think I'm missing some quirk or convention with Powershell variables in a hash filter.

My infrastructure includes these 3 VMs:
UTIL
otherUTIL
anotherUTILserver

I'm trying to use something like the following to return only the vm called UTIL. This returns all 3 VMs with UTIL in their name:

$vm = "UTIL"

Get-View -ViewType VirtualMachine -Filter @{"Name" = $vm} | select -expand name

The get-view line below eliminates anotherUTILserver but still returns otherUTIL along with UTIL:

Get-View -ViewType VirtualMachine -Filter @{"Name" = "$vm$"} | select -expand name

With "$vm$" I'm following LucD's comment here: http://rvdnieuwendijk.com/2011/09/12/how-to-use-vmware-vsphere-powercli-to-find-the-mac-addresses-of...

If necessary, I could resort to the slower-but-simpler get-vm, could add a foreach loop (my example is simplified), or could maybe filter on an additional property besides Name. But I'm hoping there's a similar trick that'll help the filter eliminate otherUTIL and return only UTIL, without adding complexity.

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
np123
Enthusiast
Enthusiast
Jump to solution

Regex to the rescue: http://msdn.microsoft.com/library/az24scfc.aspx

Get-View -ViewType VirtualMachine -Filter @{"Name" = "^$vm$"} | select -expand name

Or without regex:

$x = Get-View -ViewType VirtualMachine -Filter @{"Name" = $vm}

$x | where {$_.name -eq $vm} | select -expand Name

View solution in original post

0 Kudos
2 Replies
np123
Enthusiast
Enthusiast
Jump to solution

...getting warmer http://wannemacher.us/?p=259

0 Kudos
np123
Enthusiast
Enthusiast
Jump to solution

Regex to the rescue: http://msdn.microsoft.com/library/az24scfc.aspx

Get-View -ViewType VirtualMachine -Filter @{"Name" = "^$vm$"} | select -expand name

Or without regex:

$x = Get-View -ViewType VirtualMachine -Filter @{"Name" = $vm}

$x | where {$_.name -eq $vm} | select -expand Name

0 Kudos