abszero abszero - 1 year ago 80
PowerShell Question

Powershell 2.0 stripping double quotes

Similar questions have been asked on SO, however this is slightly different from those questions.

I am working on a Powershell script that dynamically builds a command and attempts to execute it against TFS. This command requires quotes for some of the arguments, and no matter what I try I can't seem to figure out how to inject them. I have this code snippet:

$files = Get-ChildItem $pathToDefinitions -Filter *.xml
Foreach ($file in $files){
$fileName = $file.fullName
$cmd = "importwitd /collection: $tfsProjectCollectionUrl /p:`"$projectName`" /f:`"$fileName`""
Write-Host $cmd
#iex "`"" + $($cmd) +"`""
& $witadmin "importwitd /collection: $tfsProjectCollectionUrl /p: "\`" $projectName \`"" /f: "\`"$fileName \`"""

This is my most recent attempt at escaping the quotes but to no avail. On the accepted answer for this question there is a link to Microsoft but that link appears to be broken. Additionally many of the examples that I have come across use constants rather than a dynamic string which is apparently adding a level of complexity. Is there any way to pass quotes into this command call in powershell?

Answer Source

Without really knowing anything about the command you're running I'd have thought it would accept this:

$argumentList = @(
& $witadmin $argumentList

Where " can be used to escape " giving you a literal quote in your argument string.