Escaping quotes (powershell-style)

I need to search some xml files for a certain setting – but the files were remote to me but were not shared.  So I dove into invoke-command (remote powershell was enabled) but quickly figured out my desired string was being a bear to find.

The files in question started as templates and had a parameter field:

whichparm="enter chickenparam here"

that I wanted to check had been changed. Now, I’m not picky – I only wanted to know if the double-quote was still there.  Yikes.

I’ve used invoke-command quite a bit, kind of a requirement for PowerCLI for VMware View when you can only access the API (and the cmdlets) on a Connection sever.  But I’ve never tried to pass quotes before, which will be a requirement for the -pattern argument component of the select-string that will search all of my xml files.

After many experimentations, I settled on this which only escapes each of the “inner” double quotes, both of which are needed at the remote PC when the statement runs so regex will look for any character except a doublequote:

$J = invoke-command -computername redsauce -scriptblock {select-string C:\Dinnerplans\*.xml -pattern "whichparm=[^`"`"]"}

Which works, it even sets $j.matches to the string found, however it doesn’t return the whole line like select-string normally does.

This entry was posted in Computing, PowerShell, Scripting, VMware and tagged . Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.