Svn 如何使用指定命令中的空格从PowerShell调用CMD.EXE';s目录名
我有以下PowerShell脚本用于验证我的SVN存储库:Svn 如何使用指定命令中的空格从PowerShell调用CMD.EXE';s目录名,svn,powershell,cmd,visualsvn-server,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
$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.exe:“C:\Program”未被识别为内部或外部命令,
第5行字符:12
+cmd.exe您需要像这样调用cmd.exe
:
cmd.exe /c "`"$_cmd`""
C:\>set _cmd="C:\Program Files (x86)\VisualSVN Server\bin\svnadmin" verify "C:\My Repositories\App1"
发送到cmd.exe
的命令需要完全用自己的引号括起来,而不仅仅是包含空格的路径,这些空格是这些命令的一部分。这与Powershell解析字符串的方式有关,它需要将文字引号传递给cmd.exe
,从而正确解析双引号的内容
例如,如果您已经在cmd.exe
会话中,并设置如下变量:
cmd.exe /c "`"$_cmd`""
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%"
我无法在PowerShell中实现这一点cmd.exe
不断出错,抱怨'C:\Program'未被识别为内部或外部命令…
。我正在运行PowerShell 5.1。有什么想法吗?那么powershell转义角色是backtick?