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

设置远程服务';使用Powershell的恢复选项?,powershell,service,recovery,Powershell,Service,Recovery,我真的很难做到这一点。希望有人能帮我 我目前正在为服务编写Powershell部署脚本。安装服务后,我想将服务恢复选项设置为每次服务在0分钟后崩溃时“重新启动服务” 有人知道如何使用Powershell为远程计算机设置这些选项吗?如果是本地服务,您可以使用sc.exe,但您需要更改远程服务的设置。一种方法是直接使用远程注册表设置注册表项: 以下是您需要的设置: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<ServiceShor

我真的很难做到这一点。希望有人能帮我

我目前正在为服务编写Powershell部署脚本。安装服务后,我想将服务恢复选项设置为每次服务在0分钟后崩溃时“重新启动服务”


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

如果是本地服务,您可以使用
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.
然后将其序列化到磁盘以供以后使用:

$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))
$actions2 |导入Clixml C:\actions.xml
$key=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,”)
$key2=$key.OpenSubKey('SYSTEM\CurrentControlSet\Services\',$true)
$key2.SetValue('FailureActions',([byte[]]$actions))
该库有一个非常全面的
安装服务
cmdlet,可用于指定恢复操作,例如(改编自):


这将安装死亡之星服务,并在第一次故障后延迟10秒重新启动。

如前所述,您可以使用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"

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

我采用了@Mohammad Nadeem idea,并对其进行了扩展,完全支持所有操作,而不仅仅是主要操作。我还使用了服务的显示名称而不是服务名称,所以提供参数更容易一些

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"
我已经写了一篇关于它的博文:。除了重新启动服务之外,我没有在其他场景中测试过这一点。可能需要一些工作来支持所有场景

请简化

使用powershell代码中最旧的sc.exe。更简单,功能更全面

sc.exe failure "ServiceName" actions= restart/180000/restart/180000/reboot/180000 reset= 86400;
以毫秒为单位重新启动。最后一次重置,以秒为单位

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


再见

请注意,
sc.exe
能够对远程服务器进行更改,前提是您有身份验证的方法。在这里,如果我要运行程序,需要进行什么更改?如果您正在寻找一种在CI/CD场景下工作的解决方案,您将希望使用sc.exe。我希望运行一个程序,而不是重新启动。怎么做?
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"
sc.exe failure "ServiceName" actions= restart/180000/restart/180000/reboot/180000 reset= 86400;