添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have written the below code to start the service :

invoke-command -cn $server -Credential $cred -ScriptBlock {
param($svc) 
if((get-service $svc).status -ne "running") {
    get-service $svc| start-service     
    set-service $svc -StartupType Automatic
    (get-service $svc).waitforstatus('running')  
 get-service $svc| select ServiceName
} -ArgumentList $svc

After executing the above script, I am getting below error:

Status         : Running
StartType      : Automatic
ServiceName    : svcname
PSComputerName : host1
+     invoke-command -cn $server -Credential $cred -ScriptBlock {
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException
    + FullyQualifiedErrorId : StartServiceFailed,Microsoft.PowerShell.Commands.StartServiceCommand

I see service is Running successfully so why it is throwing an error even when the service was started correctly?

I am using poweshell 5

StartServiceFailed would indicate that it's not after starting the service, it's Start-Service itself that's failing. Check the System event log on the target machine – Mathias R. Jessen Mar 15, 2022 at 16:10 You cannot use $host as self-defined variable because it is an Automatic variable: Contains an object that represents the current host application for PowerShell. Choose a different name for that – Theo Mar 15, 2022 at 16:20 Great, but is this a typo also? set-service $svc-StartupType.. There should be a space between $svc and parameter -StartyupType I also believe you need to set the startup type before starting it and you could do with a lot less calls to get-service. Why not store it in a variable? – Theo Mar 15, 2022 at 16:50

Continuing from my comments, try:

Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock {
    param($svc) 
    $theService = Get-Service -Name $svc
    if($theService.Status -ne 'Running') {
        if ($theService.Status -ne 'Stopped') { $theService | Stop-Service -Force }
        $theService | Set-Service -StartupType Automatic
        ($theService | Start-Service -PassThru).WaitForStatus('Running')
     $theService | Select-Object ServiceName
} -ArgumentList $svc
                I see the script is failing again on one server. the only difference is that it is in Paused state right now. Status         : Paused StartType      : Automatic  + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],ServiceCommandException
– meallhour
                Mar 15, 2022 at 19:15
                @meallhour Hmm.. Looks like we have to do a waiting loop to make sure it has stopped after Stop-Service. I'm on mobile now but will edit tomorrow
– Theo
                Mar 15, 2022 at 19:42
                sure. I am trying to manually start the service, it is throwing an error saying that the service and the service is stuck in paused state.
– meallhour
                Mar 15, 2022 at 19:52
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.