我有以下用于验证SVN存储库的PowerShell脚本:
$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.
但是当我尝试执行它时,我收到以下错误消息:
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.
由于我实际上是用单引号内的双引号设置$cmd = '"C:\Program Files (x86)\VisualSVN Server\bin\svnadmin" verify "C:\My Repositories\App1"';
,所以我期待 C:\ Program Files(x86)\ ... 中的空格正确传递。
我怀疑我错过的字符串有些微不足道......
答案 0 :(得分:17)
您需要像这样致电cmd.exe
:
cmd.exe /c "`"$_cmd`""
您发送给cmd.exe
的命令需要完全包装在他们自己的引号中,而不仅仅是那些命令中包含空格的路径。这与Powershell如何解析字符串有关,它需要将文字引号传递给cmd.exe
,以便它自己正确地解析双引号的内容。
例如,如果您已经在cmd.exe
会话中并设置了如下变量:
C:\>set _cmd="C:\Program Files (x86)\VisualSVN Server\bin\svnadmin" verify "C:\My Repositories\App1"
然后只需在命令行扩展该变量即可:
C:\>%_cmd%
但是,如果将其传递给新的cmd.exe
会话,则还需要额外的引号:
C:\>cmd.exe /c "%_cmd%"