为什么Windows远程管理服务坚持“延迟启动”?

问题描述:

我遇到了一些与WinRM服务有关的问题。它一直坚持是“延迟启动(自动)”服务而不是“自动”服务。为什么Windows远程管理服务坚持“延迟启动”?

为什么?这是我的虚拟机(在Hyper-V上)的问题。我通过PowerShell以编程方式恢复它们,然后需要通过PowerShell远程访问它们,但有时WinRM服务在我首次将虚拟机联机时(它们“完全引导”,就像我可以登录它们一样)尚未启动。

如果我将服务设置为自动,则运行PowerShell命令winrm quickconfig表示服务未设置为远程处理,并坚持将服务设置为延迟启动。

在我尝试打开远程PowerShell会话之前,如何确保Windows RM服务正在运行?

+0

你可以尝试改变启动类型从注册表 - http://computerstepbystep.com/windows_remote_management_(ws_management)_service.html – Mitul

+0

检查我的答案。你在看同样的东西吗? – Mitul

为什么某些服务可能会在启动过程(延迟启动)后加载的基本推理是:

  1. 为了提高服务器的boot performance,具有一定的安全优势。

  2. 某些服务依赖其他服务启动。在Windows远程管理服务的情况下,它取决于以下服务
    a。 HTTP服务
    b。远程过程调用(RPC)(自动)
    i。 DCOM服务器进程启动器(自动)
    ii。 RPC终结点映射器(自动)

我怎样才能确保Windows RM服务之前,我 试图打开远程PowerShell会话运行?

看看下面的选项和我写的功能来做你想做的事情。

A)您可以使用测试连接来检查计算机是否联机。

Test-Connection -ComputerName $Computer -Count 1 -Quiet 

)我创建功能StartWinRMIfStopped将使用WMI启动 “WinRM的” 服务。

Ç)第二个功能是TryToCreateNewPSSession将尝试创建一个新的PSSession还是应该给你的异常对象

param([string]$server) 
Get-PSSession | Remove-PSSession 
$newsession = $null 
function StartWinRMIfStopped 
{ 
param([string]$ComputerName) 
    Write-Host $ComputerName 
    $WinRMService = Get-WmiObject -Namespace "root\cimv2" -class Win32_Service -Impersonation 3 -ComputerName $ComputerName | Where-Object {$_.Name -match "WinRM"} 
    if($WinRMService.State -eq "Stopped" -or $WinRMService.State -eq "Paused"){ 
     "WinRM Service is" + $WinRMservice.State 
     $WinRMService.StartService() 
    } 
    else{ 
     "WinRM Service is " + $WinRMservice.State 
    } 
} 
function TryToCreateNewPSSession{ 
    param([string]$computerName) 
    Try 
    { 
     $newsession = New-PSSession -Computer $computerName -ErrorAction Stop  
     #Connect-PSSession -Session $newsession 
     $newsession   
    } 
    Catch [System.Management.Automation.RuntimeException]{  
     if($error.Exception.Gettype().Name -eq "PSRemotingTransportException"){ 
      Write-host "WinRM service is not started on the server" 
     } 
     Write-host "RuntimeException occured in creating new PSSession to the Server" 
    } 
    Catch [Exception]{ 
     Write-host "Generic Exception while creating PSSession" 
    } 
} 

$error.Clear() 
If (Test-Connection -Computer $server -count 1 -Quiet) { 
#Connection to server successfull  
StartWinRMIfStopped $server 
Start-Sleep -s 4 
#Invoke Command on remote server using trytocreatenewpssession function. 
Invoke-Command -Session (TryToCreateNewPSSession $server) -ScriptBlock { write-host "hello world"} 
} 

您可以调用整个脚本

PS C:\> .\ScriptName.ps1 remotecomputername 
+0

感谢您输入Mitul。我会尝试一下并回复你。 – tnw