3

The following code will output the command string I wish to run:

[string] $SourceRepo="C:\inetpub\wwwroot\Spyda\"
[string] $Repo="C:\inetpub\wwwroot\BranchClone\"
[string] $revstring="--rev `"default`" --rev `"case 1234`""

Write-Output "hg clone $SourceRepo $Repo $revstring"

Which gives

hg clone C:\inetpub\wwwroot\Spyda\ C:\inetpub\wwwroot\BranchClone\ --rev "default" --rev "case 1234"

If I run that from a powershell prompt, it works, if I try to run the hg clone command from a script using this syntax, it fails:

hg clone $SourceRepo $Repo $revstring

Error given:

hg.exe : hg clone: option --rev default --rev case not recognized
At line:6 char:3
+ hg <<<<  clone $SourceRepo $Repo $revstring
    + CategoryInfo          : NotSpecified: (hg clone: optio... not recognized:String) [], RemoteE 
   xception
    + FullyQualifiedErrorId : NativeCommandError

3 Answers 3

3

Try Invoke-Expression

$SourceRepo="C:\inetpub\wwwroot\Spyda\"
$Repo="C:\inetpub\wwwroot\BranchClone\"
$revstring="--rev `"default`" --rev `"case 1234`""

$cmdString = "hg clone $SourceRepo $Repo $revstring"

Invoke-Expression $cmdString
Sign up to request clarification or add additional context in comments.

Comments

2

Use the call operator (&) this way:

 & '.\hg' clone $SourceRepo $Repo $revstring

Comments

0

Using EchoArgs.exe from the PowerShell Community Extensions, we can see what arguments hg.exe is receiving:

PS> & ./EchoArgs.exe clone $SourceRepo $Repo $revstring
Arg 0 is <clone>
Arg 1 is <C:\inetpub\wwwroot\Spyda\>
Arg 2 is <C:\inetpub\wwwroot\BranchClone\>
Arg 3 is <--rev default --rev case>
Arg 4 is <1234>

What happens is that powershell resolves the call to a native application, so it automatically uses quotes to escape variable arguments containing spaces, such as $revstring.

Instead of pre-quoting our arguments, we can take advantage of this escaping by simply building up an array of the distinct values we want to use:

PS> $hgArgs = @('clone',$SourceRepo,$Repo,'--rev','default','--rev','case 1234')
PS> & ./EchoArgs.exe $hgArgs
Arg 0 is <clone>
Arg 1 is <C:\inetpub\wwwroot\Spyda\>
Arg 2 is <C:\inetpub\wwwroot\BranchClone\>
Arg 3 is <--rev>
Arg 4 is <default>
Arg 5 is <--rev>
Arg 6 is <case 1234>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.