powershell - 当命令行中带有引号时,如何在Powershell中安装Windows服务?

标签 powershell windows-services

我有需要安装的服务,该服务的命令行如下所示:

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/

相关文章:

c# - 代表所有的 PowerShell 枚举值

powershell - 停止 Windows 服务时出错

powershell - 使用 Powershell 为 Word 文档添加页眉和页脚

azure - 如何在 Powershell Azure Devops Pipeline 中捕获错误 ERROR : Insufficient privileges to complete the operation.

PowerShell 解析数组列表

c# - 使用多线程处理一组数据库记录的选项?

c# - 如何按计划启动和停止 Windows 服务?

c# - 使用 Inno Setup 安装具有不同配置的同一 Windows 服务的多个实例

c# - C#Windows服务-在哪里可以找到好的教程?

Powershell接收作业: Quota violation when invoking command as a job