I ran into a problem where some variables, that I'm comparing against using the like operator, contain square brackets and parenthesis.

I am using the VMware PowerCLI plugin (which should be irrelevant) and my expression is as follows -

Get-VM | Where { (Get-CDDrive $_).ISOPath -like "*$ISOLocation*" }

It is failing to identify the similarity between strings when the value of $ISOLocation is [datastore1 (13)] iso/rhel6.4_kickstart.iso. I believe this is due to the special characters. How can I escape these characters before the previously mentioned command, or how can I better achieve this comparison?

有帮助吗?

解决方案

As far as bracket are concerned, you've got the Microsoft official answer in an old Technet Windows PowerShell Tip of the Week.

In you case try :

``[datastore1 (13)``] iso/rhel6.4_kickstart.iso
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top