gpt4 book ai didi

powershell - PowerShell远程处理:控制目标版本(PowerShell Core或Windows PowerShell);跨平台远程处理的状态

转载 作者:行者123 更新时间:2023-12-03 00:42:23 26 4
gpt4 key购买 nike

这个针对Windows [1]的自我解答的问题解决了以下几个方面:

现在有两个PowerShell版本-遗留版本(仅限Windows)Windows PowerShell 跨平台PowerShell Core ,都可以安装在给定的Windows计算机上:

  • 如何才能知道哪个PowerShell版本将执行remote commands ,例如通过Invoke-Command -ComputerName
  • 如何通过配置将临时版本和持久版本定位为特定版本的

  • 注意:

    要通过在给定机器上远程处理来确定目标版本,它必须是set up for remoting:
  • 仅自动为远程设置Windows PowerShell,但仅在运行Windows Server 2012或更高版本的服务器上。
  • 从v7开始,PowerShell Core尚未随Windows一起提供。如果您使用的是官方安装程序,则可以选择在安装过程中启用远程处理。

  • 无论如何,您可以 使用 Enable-PSRemoting 来按需重新启用PowerShell远程处理:
  • 必须从相应的版本运行。
  • 必须以管理员权限运行


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

    至于使用PowerShell Core 进行跨平台远程处理的 :
  • 您已经可以在所有平台上使用 SSH-based remoting :
  • 使用基于SSH的远程处理所涉及的cmdlet与基于WinRM的远程处理几乎相同,尽管所涉及的参数有所不同;最值得注意的是,您通过-HostName参数而不是通过指定目标计算机-ComputerName参数。
  • 限制(自v7起):“基于SSH的远程处理当前不支持远程端点配置和充分管理(JEA)。”
  • 对于,从Unix到Windows的远程处理(Unix指的是从类似Unix的平台(例如macOS和Linux))-即从类似Unix的计算机迁移到Windows机器-您也可以使用基于WinRM的远程处理,以及其他功能。组态:
  • 在Windows机器上:
  • 必须通过为HTTPS配置WinRM来启用SSL连接。
  • 必须将类似Unix的计算机上要使用的用户帐户定义为本地Administrators组中的本地用户帐户-域帐户将不起作用。
  • 类似于Unix的计算机必须使用带有-Authentication Basic -UseSsl参数的远程cmdlet。
  • 参见about_Remote_Requirements
  • psl-omi-provider repository 中正在研究基于Unix WSMan的实现,该实现已经使Linux机器可以用作远程目标(即服务器组件已经可用-我尚不清楚它是否也可以安装在苹果系统);但是,在撰写本文时,客户端组件尚未投入生产。
    一旦客户端客户端组件可用,在类Unix机器(Linux,macOS)之间以及类Unix机器和Windows机器之间都可以进行基于WSMan的统一跨平台远程处理。
  • 最佳答案

    注意:正在考虑默认更改远程端点PowerShell [Core]的目标(从7.0版开始仍然是Window PowerShell):请参阅this GitHub issue

    这是本地指定的远程 session 配置,它确定在远程计算机上将使用哪种PowerShell版本以及可能的版本:

  • 临时,可以使用远程处理cmdlet的 -ConfigurationName参数诸如Invoke-CommandNew-PSSessionEnter-PSSession显式指定 session 配置。
  • 通过配置,您可以通过 $PSSessionConfigurationName preference variable持久地通过设置默认的 session 配置(链接的帮助主题还讨论了其他与远程 session 相关的首选项变量,即$PSSessionApplicationName$PSSessionOption)
  • 默认情况下,客户端连接到远程计算机上的 session 配置microsoft.powershell(请参见下文)。因此,您可以替代地在远程目标计算机上更改此配置的定义,但是请注意,这意味着所有使用默认设置的客户端都将使用重新定义的配置-有关如何实现此重新定义的信息,请参见底部。

  • 在远程操作的目标计算机上, Get-PSSessionConfiguration cmdlet列出了客户端可以用来连接的所有已注册 session 配置,您可以使用Register-PSSessionConfigurationUnregister-PSSessionConfiguration对其进行管理:
  • 警告:Get-PSSessionConfiguration必须在提升的 session 中运行(以管理员身份),并且由于Windows PowerShell 5.1中的错误,您可能必须首先运行以下虚拟命令:$null = Get-Command Test-WSMan,以确保wsman:驱动器被定义为)。
  • 名称以开头的 session 配置'microsoft.powershell'属于Windows PowerShell 。
  • 前缀 'PowerShell.' 是指PowerShell 核心

  • 在这两个版本中,$PSSessionConfigurationName均默认为'http://schemas.microsoft.com/powershell/Microsoft.PowerShell',这意味着 Windows PowerShell默认情况下针对远程计算机,即使您从PowerShell Core 运行:
  • Microsoft.PowerShell部分指的是(64位)Windows PowerShell session 配置,由Get-PSSessionConfiguration列出(小写)。
  • http://schemas.microsoft.com/powershell/前缀是可选的,可以省略。请注意,在前缀中使用https:无效,并且不会自动切换到基于SSL的传输;对于后者,需要explicit configuration。请注意,如果所有远程处理都在Windows域中进行,则不需要基于HTTPS / SSL的远程处理。

  • 要在远程计算机上定位PowerShell Core(PowerShell v6 +):
  • 通常,PowerShell Core session 配置是特定于版本的,您有两种选择:
  • 使用目标计算机上安装的最新v7.x版本,定位主要的PowerShell Core版本-例如PowerShell.7
  • 最好使用,因为您不需要每次在目标计算机上安装补丁或次要版本更新时都需要更新代码。
  • 指定特定版本-例如PowerShell.7.1.2
  • 仅当您具有多个共享相同主版本的并行安装且您明确需要将其中一个作为目标时,才执行此操作。
  • 再次,从提升的 session 在目标计算机上运行Get-PSSessionConfiguration,会告诉您所有已注册 session 配置的名称。
  • 要定位PowerShell Core即席,请使用-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

    关于powershell - PowerShell远程处理:控制目标版本(PowerShell Core或Windows PowerShell);跨平台远程处理的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58238481/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com