簡體   English   中英

啟動服務后腳本拋出錯誤

[英]Script throwing an error after starting 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

執行上述腳本后,出現以下錯誤:

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

我看到服務正在成功Running ,那么為什么即使服務已正確啟動它也會拋出錯誤?

我正在使用poweshell 5

繼續我的評論,嘗試:

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