【问题标题】:Trying to run sc create from PowerShell with nested quotes and can't get it to work尝试使用嵌套引号从 PowerShell 运行 sc create 并且无法使其正常工作
【发布时间】:2019-05-23 22:56:06
【问题描述】:

我正在尝试以编程方式创建一个服务,其中 binpath 包含包含空格的参数的嵌套引号。

通过命令提示符运行它可以正常工作,但它在使用 Invoke-Expression 的 powershell 中失败。我尝试过使用这里的字符串和脚本块,但仍然无法正常工作。

$serviceName = "my service"
$svcBinaryPath = '\"' + $pathToExe + '\" \"' + $parameterWithSpaces + '\"'

$cmd = "sc.exe create $('"' + $serviceName + '"') binpath= $('"' + $svcBinaryPath + '"')"
Invoke-Command $cmd

打印出$cmd 并运行它在命令提示符中显示的内容可以正常工作。

【问题讨论】:

  • 这里根本不需要Invoke-Commandsc.exe create "$serviceName" binpath= "$svcBinaryPath" 应该没问题。

标签: powershell


【解决方案1】:

你不能只传递一个字符串给Invoke-Command而你不需要它,就这样运行它:

$serviceName = "my service"
$scvBinaryPath = '\"' + $pathToExe + '\" \"' + $parameterWithSpaces + '\"'
sc.exe create $serviceName binpath= $svbBinaryPath

【讨论】:

    【解决方案2】:

    请注意,有一个用于创建服务的新服务 cmdlet。

    PS C:\users\j> get-command -noun service
    
    CommandType     Name                      Version    Source
    -----------     ----                      -------    ------
    Cmdlet          Get-Service               3.1.0.0    Microsoft.PowerShell.Management
    Cmdlet          New-Service               3.1.0.0    Microsoft.PowerShell.Management
    Cmdlet          Restart-Service           3.1.0.0    Microsoft.PowerShell.Management
    Cmdlet          Resume-Service            3.1.0.0    Microsoft.PowerShell.Management
    Cmdlet          Set-Service               3.1.0.0    Microsoft.PowerShell.Management
    Cmdlet          Start-Service             3.1.0.0    Microsoft.PowerShell.Management
    Cmdlet          Stop-Service              3.1.0.0    Microsoft.PowerShell.Management
    Cmdlet          Suspend-Service           3.1.0.0    Microsoft.PowerShell.Management
    

    【讨论】:

      猜你喜欢
      • 2016-02-23
      • 2020-06-03
      • 2023-03-26
      • 2022-12-12
      • 1970-01-01
      • 2017-09-17
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多