答案1
目前没有本机 PowerShell cmdlet 来管理服务恢复。
但是,要在服务失败时自动重新启动,您可以使用SC
。
(在 PowerShell 提示符中,您必须在其前面加上 & 并使用全名sc.exe
)
& sc.exe failure msftpsvc reset= 30 actions= restart/5000
##reset in sec, rest in ms
## example for 1 day reset and 2min restart, twice before take no action
& sc.exe failure myService reset= 86400 actions= restart/120000/restart/240000/""/240000
官方文档位于微软文档在下面Sc 失败
答案2
摘要https://evotec.xyz/set-service-recovery-options-powershell/
function Set-ServiceRecovery{
[alias('Set-Recovery')]
param
(
[string] [Parameter(Mandatory=$true)] $ServiceDisplayName,
[string] [Parameter(Mandatory=$true)] $Server,
[string] $action1 = "restart",
[int] $time1 = 30000, # in miliseconds
[string] $action2 = "restart",
[int] $time2 = 30000, # in miliseconds
[string] $actionLast = "restart",
[int] $timeLast = 30000, # in miliseconds
[int] $resetCounter = 4000 # in seconds
)
$serverPath = "\\" + $server
$services = Get-CimInstance -ClassName 'Win32_Service' -ComputerName $Server| Where-Object {$_.DisplayName -imatch $ServiceDisplayName}
$action = $action1+"/"+$time1+"/"+$action2+"/"+$time2+"/"+$actionLast+"/"+$timeLast
foreach ($service in $services){
# https://technet.microsoft.com/en-us/library/cc742019.aspx
$output = sc.exe $serverPath failure $($service.Name) actions= $action reset= $resetCounter
}
}
Set-ServiceRecovery -ServiceDisplayName "Pulseway" -Server "MAIL1"