使用Powershell设置远程服务的恢复选项?

6

我很难让这个工作起来。希望有人能帮帮我!

我目前正在编写一个用于服务的Powershell部署脚本。在安装服务后,我想将服务恢复选项设置为“每次服务崩溃后立即重启”,时间间隔为0分钟。

有人知道如何使用Powershell为远程计算机设置这些选项吗?

6个回答

8

您可以按照这里所述的方法,使用sc.exe编写PowerShell函数。该函数应如下所示:

function Set-Recovery{
    param
    (
        [string] 
        [Parameter(Mandatory=$true)]
        $ServiceName,

        [string]
        [Parameter(Mandatory=$true)]
        $Server
    )

    sc.exe "\\$Server" failure $ServiceName reset= 0 actions= restart/0 #Restart after 0 ms
}

你可以像这样调用函数:

Set-Recovery -ServiceName "ServiceName" -Server "ServerName"

注意:运行该脚本的帐户必须在远程服务器上拥有管理员权限。

6
请简化一下。。
在PowerShell代码中使用最古老的sc.exe。这样更容易且功能完整。
sc.exe failure "ServiceName" actions= restart/180000/restart/180000/reboot/180000 reset= 86400;

重新启动时间,以毫秒计算。最后一次重置是以秒为单位的。
(每次重新启动间隔3分钟,重置每天进行一次)
再见!

这里,如果我想运行一个程序而不是重新启动,需要做出什么改变? - Priya
如果您正在寻找适用于CI/CD场景的解决方案,那么您需要使用sc.exe。 - ntcolonel

4
我采纳了@Mohammad Nadeem的想法,并扩展了它,使得所有操作都得到了充分支持,而不仅仅是主要操作。我还使用了显示名称来表示服务,而不是服务名称,这样更容易提供参数。
function 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' | 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-Recovery -ServiceDisplayName "Pulseway" -Server "MAIL1"

我已经写了一篇关于此的博客文章:https://evotec.xyz/set-service-recovery-options-powershell/。除了服务重启之外,我还没有在其他场景中进行测试。可能需要进行一些工作才能支持所有情况。


我想运行一个程序而不是重新启动,该怎么做? - Priya

3
如果是本地服务,你可以使用 sc.exe 命令。但如果你要更改远程服务的设置,则需要直接使用远程注册表设置以下键值:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<ServiceShortName>    
Value Name                Data Type    Description
FailureActions            REG_BINARY   Configuration information for 1st, 2nd, and subsequent failures.

我会做的是设置你想要的服务恢复选项,然后读取注册表值FailureActions

$actions = get-itemproperty hklm:\system\currentcontrolset\services\<ServiceShortName> | select -Expand FailureActions

然后将其序列化到磁盘上以备后用:

$actions | Export-Clixml C:\actions.xml

当您准备远程配置服务时,请重新阅读FailureActions数据,连接到远程注册表并设置注册表键:

$actions2 | Import-Clixml C:\actions.xml
$key = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, "<RemoteComputerName>")
$key2 = $key.OpenSubKey('SYSTEM\CurrentControlSet\Services\<ServiceShortName>', $true)
$key2.SetValue('FailureActions', ([byte[]] $actions))

请注意,sc.exe 有能力对远程服务器进行更改,前提是您有身份验证的手段。 - Eric A. Laney

1

Carbon库拥有非常全面的Install-Service cmdlet命令,可让您指定恢复操作,例如(改编自Install-Service doc page):

Install-Service -Name DeathStar -Path C:\ALongTimeAgo\InAGalaxyFarFarAway\DeathStar.exe -OnFirstFailure Restart -RestartDelay 10000

这将安装DeathStar服务,并在第一次失败后延迟10秒后重新启动。


0

jborean93 创建了一个自定义类型,将本地 C# 服务对象和方法暴露给 PowerShell。包括的 Get-ServiceRecovery 和 Set-ServiceRecovery 函数使得在 PowerShell 中查看和更改服务恢复设置变得容易。 https://gist.github.com/jborean93/889288b56087a2c5def7fa49b6a8a0ad

.\ServiceRecovery.ps1
Get-ServiceRecovery -ComputerName 'MyComputer' -Name 'MyService'
Set-ServiceRecovery -ComputerName 'MyComputer' -Name 'MyService' -Actions @('RunCommand', 'Restart', 'None') -Command '"C:\Windows\System32\cmd.exe" /c echo hi'

对于DSC的粉丝们,看起来这个功能也将在未来某个时候被整合到xPSDesiredStateConfiguration(xService)中。 https://github.com/dsccommunity/xPSDesiredStateConfiguration/pull/679

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接