【问题标题】:Getting error in my script在我的脚本中出现错误
【发布时间】:2019-01-18 11:33:27
【问题描述】:

我不断收到一条错误消息,说明

"Exception calling "Start" with "0" argument(s): "Cannot start service RTCRGS on computer 'CORPxxxxxxx.xxxx.net'."
At line:25 char:18
+          $_.Start <<<< () 
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException"

关于为什么会发生这种情况以及如何解决它的任何建议?

$services = "RTCRGS"
$SvrName = 'CORPxxxx.xxxx.xxxx'
Get-Service -ComputerName $SvrName -Name $services | % {
    Write-host "$($_.Name) on $SvrName is $($_.Status)"
    if ($_.Status -eq 'stopped') {
        Write-Host "Starting $($_.Name) in 5 sec..."
        Start-Sleep 5
        Write-Host "$($_.Name) is Starting, please wait..."
        $_.Start()
        Write-Host "Checking status of service, please wait for 25 sec..."
        Start-Sleep 20
        Write-Host "$($_.Name) on $SvrName is $($_.Status)"
    } elseif ($_.Status -eq 'running') {
        Write-Host "Restaring $($_.Name) in 5 sec..."
        Start-Sleep 5
        $_.Stop()
        Start-Sleep 5
        Write-Host "$($_.Name) is Starting, please wait..."
        $_.Start()
        Write-Host "Checking status of service, please wait for 25 sec..."
        Start-Sleep 25
        Write-Host "$($_.Name) on $SvrName is $($_.Status)"
    }
}

【问题讨论】:

  • 目标服务器上的服务有问题吗?即您可以手动停止/启动它吗? (即它可能被禁用)。某些服务可能需要以不同的方式手动启动。
  • 我同意。您是否尝试过将Start-Service-ComputerName 参数一起使用?
  • 哪个$_.Start() 行失败了?如果是第二个,则需要在尝试重新启动之前添加一个检查和等待循环。即服务可能仍处于“停止”状态,并且在尝试重新启动时会出错。 (即 5 秒可能不足以让服务完全停止)
  • @HAL9256 感谢您的帮助,关于时间和循环的惊人建议!!! :)

标签: powershell scripting powershell-remoting


【解决方案1】:

如果该服务未被禁用(并且您已检查可以手动重新启动它),您可能会更好地使用类似的东西

$services = "RTCRGS"
$SvrName = 'CORPxxxx.xxxx.xxxx'
Get-Service -ComputerName $SvrName -Name $services | ForEach-Object {
    Write-host "$($_.Name) on $SvrName is $($_.Status)"

    if (@("Stopped","StopPending","Paused","PausePending") -contains $_.Status) {
        Write-Host "Starting service $($_.Name), please wait..."
        $_.Start()
    } 
    else {
        Write-Host "Stopping service $($_.Name)..."
        $_.Stop()
        # as HAL9256 suggested, perform a loop to see if service has really stopped
        do {
            # recheck the ACTUAL status, not the status you captured before
            $newStatus = (Get-Service -ComputerName $SvrName -Name $_.Name).Status
        } while ($newStatus -ne "Stopped")
        Write-Host "$($_.Name) is Restarting, please wait..."
        $_.Start()
    }

    Write-Host "Checking status of service, please wait for 25 sec..."
    Start-Sleep 25

    # here again you should recheck the ACTUAL status, not the status you captured before
    $newStatus = (Get-Service -ComputerName $SvrName -Name $_.Name).Status
    Write-Host "$($_.Name) on $SvrName is $newStatus"
}

您可以读取“状态”here 的可能值

【讨论】:

  • 啊..我看到你问过before的状态@
猜你喜欢
  • 2016-04-04
  • 2016-06-21
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
  • 1970-01-01
  • 2019-06-23
  • 1970-01-01
相关资源
最近更新 更多