代码之家  ›  专栏  ›  技术社区  ›  Blue Clouds

无法从powershell设置windows服务运行方式

  •  0
  • Blue Clouds  · 技术社区  · 4 年前

    $myArgs = 'config "service22" start= demand obj= "'+$machineName+'\'+$userName+'" password= "'+$userPassword+'"'
    Start-Process -FilePath sc.exe -ArgumentList $myArgs
    

    我也尝试过多次改变变量的位置(所有地方都尝试过)

    $svc = Get-WmiObject win32_service -filter "name='service22'"
    $svc.change($null,$null,$null,$null,$null,$null,$null,$null,$null,$userName,$userPassword)
    

    0 回复  |  直到 4 年前
        1
  •  1
  •   BeerRider    4 年前

    没必要把这件事搞得太复杂了。

    只是:

    Invoke-Command -ComputerName computername -ScriptBlock {
        Set-Service -Name servicename -Credential (Get-Credential user@domain.com)
    }
    

        2
  •  0
  •   Blue Clouds    4 年前

    here

    只是需要补充一下 type=own

    $myArgs = 'config "service22" start= demand obj= "'+$machineName+'\'+$userName+'" password= "'+$userPassword+'" type=own'
    Start-Process -FilePath sc.exe -ArgumentList $myArgs