VMware Cloud Community
mhorgan61
Contributor
Contributor

Get-CIVM/Search-Cloud Regex Escape

Hi All,

I am trying to use Get-CIVM with the ID parameter but I've noticed that with certain special characters e.g. square brackets, it returns an error (I've blanked out the full VM name but you should be able to see what I mean):

pastedImage_6.png

So from this, it looks like Get-CIVM actually translates the ID back into the name and queries the API but obviously square brackets would need to be escaped in that URL. Can anybody think of a way around this? I wanted to use the ID parameter because we have 15k+ VMs in our environment and the ID seems the quickest way to get a CIVM object back.

Cheers,

Matt

Reply
0 Kudos
9 Replies
LucD
Leadership
Leadership

I assume that $ReturnedVMs contains multiple entries?

In that case the Get-CIVM cmdlet states "Note: When a list of values is specified for the Id parameter, the returned objects would have an ID that matches exactly one of the string values in that list."

It seems that the RegEx match is caused by this.

You can escape a RegEx expression with [RegEx]::Escape($str), but that only accepts one parameter.

$ids = $ReturnedVMs.Id | %{[RegEx]::Escape($_)}

Get-CIVM -Id $ids


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

Reply
0 Kudos
mhorgan61
Contributor
Contributor

Hi LucD, thanks for the reply.

Here is a snippet of the script for more context:

# If we have duplicate named VMs, we need to narrow down the choice here

if($ReturnedVMs | Group-Object Name | Where {$_.Count -ge 2})

{

    $ReturnedVMs = $ReturnedVMs |

        Select-Object @{label="Org";e={$ReturnedOrg.Name}},

            @{label="Vdc";e={(Search-Cloud -QueryType AdminOrgVdc -Filter "id==$($ReturnedVM.Vdc)").Name}},

            @{label="vApp";e={$_.ContainerName}},

            @{label="VM";e={$_.Name}},

            @{label="Id";e={$_.Id}} | 

                Out-GridView -PassThru -Title "Duplicate VMs found: Please select ALL VMs required"

   

    Get-CIVM -Id $ReturnedVMs.Id

   

} else {Get-CIVM -Id $ReturnedVMs.Id}

You're correct in that $ReturnedVMs can have more than one entry in it however my issue is that even with a single urn, the Get-CIVM cmdlet seems to take that urn and do some sort of API call on the actual name of the VM (which of course has special characters in) causing it to error. I'm just wondering if there's a better way to quickly retrieve the CIVM object for a specific selected VM?

Hopefully that makes sense Smiley Happy

Cheers,

Matt

Reply
0 Kudos
LucD
Leadership
Leadership

Out of curiosity, does

Get-CIView -Id $ReturnedVMs.Id

return the same type of errors?


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

Reply
0 Kudos
mhorgan61
Contributor
Contributor

Using Get-CIView -Id $ReturnedVMs.Id does work... I know you can convert a CIVM object into a view but can you do something to turn a view into a CIVM object?

Cheers,

Matt

Reply
0 Kudos
LucD
Leadership
Leadership

Not that I know of I'm afraid.

But I wanted to check if the Get-CIView faced the same issues with the HRef in the IDs as the Get-CIVM.

So it doesn't, looks like this might be a Get-CIVM -Id "feature".
I suggest you open a SR for this (PowerCLI is supported).


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

Reply
0 Kudos
mhorgan61
Contributor
Contributor

Thanks LucD, I'll raise an SR for it. For now, I've just got around it by replacing the offending characters with a wildcard... not ideal but not many people decide to have square brackets in their VM names thankfully!

Cheers,

Matt

Reply
0 Kudos
mhorgan61
Contributor
Contributor

So it turns out that this appears to be a bug within the PowerShell ISE... running this from PowerCLI or regular PowerShell returns the result as expected.


What I don't understand is why this would happen? Annoyingly I use the ISE for creating all my scripts/debugging etc. so may have to rethink that.

Cheers,

Matt

Reply
0 Kudos
LucD
Leadership
Leadership

I'm curious to know if the Visual Studio Code editor with the PowerShell extension also has this issue?


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

Reply
0 Kudos
mhorgan61
Contributor
Contributor

Hi LucD,

Looks like Visual Studio Code works fine so seems like a bug within the ISE. I'm interested to know what within the ISE causes it to error like that but I'll probably have to start using VS Code from now on.

Cheers,

Matt

Reply
0 Kudos