簡體   English   中英

PowerShell 遠程處理:控制目標版本(PowerShell Core 或 Windows PowerShell); 跨平台遠程處理的state

[英]PowerShell remoting: Controlling what edition is being targeted (PowerShell Core or Windows PowerShell); the state of cross-platform remoting

這個自我回答的問題側重於 Windows [1] ,涉及以下方面:

Now that there are two PowerShell editions - the legacy, Windows-only Windows PowerShell and the cross-platform PowerShell Core , both may be installed on a given Windows machine:

  • 如何判斷哪個 PowerShell 版本將執行遠程命令,例如通過Invoke-Command -ComputerName

  • 我怎樣才能通過配置來定位特定的版本,無論是臨時的還是持久的?

筆記:

對於通過遠程在給定機器上定位的版本,它必須設置為遠程

  • 只有Windows PowerShell自動設置為遠程處理,但僅在運行 Windows Server 2012 或更高版本的服務器上。

  • 從 v7 開始,PowerShell Core還沒有附帶 Windows; 如果您使用的是官方安裝程序,您可以選擇在安裝期間啟用遠程處理。

在任何情況下,您都可以使用Enable-PSRemoting按需(重新)啟用 PowerShell 遠程處理,其中:

  • 必須從相應的版本運行。

  • 必須以管理權限運行


[1] 也就是說,問題側重於基於WinRM的遠程處理(WinRM 是 DTMF WSMan (WS-Management) 標准的 Windows 特定實現)。

至於使用 PowerShell Core 的跨平台遠程處理

  • 您已經可以在所有平台上使用基於 SSH 的遠程處理

    • 使用基於 SSH 的遠程處理與基於 WinRM 的遠程處理主要涉及相同的 cmdlet,但所涉及的參數不同; 最值得注意的是,您通過-HostName參數而不是-ComputerName參數指定目標計算機。

    • 限制(從 v7 開始) :“基於 SSH 的遠程處理當前不支持遠程端點配置和 Just Enough Administration (JEA)。”

  • 對於Unix 到 Windows 的遠程處理Unix指的是 macOS 和 Linux 等類 Unix 平台) - 也就是說,從類 Unix 機器遠程處理到 Windows 機器 - 您也可以使用基於 WinRM 的遠程處理和其他配置:

    • 在 Windows 機器上:

      • 必須通過為 HTTPS 配置 WinRM 來啟用 SSL 連接。
      • 從類 Unix 機器上使用的用戶帳戶必須定義為本地管理員組中的本地用戶帳戶 - 域帳戶將不起作用。
    • 類 Unix 機器必須使用帶有-Authentication Basic -UseSsl參數的遠程 cmdlet。

    • 查看about_Remote_Requirements

  • psl-omi-provider 存儲庫中正在開發基於 Unix WSMan 的實現,它已經使Linux機器能夠充當遠程目標(也就是說,服務器組件已經可用 - 我不清楚它是否也可以安裝在macOS上); 然而,在撰寫本文時,客戶端組件還沒有准備好生產。
    一旦客戶端客戶端組件可用,就可以在類 Unix 機器(Linux、macOS)之間以及類 Unix 機器和 Windows 機器之間進行統一的基於 WSMan 的跨平台遠程處理。

Note : Changing what remote endpoint PowerShell [Core] targets by default - which as of 7.0 is still Window PowerShell - is being considered: see this GitHub issue .


本地指定的遠程處理 session 配置決定了遠程計算機上將使用的 PowerShell 版本和可能的版本

  • Ad hoc ,您可以使用遠程 cmdlet(例如Invoke-CommandNew-PSSessionEnter-PSSession )的 -ConfigurationName -ConfigurationName來明確指定 session 配置。

  • 持久地,通過配置,您可以通過$PSSessionConfigurationName首選項變量設置默認的 session 配置(鏈接的幫助主題還討論了其他與遠程會話相關的首選項變量,即$PSSessionApplicationName$PSSessionOption

    • 默認情況下,客戶端連接到遠程計算機上的 session 配置microsoft.powershell (見下文)。 因此,您也可以在遠程目標機器上更改此配置的定義,但請注意,這意味着所有使用默認值的客戶端都將使用重新定義的配置- 請參閱底部了解如何實現此重新定義。

在遠程操作的目標計算機Get-PSSessionConfiguration cmdlet 列出了客戶端可以用來連接的所有已注冊 session 配置,您可以使用Register-PSSessionConfigurationUnregister-PSSessionConfiguration管理這些配置:

  • Caveat : Get-PSSessionConfiguration must be run in an elevated session (as administrator), and, due to a bug in Windows PowerShell 5.1, you may have to run the following dummy command first: $null = Get-Command Test-WSMan , so以確保wsman: drive 已定義)。

  • Session 配置名稱以'microsoft.powershell '為前綴屬於Windows Z3D265B4FAE00318DDFZB7881

  • 前綴'PowerShell.' 指 PowerShell內核

