I have the following PowerShell script for verifying my SVN repositories:
$SVNAdminDir = 'C:\Program Files (x86)\VisualSVN Server\bin';
$RepositoryDir = 'C:\My Repositories\App1';
$_cmd = "`"$SVNAdminDir`\svnadmin`" verify `"$RepositoryDir`"";
Write-Host $_cmd; # Copying this into the command prompt runs without an issue...
cmd.exe /c $_cmd; # It's when I try to execute the command from PS that I get the error.
But when I try to execute it, I’m receiving the following error message:
cmd.exe : 'C:\Program' is not recognized as an internal or external command,
At line:5 char:12
+ cmd.exe <<<< /c $_cmd;
+ CategoryInfo : NotSpecified: ('C:\Program' is...ternal command,:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
operable program or batch file.
Since I’m essentially setting $cmd = '"C:\Program Files (x86)\VisualSVN Server\bin\svnadmin" verify "C:\My Repositories\App1"'; with the double quotes inside of the single quotes, I was expecting the space in C:\Program Files (x86)\… to be passed correctly.
I suspect there’s something trivial with the string that I’m missing…
You need to call
cmd.exelike this:The commands you send to
cmd.exeneed to be entirely wrapped in their own quotes, not just the paths-with-spaces that are part of those commands. This has to do with how Powershell parses the string and it needs to pass literal quotes tocmd.exeso that it in turn does its own parsing of the contents of double-quotes correctly.For example, if you were already in a
cmd.exesession and set a variable like this:Then simply expanding that variable at the commandline would work:
However, if passing it to a new
cmd.exesession, it would also need extra quotes: