在 PowerShell 中WS-Management (WSMan) 遠端處理

建立遠端端點的指示

適用于 Windows 的 PowerShell 套件包含 WinRM 外掛程式 () pwrshplugin.dll ,以及 中的 $PSHome 安裝腳本 () Install-PowerShellRemoting.ps1 。 這些檔案讓 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