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

Max*_*der 5 powershell service recovery

我很难让这个工作起来.希望有人可以帮助我!

我目前正在为服务开发Powershell部署脚本.安装服务后,我希望每次服务在0分钟后崩溃时将服务恢复选项设置为"重新启动服务".

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

Moh*_*eem 7

为解释你可以写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
}
Run Code Online (Sandbox Code Playgroud)

你可以调用这个函数:

Set-Recovery -ServiceName "ServiceName" -Server "ServerName"
Run Code Online (Sandbox Code Playgroud)

注意:您运行脚本的帐户必须具有远程服务器的管理员权限.


小智 6

请简化一下..

在 powershell 代码中使用最旧的 sc.exe。更加简单且功能齐全。

sc.exe failure "ServiceName" actions= restart/180000/restart/180000/reboot/180000 reset= 86400;
Run Code Online (Sandbox Code Playgroud)

重新启动(以毫秒为单位)。最后一次重置,以秒为单位。

(每次重启3分钟,重置1天)

再见!


Mad*_*Boy 5

我采纳了 @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"
Run Code Online (Sandbox Code Playgroud)

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


And*_*ndi 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.
Run Code Online (Sandbox Code Playgroud)

我要做的就是按照您想要的方式设置服务恢复选项,然后读取注册表值FailureActions

$actions = get-itemproperty hklm:\system\currentcontrolset\services\<ServiceShortName> | select -Expand FailureActions
Run Code Online (Sandbox Code Playgroud)

然后将其序列化到磁盘以供稍后使用:

$actions | Export-Clixml C:\actions.xml
Run Code Online (Sandbox Code Playgroud)

当您准备好远程配置服务时,重新读取 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))
Run Code Online (Sandbox Code Playgroud)