PowerShell远程管理01——Powershell远程管理依赖的服务及配置

概述:



Windows PowerShell 通过使用各种技术(包括 WMI、RPC 和 WS-Management)来支持远程管理的。

注:
WMI:Windows Management Instrumentation (服务名称:Winmgmt
RPC:Remote Procedure Call(RPC) (服务名称:RpcSs
WS-Management:Windows Remote Management(WS-Management)(服务名称:WinRM
以上三个服务,Windows系统中前两个服务默认是开启状态;
“WS-Management”(WinRM)服务可能需要干预开启

通过命令可以查询出三个服务的状态和启动类型

PS C:\> Get-Service -Name Winmgmt,RPCSS,WinRM  | Format-Table -Property Name,Status,StartType,DisplayName

Name        Status       StartType     DisplayName
----             ------           ---------         -----------
RPCSS     Running     Automatic    Remote Procedure Call (RPC)
Winmgmt  Running     Automatic    Windows Management Instrumentation
WinRM      Stopped    Manual         Windows Remote Management (WS-Management)

其中“RPCSS”和“Winmgm”服务都是开机启动运行(Automatic)且当前是运行状态(Running)。
只有“WinRM”服务启动类型是手动(Manual),且当前是停止状态(Stoped)。

 所以在不考虑防火墙拦截的情况下,需要PowerShell支持远程管理,确保以上三个服务属于开启状态即可。

 官方文档中有提到10个命令不需要特殊配置,可以直接运行,其实也是差不多也是依赖于已经开启的RPC和WMI服务(其中Test-Connection的原理和Ping命令类似)。

不需要做特殊配置,就可以进行操作的命令或者cmdlet如下:
(注:防火墙没有做相应的拦截的前提下,可以直接执行。)
  Restart-Computer
  Test-Connection
  Clear-EventLog
  Get-EventLog
  Get-HotFix
  Get-Process
  Get-Service
  Set-Service
  Get-WinEvent
  Get-WmiObject

通过以上的命令我们可以去操作主机的服务及进程等,例如可以去开启“WinRM”服务
  

例子1:远程重启计算机

  使用“Restart-Computer”命令重启计算机,如果有用户登录的情况下,无法执行,可以加上“-Force”参数强制执行。
  

$cred=Get-Credential
Restart-Computer -ComputerName "sz-test1119.test.local"  -Credential $cred
Restart-Computer -ComputerName "sz-test1119.test.local"  -Credential $cred -Force

命令执行结果如下:

PS C:\Users> $cred=Get-Credential
位于命令管道位置 1 的 cmdlet Get-Credential
请为以下参数提供值:
Credential
PS C:\Users>Restart-Computer -ComputerName "sz-test1119.test.local"  -Credential $cred
Restart-Computer : 无法重新启动计算机 sz-test1119.test.local,并显示以下错误消息: 无法启动系统关机
,因为有其他用户登录到计算机。
所在位置 行:1 字符: 1
+ Restart-Computer -ComputerName "sz-test1119.test.local"  -Credential $c ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (sz-test1119.test.local:String) [Restart-Computer
   ], InvalidOperationException
+ FullyQualifiedErrorId : RestartcomputerFailed,Microsoft.PowerShell.Commands.RestartComput
   erCommand

PS C:\Users> Restart-Computer -ComputerName "sz-test1119.test.local"  -Credential $cred
PS C:\Users> Restart-Computer -ComputerName "sz-test1119.test.local"  -Credential $cred -Force

PowerShell远程管理01——Powershell远程管理依赖的服务及配置

例子2: 开启远程计算机的WinRM服务

# PowerShell 获取 WinRM 服务状态
Get-Service WinRM

可以直接通过“Start-service winrm”开启

PS C:\WINDOWS\system32> Start-Service WinRM
PS C:\WINDOWS\system32> Get-Service WinRM

Status      Name             DisplayName
---------     -------              -----------
Running   WinRM          Windows Remote Management (WS-Manag...

如果是开启远程计算机的WinRM服务可以借助“Set-Service”的“-Status running”进行设置

Set-Service WinRM -ComputerName "sz-test1119.test.local" -Status Running

命令执行过程

PS C:\Users> Get-Service WinRM -ComputerName "sz-test1119.test.local"

Status        Name           DisplayName
------             ----               -----------
Stopped     WinRM          Windows Remote Management (WS-Manag...

PS C:\Users> Set-Service WinRM -ComputerName "sz-test1119.test.local" -Status Running
PS C:\Users> Get-Service WinRM -ComputerName "sz-test1119.test.local"

Status       Name               DisplayName
------          ----                     -----------
Running   WinRM              Windows Remote Management (WS-Manag...

PowerShell远程管理01——Powershell远程管理依赖的服务及配置

下一节:PowerShell远程管理02——Powershell远程管理的几种方式:
返回我的Powershell学习笔记

猜你喜欢

转载自blog.51cto.com/3chou/2562585