在 PowerShell 中WS-Management (WSMan) 远程处理

有关创建远程处理终结点的说明

适用于 Windows 的 PowerShell 包包括一个 WinRM 插件 (pwrshplugin.dll) 和一个安装脚本, (Install-PowerShellRemoting.ps1) 。$PSHome 指定终结点时,这些文件可使 PowerShell 接受传入的 PowerShell 远程连接。

动机

安装 PowerShell 可使用 New-PSSessionEnter-PSSession 针对远程计算机创建 PowerShell 会话。 若要使其接受传入的 PowerShell 远程连接,用户必须创建一个 WinRM 远程处理终结点。 这是一个显式选择加入方案,其中用户运行 Install-PowerShellRemoting.ps1 创建 WinRM 终结点。 将其他功能添加到 Enable-PSRemoting 以执行相同操作前,安装脚本是一个短期解决方案。 有关详细信息,请参阅问题 #1193

脚本操作

脚本

  1. $env:windir\System32\PowerShell 中创建插件目录
  2. 将 pwrshplugin.dll 复制到该位置
  3. 生成配置文件
  4. 使用 WinRM 注册该插件

注册

脚本必须在管理员级别的 PowerShell 会话中执行,并且以两种模式中运行。

由其将注册的 PowerShell 实例执行

Install-PowerShellRemoting.ps1

由代表其注册实例的另一 PowerShell 实例执行

<path to powershell>\Install-PowerShellRemoting.ps1 -PowerShellHome "<absolute path to the instance's $PSHOME>"

例如:

Set-Location -Path 'C:\Program Files\PowerShell\6.0.0\'
.\Install-PowerShellRemoting.ps1 -PowerShellHome "C:\Program Files\PowerShell\6.0.0\"

注意

远程处理注册脚本将重启 WinRM。 运行脚本后,将立即终止所有现有的 PSRP 会话。 如果在远程会话期间运行,脚本将终止连接。

如何连接到新的终结点

指定 -ConfigurationName "some endpoint name",从而针对新 PowerShell 终结点创建一个 PowerShell 会话。 若要连接到上述示例中的 PowerShell 实例,请使用以下任意一种方式:

New-PSSession ... -ConfigurationName "powershell.6.0.0"
Enter-PSSession ... -ConfigurationName "powershell.6.0.0"

请注意,New-PSSession未指定的 -ConfigurationNameEnter-PSSession 调用将面向默认 PowerShell 终结点 microsoft.powershell