【问题标题】:PowerShell remoting: Controlling what edition is being targeted (PowerShell Core or Windows PowerShell); the state of cross-platform remotingPowerShell 远程处理:控制目标版本(PowerShell Core 或 Windows PowerShell);跨平台远程处理的状态
【发布时间】:2020-02-02 21:57:41
【问题描述】:

这个以 Windows[1] 为重点的自我回答问题解决了以下几个方面:

现在有 两个 PowerShell 版本 - 旧版仅限 Windows 的 Windows PowerShell 和 跨平台 PowerShell Core,两者都可以安装在给定的 Windows 机器上:

  • 如何判断哪个 PowerShell 版本将执行 remote commands,例如通过 Invoke-Command -ComputerName

  • 如何通过配置定位特定版本临时永久

    李>

注意:

要在给定计算机上通过远程处理可定位的版本,它必须是 set up for remoting:

  • 只有 Windows PowerShell自动设置为远程处理,但仅在运行 Windows Server 2012 或更高版本的服务器上。 p>

  • 从 v7 开始,PowerShell Core 尚未随 Windows 提供;如果您使用的是官方安装程序,您可以选择在安装期间启用远程处理。

无论如何,您都可以使用Enable-PSRemoting 按需(重新)启用PowerShell 远程处理,其中:

  • 必须从相应的版本运行

  • 必须以管理权限运行


[1] 也就是说,问题的重点是基于WinRM 的远程处理(WinRM 是Windows 特定的DTMF WSMan (WS-Management) standard 实现)。

至于 跨平台 使用 PowerShell Core 进行远程处理

  • 您已经可以在所有平台上使用SSH-based remoting

    • 使用基于 SSH 的远程处理涉及与基于 WinRM 的远程处理大部分相同的 cmdlet,但所涉及的参数不同;最值得注意的是,您通过 -HostName 参数指定目标计算机,而不是 -ComputerName 参数。

    • 限制(自 v7 起):“基于 SSH 的远程处理当前不支持远程端点配置和 Just Enough Administration (JEA)。”

  • 对于 Unix-to-Windows 远程处理Unix 指 macOS 和 Linux 等类 Unix 平台) - 即远程处理从类 Unix 机器到 Windows 机器 - 您也可以使用基于 WinRM 的远程处理和其他配置:

    • 在 Windows 机器上:

      • 必须通过为 HTTPS 配置 WinRM 来启用 SSL 连接。
      • 要在类 Unix 机器上使用的用户帐户必须定义​​为 local 管理员组中的 local 用户帐户 - 域帐户将不起作用。
    • 类 Unix 机器必须使用带有 -Authentication Basic -UseSsl 参数的远程 cmdlet。

    • about_Remote_Requirements

  • psl-omi-provider repository 中正在开发基于 Unix WSMan 的实现,它已经使 Linux 机器能够充当远程处理目标(即server 组件已经可用 - 我不清楚它是否也可以安装在 macOS 上);但是,在撰写本文时,客户端组件尚未准备好生产。
    一旦客户端客户端组件可用,就可以在类 Unix 机器(Linux、macOS)之间以及类 Unix 机器和 Windows 机器之间进行统一的基于 WSMan 的跨平台远程处理。

【问题讨论】:

    标签: powershell powershell-remoting winrm powershell-core wsman


    【解决方案1】:

    注意:正在考虑更改远程端点 PowerShell [Core] 的目标默认情况下 - 从 7.0 开始仍然是 Window PowerShell :见this GitHub issue


    本地指定的远程会话配置决定了远程机器上将使用什么 PowerShell 版本,可能还有版本

    • Ad hoc,您可以使用Invoke-CommandNew-PSSessionEnter-PSSession 等远程 cmdlet 的 -ConfigurationName 参数来指定明确的会话配置。

    • 永久,通过配置,您可以通过$PSSessionConfigurationName preference variable设置默认会话配置(链接的帮助主题还讨论了其他远程-session相关的偏好变量,即$PSSessionApplicationName$PSSessionOption)

      • 默认情况下,客户端连接到远程机器上的会话配置microsoft.powershell(见下文)。因此,您也可以选择更改此配置的定义在远程目标机器上,但请注意,这意味着所有客户端使用默认值将使用重新定义的配置 - 请参阅底部了解如何实现此重新定义。

    在远程操作的目标计算机Get-PSSessionConfiguration cmdlet 列出了客户端可以用来连接的所有已注册会话配置,您可以使用Register-PSSessionConfigurationUnregister-PSSessionConfiguration 进行管理:

    • 警告Get-PSSessionConfiguration 必须在提升会话中运行(以管理员身份),并且由于错误在 Windows PowerShell 5.1 中,您可能需要先运行以下虚拟命令:$null = Get-Command Test-WSMan,以确保定义了 wsman: 驱动器。

    • 名称以 'microsoft.powershell' 为前缀的会话配置属于 Windows PowerShell

    • 前缀 'PowerShell.' 指的是 PowerShell Core

    $PSSessionConfigurationNameboth 版本中默认为 'http://schemas.microsoft.com/powershell/Microsoft.PowerShell',这意味着 Windows PowerShell 默认针对远程计算机 即使您'从 PowerShell Core 运行

    • Microsoft.PowerShell 部分指的是(64 位)Windows PowerShell 会话配置,由Get-PSSessionConfiguration(小写)列出。

    • http://schemas.microsoft.com/powershell/ 前缀是可选的,可以省略;请注意,在前缀中使用 https: 确实 not 工作,并且将 not 自动切换到基于 SSL 的传输;对于后者,需要explicit configuration。请注意,如果您的所有远程处理都发生在 Windows 域中,则不需要基于 HTTPS/SSL 的远程处理。

    以远程计算机上的 PowerShell Core (PowerShell v6+) 为目标

    • 通常,PowerShell Core 会话配置是特定于版本的,您有两种选择:

      • 定位一个主要 PowerShell Core 版本 - 例如,PowerShell.7 - 使用目标计算机上安装的任何最新 v7.x 版本。

        • 这是更可取的,因为这样您的代码就不需要每次在目标机器上安装补丁或次要版本更新时都进行更新。
      • 定位一个特定的版本 - 例如,PowerShell.7.1.2

        • 仅当您有多个共享相同主要版本的并行安装并且您明确需要以其中一个为目标时才执行此操作。
      • 再次,在目标机器上运行Get-PSSessionConfiguration,从提升会话,告诉您所有已注册会话配置的名称。

    • 要针对 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'
    
    • 要让给定远程服务器机器的所有客户端默认情况下持久为目标,您必须重新定义服务器的microsoft.powershell会话配置,这需要管理员权限;您可以调整以下 sn-p:
    # 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
    

    【讨论】:

      猜你喜欢
      • 2014-11-19
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多