简体   繁体   English

启动服务后脚本抛出错误

[英]Script throwing an error after starting the service

I have written the below code to start the service :我写了下面的代码来start 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?我看到服务正在成功Running ,那么为什么即使服务已正确启动它也会抛出错误?

I am using poweshell 5我正在使用poweshell 5

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM