我有需要安装的服务,该服务的命令行如下所示:
d:\My Service\service.exe /AsService /Config="d:\Config Files\TheConfig.config"
对于命令行而言,这几乎是最坏的情况。 我遇到的问题是args 中的双引号。
这是我尝试过但不完全是的事情:
$cmd = "create ""$ServiceName"" binPath= $FullServicePath start= $StartMethod"
Invoke-Expression "cmd.exe /c sc.exe $cmd" | Write-Host
其中
$FullServicePath
是:d:\My Service\service.exe /AsService /Config="d:\Config Files\TheConfig.config"
有什么建议吗?我对任何事情都开放。 WMI。随你。
救命!
最佳答案
如果您100%确定需要引号,则必须使用\
对其进行转义:
&'d:\My Service\service.exe' '/AsService' '/Config=\"d:\Config Files\TheConfig.config\"'
Echoargs输出:
Arg 0 is </AsService>
Arg 1 is </Config="d:\Config Files\TheConfig.config">
但这可能也可以工作,并且容易得多:
&'d:\My Service\service.exe' @('/AsService', '/Config=d:\Config Files\TheConfig.config\')
Echoargs输出:
Arg 0 is </AsService>
Arg 1 is </Config=d:\Config Files\TheConfig.config>
关于powershell - 当命令行中带有引号时,如何在Powershell中安装Windows服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11762890/