首页 > 系统相关 >PowerShell教程 - 远程管理(Remoting and Remote Management)

PowerShell教程 - 远程管理(Remoting and Remote Management)

时间:2022-08-27 08:33:56浏览次数:58  
标签:Management Remote 连接 PowerShell PSSession Remoting New 远程

更新记录
转载请注明出处。
2022年8月27日 发布。
2022年8月27日 从笔记迁移到博客。

远程管理说明(Remoting and Remote Management)

PowerShell远程连接说明

与Telnet和Secure Shell(SSH)不同
PowerShell采用一种新的通信协议
称之为针对管理的Web服务(Web Services for Management,WS-Management)
WS-MAN完全基于HTTP或者HTTPS进行工作
WS-Management uses SOAP)to pass information between the client and the server
WS-MAN的实现主要基于一个后台服务:Windows远程管理组件(WinRM)
安装PowerShell v2以上的时候会同时安装WinRM
在服务器操作系统(比如Windows Server 2008 R2)中默认开启该服务
Windows 7操作系统默认安装该服务,但处于禁用状态

远程连接工作原理

当运行一个远程命令时,它会将输出结果放入一个特定形式的包中
之后通过网络中的HTTP(或者HTTPS)协议传回本地计算机
传输的对象数据PowerShell会将输出对象序列化到XML中
XML文件会通过网络进行传输
当到达本地计算机之后,该XML会反序列化为PowerShell可以处理的对象
序列化和反序列化仅仅是一种格式转换的形式
从对象转化为XML称为序列化,从XML转为对象则为反序列化

远程连接的配置要求

PowerShell v2或更新版本

WinRM服务说明

WinRM服务默认会使用TCP端口5985侦听HTTP,使用5986端口侦听HTTPS
image

连接处理

WSMan drive(WSMan驱动)

The WSMan drive is accessible when PowerShell is running as the administrator
The drive can be used to view and change the configuration of remoting

设置maximum permissible size of SOAP messages sent and received by WSMan

Set-Item WSMan:\localhost\MaxEnvelopeSizekb 1024

重启WSMan服务

Restart-Service winrm

Remoting and SSL

By default, Windows remoting requests are unencrypted
可以使用HTTPS来支持加密
在使用HTTPS监听请求之前,需要一个证书
一般使用自签的证书(self-signed certificate)来配置SSL(configuring SSL)

创建自签证书

New-SelfSignedCertificate -DnsName $env:COMPUTERNAME

然后会生成一个自签的密钥,将自签的密钥放入到CertificateThumbprint中

开启'WSMan的监听

将自签的密钥放入到CertificateThumbprint中

$params = @{
  Path = 'WSMan:\localhost\Listener'
  Address = '*'
  Transport = 'HTTPS'
  CertificateThumbprint = 'D8D2F174EE1C3888C2021C9B7EB88883CB1B9A41'
  Force = $true
}
New-Item @params

注意:如果开启了防火墙,需要放行WsMan的连接和端口

$params = @{
   DisplayName = $name = 'Windows Remote Management (HTTPS-In)'
   Name = $name
   Profile = 'Any'
   LocalPort = 5986
   Protocol = 'TCP'
}
New-NetFirewallRule @params

设置服务器端启用SSL

Set-WSManQuickConfig -UseSSL

开启远程连接(Enabling remoting)

进行连接之前需要在服务器端开启远程连接
开启远程连接的方法:
组策略名称: Allow remote server management through WinRM
组策略路径: Computer configuration\Administrative Templates\Windows Components\Windows Remote Management (WinRM)\WinRM Service

注意:在防火墙端放行远程连接的端口

开启远程管理服务

Enable-PSRemoting

关闭远程管理服务

Disable-PSRemoting

连接说明

PowerShell可以通过两种方法实现远程处理
第一种称为一对一 或者 1:1远程处理
第二种称为一对多 或者 1:n远程处理

创建会话

New-PSSession

实例:
新建一个新的会话

New-PSSession -ComputerName 192.168.1.106

指定端口

New-PSSession -ComputerName xx.com -Port 1103 -Name "指定名称"

指定名称

New-PSSession -ComputerName xx.com -Port 1103 -Name "指定名称"

将会话存入变量

$panda = New-PSSession -ComputerName 192.168.1.106

还可以添加多台计算机

New-PSSession -ComputerName 192.168.1.106,192.168.1.107

获得创建好的会话

Get-PSSession

关闭会话

Remove-PSSession

一对一远程处理

实际上是在单台远程计算机上调用了一个Shell命令窗口
输入的任何命令都会直接在该计算机上运行
然后在远程处理窗口中返回输出结果
该机制非常类似于远程桌面连接(Remote Desktop Connection)
只是Windows PowerShell采用的是命令行环境
相对于远程桌面连接,这种远程处理技术只需要使用很少的资源
所以对服务器来讲,开销会小很多

服务器端开启允许远程连接

Enable-PSRemoting

客户端开启允许远程连接并跳过本地的网络检查

Enable-PSRemoting -SkipNetworkProfileCheck -force

客户端设置信任的IP或域名
注意:*表示全部IP信任

Set-Item WSMan:\localhost\Client\TrustedHosts -Value * -Force

客户端设置允许使用脚本

set-ExecutionPolicy RemoteSigned

查看WinRm是否开启成功

winrm enumerate winrm/config/listener

开始连接到服务器
设置用户名,administrator为用户名

$uname="administrator"

设置密码,123456为密码

$pwd=ConvertTo-SecureString "123456" -AsPlainText -Force; 

创建自动认证对象

$cred=New-Object System.Management.Automation.PSCredential($uname,$pwd); 

设置服务器端的IP地址

$servers="IP"

登录到远程服务器

Enter-PSSession -ComputerName $servers -Credential $cred

还可以使用New-Session创建好的会话

Enter-PSSession -session

退出远程连接

Exit-PSSession

获得使用弹出窗口获得账号和密码

New-PSSession -ComputerName PSTest -Credential (Get-Credential) - Authentication CredSSP

一对多远程处理(批量执行命令)

将一个命令同时传递给多台远程计算机
每台计算机都独立运行发送的命令,然后将结果集返回
批量更新,则不需要连接到远程机子上,只需要给远程机发送指令

发起命令

Invoke-Command

实例:

#设置用户名,administrator为用户名
$uname="administrator"
#设置密码,123456为密码
$pwd=ConvertTo-SecureString "123456" -AsPlainText -Force;
#创建自动认证对象
$cred=New-Object System.Management.Automation.PSCredential($uname,$pwd); 
#设置服务器IP地址
$servers="IP"
#执行的命令,ScriptBlock参数后为命令
Invoke-Command -ComputerName $servers -credential $cred -ErrorAction Stop -ScriptBlock {Invoke-Expression -Command "& `"D:\lwccRelease\tools\AutoUpdate\Auto.bat`" C:\temp\TestProject1\TestProject1.pjs /run /exit /SilentMode"} 
Invoke-Command -ComputerName $servers -credential $cred -ErrorAction Stop -ScriptBlock {Invoke-Expression -Command "New-Item -Type File D:/test.txt"}

标签:Management,Remote,连接,PowerShell,PSSession,Remoting,New,远程
From: https://www.cnblogs.com/cqpanda/p/16629774.html

相关文章