Powershell 在我的脚本中获取错误

Powershell 在我的脚本中获取错误,powershell,scripting,powershell-remoting,Powershell,Scripting,Powershell Remoting,我一直收到一条错误消息 "Exception calling "Start" with "0" argument(s): "Cannot start service RTCRGS on computer 'CORPxxxxxxx.xxxx.net'." At line:25 char:18 + $_.Start <<<< () + CategoryInfo : NotSpecified: (:) [], MethodInvoc

我一直收到一条错误消息

"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 | 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"
}

您可以读取“Status”的可能值。

目标服务器上的服务是否存在问题?i、 你能手动停止/启动它吗?(即,它可能被禁用)。有些服务可能需要以不同的方式手动启动。我同意。您是否尝试过将
启动服务
-ComputerName
参数一起使用?哪个
$\uuu.Start()
行出现故障?如果是第二个循环,您需要在尝试重新启动它之前添加一个检查和等待循环。i、 e.服务可能仍处于“停止”状态,并在尝试重新启动时出错。(也就是说,5秒的时间可能不足以让服务完全停止)@HAL9256谢谢您的帮助,关于时间和循环的惊人建议!!!:)啊。。我知道你问过他的情况
$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"
}