如何在命令行中引用Powershell时在Windows中安装Windows服务?
我有一个服务,我需要安装有一个命令行,看起来是这样的:如何在命令行中引用Powershell时在Windows中安装Windows服务?
d:\My Service\service.exe /AsService /Config="d:\Config Files\TheConfig.config"
这是相当多了一个命令行中最糟糕的情况。 我遇到的问题是参数中的双引号。
这是我已经试过了是接近但不完全:
$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>
尝试声明$FullServicePath
这样的:
$FullServicePath = "d:\My Service\service.exe /AsService /Config=`"d:\Config Files\TheConfig.config`""
它使用转义字符`来告诉它处理的报价为文本。 (转义字符是在1个键上大多数键盘背面抽动未来)
我相信你也可以使用这个语法:我的系统上
$FullServicePath = $('d:\My Service\service.exe /AsService /Config="d:\Config Files\TheConfig.config"')
两个创建的变量没有问题。
当你调用$FullServicePath
做这样的:$($FullServicePath)
我试过这个,但我似乎无法通过它sc.exe或installutil正确。我已经尝试过每一种方式,但它没有出现。 – 2012-08-01 21:49:27
对不起,我想我误解了你的问题。我添加了一个不同的方式来调用$ FullServicePath。试试这个,看看会发生什么。在创建变量组合并使用奇怪字符调用变量的变量时,我会做很多工作。 – Nick 2012-08-01 22:27:52
你知道在命令行的双引号是否会得到支持?这篇文章似乎并不表示某种方式。 – 2012-08-01 16:02:04
我不确定试试看,如果它不起作用尝试一些这样的事情''$ ServiceName'''应该让PowerShell把它当作一个字符串 – justinf 2012-08-01 16:07:30