VMware Cloud Community
rsimonsca
Contributor
Contributor
Jump to solution

Get-View and -filter

I am a (relatively) recent convert to get-view due to the amazing increase in speed for running PowerCLI scripts across hundred of ESXi hosts / thousands of VMs.

I've been struggling with how to do this with a supplied list of VMs?  What I want to do is with a supplied list of VMs get the hardware version and tools version.

Normally if you wanted a report of everything you'd do the following:

get-view -viewtype VirtualMachine -property Name, Config | select Name, @{N="HWVersion";E={$_.Config.Version}},@{N="Tools";E={$_.Config.Tools.ToolsVersion}} | ft -auto

Now reading up on -filter is seems the way to go - but it only uses a hashtable.  ie "Name" = "Foo".  It doesn't seem to handle "Name"=$vmList where $vmList is a collection of mulitple VMs.  I won't go into how if the variable is a single VM that Foo matches Foo and FooBar.

What I've used is the following:

$vmList = get-content c:\scripts\import\listofvms.txt

get-vm $vmList | get-view -property Name, Config | select Name, @{N="HWVersion";E={$_.Config.Version}},@{N="Tools";E=$_.Config.Tools.ToolsVersion}} | ft -auto

The only problem is I feel like this is a workaround - that this should all be doable using get-view directly.  So - if I wanted to use -filter with a collection of VMs how would I go about doing that?

Cheers

Robert

Reply
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Robert,

if you use the Get-View cmdlet and want to search for two VM's called foo and bar you have to make a filter with a regular expression. You have to use the vertical bar as an 'or' sign in the regular expression. E.g.

Get-View -ViewType VirtualMachine -Property Name,Config -Filter @{"Name"="foo|bar"}

You can use the .NET string Join method to construct the regular expression from the $vmList variable. Like this:

$vmList = get-content c:\scripts\import\listofvms.txt
Get-View -ViewType VirtualMachine -Property Name,Config -Filter @{"Name"="$([string]::Join('|',$vmList))"} | `
Select-Object -Property Name,@{N="HWVersion";E={$_.Config.Version}},@{N="Tools";E={$_.Config.Tools.ToolsVersion}} | `
Format-Table -AutoSize

Regards, Robert

Message was edited by: RvdNieuwendijk Added the explanation at the beginning of the post.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
2 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Robert,

if you use the Get-View cmdlet and want to search for two VM's called foo and bar you have to make a filter with a regular expression. You have to use the vertical bar as an 'or' sign in the regular expression. E.g.

Get-View -ViewType VirtualMachine -Property Name,Config -Filter @{"Name"="foo|bar"}

You can use the .NET string Join method to construct the regular expression from the $vmList variable. Like this:

$vmList = get-content c:\scripts\import\listofvms.txt
Get-View -ViewType VirtualMachine -Property Name,Config -Filter @{"Name"="$([string]::Join('|',$vmList))"} | `
Select-Object -Property Name,@{N="HWVersion";E={$_.Config.Version}},@{N="Tools";E={$_.Config.Tools.ToolsVersion}} | `
Format-Table -AutoSize

Regards, Robert

Message was edited by: RvdNieuwendijk Added the explanation at the beginning of the post.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
rsimonsca
Contributor
Contributor
Jump to solution

Thanks Robert.  Used that method several times in the past.  A case of not seeing the forest thru the trees I'm afraid!

Reply
0 Kudos