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
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.
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.
Thanks Robert. Used that method several times in the past. A case of not seeing the forest thru the trees I'm afraid!