$PSSessionConfigurationName兩個版本中默認為'http://schemas.microsoft.com/powershell/Microsoft.PowerShell' ,這意味着Windows PowerShell默認針對遠程計算機,即使您是從 Z3D265B4E01EEEF018DCCZZ Core 運行的

  • The Microsoft.PowerShell part refers to the (64-bit) Windows PowerShell session configuration, as listed by Get-PSSessionConfiguration (in lowercase).

  • http://schemas.microsoft.com/powershell/前綴是可選的,可以省略; 請注意,使用https:前綴不起作用並且不會自動切換到基於 SSL 的傳輸; 對於后者,需要顯式配置 請注意,如果您的所有遠程處理都發生在 Windows 域中,則不需要基於 HTTPS/SSL 的遠程處理。

要在遠程計算機上定位 PowerShell Core (PowerShell v6+)

  • 通常,PowerShell Core session 配置是特定於版本的,您有兩種選擇:

    • 使用目標機器上安裝的最新 v7.x 版本,主要的 PowerShell 核心版本為目標- 例如, PowerShell.7

      • 這是更可取的,因為您的代碼不需要每次在目標機器上安裝補丁或次要版本更新時進行更新。
    • 針對特定版本 - 例如, PowerShell.7.1.2

      • 僅當您有多個共享相同主要版本的並行安裝並且您明確需要以其中一個為目標時才執行此操作。
    • 同樣,從提升的 session 在目標計算機上運行Get-PSSessionConfiguration ,會告訴您所有已注冊的 session 配置的名稱。

  • 要針對 PowerShell Core ad hoc ,請使用-ConfigurationName PowerShell.7 ,例如:

# Connect to computer $comp and make it execute $PSVersionTable 
# in PowerShell Core v7.x, which tells you what PowerShell edition 
# and version is running.
Invoke-Command -ComputerName $comp -ConfigurationName PowerShell.7 { $PSVersionTable }
  • 默認情況下,要從給定的客戶端機器上以 PowerShell Core 為目標,請在$PROFILE文件中添加以下內容:
# When remoting, default to running PowerShell Core v7.x on the
# the target machines:
$PSSessionConfigurationName = 'PowerShell.7'
  • 要讓給定遠程服務器機器的所有客戶端默認以 PowerShell Core 為目標,您必須重新定義服務器的microsoft.powershell session 配置 您可以調整以下代碼段:
# Run WITH ELEVATION (as administrator) and
# ONLY IF YOU UNDERSTAND THE IMPLICATIONS.

$ErrorActionPreference = 'Stop'

# The configuration whose definition you want to make the new default.
$newDefaultConfigSource = 'PowerShell.7'

# Standard registry locations and names.
$defaultConfigName = 'Microsoft.PowerShell'
$configXmlValueName = 'ConfigXml'
$configRootKey = 'registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Plugin'

# Rename the current default configuration XML to "ConfigXml.OLD" to keep a backup.
Rename-ItemProperty $configRootKey\$defaultConfigName $configXmlValueName -NewName "$configXmlValueName.OLD"

# Get the configuration XML from the configuration that should become the new default.
# Modify it to replace the source configuration name with the default configuration name.
$xmlText = (Get-ItemPropertyValue $configRootKey\$newDefaultConfigSource $configXmlValueName) -replace 
             ('\b{0}\b' -f [regex]::Escape($newDefaultConfigSource)), $defaultConfigName

# Save the modified XML as the default configuration's config XML.
Set-ItemProperty $configRootKey\$defaultConfigName $configXmlValueName $xmlText

# Restart the WinRM service for changes to take effect.
Restart-Service WinRM

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM