VMware Cloud Community
ImaRelic
Contributor
Contributor
Jump to solution

Get-VM with / in name

I have this line of code-

$T = Get-VM $M.Name | Get-Harddisk | Select StorageFormat

It works fine unless there is a / character in the name. (in $M.Name), in which case it errors and fails.  Is there a simple way around this?

Thanks for any suggestion

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

In that case, go for the Replace method and only replace the slash.

Like this

$name = 'Te st/123'

Get-VM -Name $name.Replace('/','%2F')


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

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Use the URL decoding functionality

Something like this

$name = 'Test/123'

Get-VM -Name ([uri]::EscapeDataString($name))


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

Reply
0 Kudos
ImaRelic
Contributor
Contributor
Jump to solution

This seems to expand spaces to %20 so it doesn't find the names.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

In that case, go for the Replace method and only replace the slash.

Like this

$name = 'Te st/123'

Get-VM -Name $name.Replace('/','%2F')


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

Reply
0 Kudos
ImaRelic
Contributor
Contributor
Jump to solution

That worked perfectly. Thank you very much for the help.

Randy

Reply
0 Kudos