svn - 如何在指定命令的目录名称中使用空格从 PowerShell 调用 CMD.EXE

标签 svn powershell cmd visualsvn-server

我有以下 PowerShell 脚本来验证我的 SVN 存储库:

$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)\... 才能正确通过。

我怀疑我缺少的字符串有些微不足道...

最佳答案

您需要调用cmd.exe像这样:

cmd.exe /c "`"$_cmd`""

您发送给 cmd.exe 的命令需要完全用它们自己的引号括起来,而不仅仅是这些命令中的带空格的路径。这与 Powershell 解析字符串的方式有关,它需要将文字引号传递给 cmd.exe以便它反过来正确解析双引号的内容。

例如,如果您已经在 cmd.exe session 并设置一个像这样的变量:
C:\>set _cmd="C:\Program Files (x86)\VisualSVN Server\bin\svnadmin" verify "C:\My Repositories\App1"

然后只需在命令行中扩展该变量即可:
C:\>%_cmd%

但是,如果将其传递给新的 cmd.exe session ,它还需要额外的引号:
C:\>cmd.exe /c "%_cmd%"

关于svn - 如何在指定命令的目录名称中使用空格从 PowerShell 调用 CMD.EXE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6471320/

相关文章:

linux - 我可以在 Linux、Mac、Windows 上使用哪个 SVN subversion?

powershell - 与 Powershell 并行发出请求会导致 "You cannot call a method on a null-valued expression"

python - 执行命令并获取输出

windows - 从批处理文件中的不同目录运行时 VBS 文件卡住

c++ - 如何确定 lib "references"是哪个 dll?

java - 无法通过java执行powershell命令?

java - SVN - 以编程方式提交文件 - 错误 svn : E200009: xyz. java 不受版本控制

java - 如果我将 .svn 目录从 ASCII 转换为 UTF-8 会怎样?

Eclipse 和 SVN : How to Rename Files to Uppercase?

powershell - 删除项目错误 : Cannot remove item [item path & name]: Access to the path '[item path & name]' is denied