使用调用运算符执行 .exe
Execute an .exe using the call operator
我需要构建一个实际上是命令行的字符串,然后执行该命令行的内容。我希望调用运算符 (&) 能有所帮助,但似乎没有帮助。
$cmd = "`'c:\some path with spaces in it\java.exe`'"
$arguments = "-jar","`"C:\some Other Path\MyJar.jar`""
& $cmd @arguments
(@joey 在这里非常有帮助地建议:)
这产生了一个错误:
& : The term '"C:\some path with spaces in it\java.exe"' is not recognized as the name of
a cmdlet, function, script file, or operable program.
然后我发现,如果我将可执行文件放在不包含空格的路径中,那么我可以将我的代码更改为:
$command = "C:\somepathWithoutSpaces\java.exe"
$arguments = '-jar','"C:\someOtherPath\Cronacle.car"'
&$command @arguments
并且有效(注意缺少用于分隔符的转义字符)。
所以我想真正的问题是,如何在对 "c:\some path with spaces in it\java.exe" 的调用周围加上定界符,这样才能正常工作?
除了引用问题外,您的代码应该可以工作。请注意,该字符串应包含可执行文件的路径,而无需任何额外的引号。此外,不需要在字符串中引用参数。这就是我在上一个回答中试图阐明的内容,PowerShell 在正确使用时会自动执行这些操作。
如果它仍然抱怨找不到可执行文件,那么我会说它不存在。
PS> $cmd = 'C:\Program Files\Java\jdk1.8.0_05\bin\java.exe'
PS> $arguments = ,'-version'
PS> & $cmd @arguments
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)
尽管 space.
对我来说效果很好
同样,不要在程序或参数中添加额外的引号:
PS> $cmd = '"C:\Program Files\Java\jdk1.8.0_05\bin\java.exe"'
PS> & $cmd @arguments
& : The term '"C:\Program Files\Java\jdk1.8.0_05\bin\java.exe"' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
注意字符串中多余的(不必要的,甚至是错误的)引号。同样适用于参数:
PS> $arguments = ,'''-version'''
PS> & $cmd @arguments
Error: Could not find or load main class '-version'
我需要构建一个实际上是命令行的字符串,然后执行该命令行的内容。我希望调用运算符 (&) 能有所帮助,但似乎没有帮助。
$cmd = "`'c:\some path with spaces in it\java.exe`'"
$arguments = "-jar","`"C:\some Other Path\MyJar.jar`""
& $cmd @arguments
(@joey 在这里非常有帮助地建议:)
这产生了一个错误:
& : The term '"C:\some path with spaces in it\java.exe"' is not recognized as the name of a cmdlet, function, script file, or operable program.
然后我发现,如果我将可执行文件放在不包含空格的路径中,那么我可以将我的代码更改为:
$command = "C:\somepathWithoutSpaces\java.exe"
$arguments = '-jar','"C:\someOtherPath\Cronacle.car"'
&$command @arguments
并且有效(注意缺少用于分隔符的转义字符)。
所以我想真正的问题是,如何在对 "c:\some path with spaces in it\java.exe" 的调用周围加上定界符,这样才能正常工作?
除了引用问题外,您的代码应该可以工作。请注意,该字符串应包含可执行文件的路径,而无需任何额外的引号。此外,不需要在字符串中引用参数。这就是我在上一个回答中试图阐明的内容,PowerShell 在正确使用时会自动执行这些操作。
如果它仍然抱怨找不到可执行文件,那么我会说它不存在。
PS> $cmd = 'C:\Program Files\Java\jdk1.8.0_05\bin\java.exe'
PS> $arguments = ,'-version'
PS> & $cmd @arguments
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)
尽管 space.
对我来说效果很好同样,不要在程序或参数中添加额外的引号:
PS> $cmd = '"C:\Program Files\Java\jdk1.8.0_05\bin\java.exe"'
PS> & $cmd @arguments
& : The term '"C:\Program Files\Java\jdk1.8.0_05\bin\java.exe"' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
注意字符串中多余的(不必要的,甚至是错误的)引号。同样适用于参数:
PS> $arguments = ,'''-version'''
PS> & $cmd @arguments
Error: Could not find or load main class '-version'