Passing arguments with variables in PowerShell to native application

Loading...

Passing arguments with variables in PowerShell to native application

Here is a part from my deployment automation script:
$SVNEXE = "$env:ProgramFiles\TortoiseSVN\bin\svn.exe"
foreach ($element in $commandstoken) {
#$exportfile = [System.Web.HttpUtility]::UrlPathDecode($StreamServe5RepoURI + $commandstoken[$a])
$exportfile = ($StreamServe5RepoURI + $commandstoken[$a])
$SVNRevision = $commandstoken[$a+1]
[string]$SVNCommand = "export -r $SVNRevision --force"
Write-Host -ForegroundColor Red $SVNCommand
Write-Host -ForegroundColor Red $SVNCommand $exportfile
&"$SVNEXE" $SVNCommand $exportfile "C:\Temp\__foo\export"
$a = $a+2
}

I want to pass the svn.exe the $SVNCommand variable but the svn.exe throws an error: svn.exe : Unknown subcommand: 'export -r 2384 --force'
As far as I can see the variable expansion is working so I can not understand why the svn.exe throws this error.

Solutions/Answers:

Answer 1:

The error Unknown subcommand: 'export -r 2384 --force' suggests that call operator treats 'export -r 2384 --force''export -r 2384 --force' this like a single argument (from technet on the & call operator):

Things can get tricky when an external command has a lot of parameters
or there are spaces in the arguments or paths! With spaces you have to
nest Quotation marks and the result it is not always clear!

In your case, you could do something like like suggested on that page:

 $SVNcommand = @('export', '-r', $SVNRevieion, "--force", $exportfile, "C:\Temp\__foo\export") 

And invoke with all arguments like this:

&"$SVNEXE" $SVNCommand 

Answer 2:

The only way I got it to work is to hold every parameter in a separate variable:

$arg1 = 'export', '-r'
$arg2 = $SVNRevision
$arg3 = '--force'
$arg4 = $exportfile
$arg5 = 'C:\Temp\__foo\export\'
&"$SVNEXE" $arg1 $arg2 $arg3 $arg4 $arg5

This seems actually bug Executing commands which require quotes and variables is practically impossible

References

Loading...