VMware Cloud Community
aleksvasiukov
Contributor
Contributor

Get-View with Name filter doesn't work with vCD VMs

Hello,

I faced with the following problem some days ago: unable to get VM view by executing "get-view" cmdlet with filter containig "Name" property. This issue occurs only with VMs that managed by vCloud Director.

Everyone knows that vCD gives the name to VMs in vCenter like this: "VMName (<vCD_ID>)" - where <vCD_ID> is an identifier that can be expressed as this regex:"(\w|\d){8}[-]+((\w|\d){4}[-]+){3}(\w|\d){12}".

I tried to execute something like this: "Get-View -ViewType VirtualMachine -Filter @{"Name" = "<VMName> (<vCD_ID>)"}"

And like this: "Get-View -ViewType VirtualMachine -Filter @{"Name" = "^$($<VMName> (<vCD_ID>))$"}"

Cmdlet returns an empty response.

To my surprise if i try to execute: "Get-View -ViewType VirtualMachine -Filter @{"Name" = "<VMName>"}" - it responed VM view.

The problem is that <VMName> without identifier is not a unique name of vCD VM. So powercli can respond an array of VMS View objects.

Anyone else seeing similar issue?

0 Kudos
4 Replies
LucD
Leadership
Leadership

Just to make sure, you did it like this ?

$VMName = "<VM Name>"

$vCDID = "<vCD ID>"

Get-View -ViewType VirtualMachine -Filter @{"Name" = "^$($VMName)$($vCDID)$"}


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

0 Kudos
RvdNieuwendijk
Leadership
Leadership

The value of the Get-View -Filter parameter is a regular expression. In a regular expression, the opening parenthesis (, and the closing parenthesis ) have special meanings. So you have to escape them using the backslash character \. The following command will work:

Get-View -ViewType VirtualMachine -Filter @{"Name" = "<VMName> \(<vCD_ID>\)"}

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

RvdNieuwendijkRvdNieuwendijk,

Thanks a lot! This form work correct.

0 Kudos
AdamRushUK
Enthusiast
Enthusiast

I think you can also use this:

[uri]::EscapeDataString($VMName)

VCP-Cloud | VCP5-DCV | MCITP:EA | MCSE | CCNA | CCAA LinkedIn: https://www.linkedin.com/in/adamrushuk | Twitter : @adamrushuk
0 Kudos