簡體   English   中英

如果停止則啟動多個Windows服務

[英]Starting multiple Windows services if stopped

我已經嘗試了以下幾步,並且正在嘗試創建要插入另一個Powershell腳本的函數

function start-OService{
Get-Service service1|?{$_.Status -eq 'Stopped'}|Start-Service
Get-Service service2|?{$_.Status -eq 'Stopped'}|Start-Service
}

還有這個 ...

function start-OService {
$services = 'service1', 'service1'

Get-Service | ? {
$services -contains $_.Name -and $_.Status -eq 'Stopped'
} | Start-Service

}

第一個代碼段似乎無法正常運行,但我沒有收到錯誤消息。 我可以逐行運行每一行,但不能按我的方式進行運行。 第二個是我最初的嘗試,它來自另一個SO問題。 Get-Service似乎不喜歡包含多個服務的$services變量。

嘗試這個:

function start-OService {
$services = 'service1', 'service1'

    foreach ($service in $services)
    {
        if ((Get-Service $service).Status -eq "Stopped")
        {
        Start-Service $service
        }
    }

}

一班輪:

"service1", "service2" | Get-Service | ?{ $_.Status -eq "Stopped" } | Start-Service 

對於啟動在給定時間停止的所有服務,下面的腳本將很有幫助

$StoppedServices=Get-Service | where {$_.Status -eq "Stopped"}
foreach ($item in $StoppedServices)
{
$Name=$item.Name
    Start-Service -Name $Name
}
function start-OService {
$services = 'service1', 'service1'

    $services | % {
        $service =  get-service -name $_;
        if($service.Status -eq [System.ServiceProcess.ServiceControllerStatus]::Stopped)
        {
            Write-Output "Starting $_"
            $service.Start()
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM