当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 远程处理入门

作者:精品下载站 日期:2024-12-14 08:09:59 浏览:16 分类:玩电脑

PowerShell 远程处理入门


[玩转系统] PowerShell 远程处理入门

PowerShell 远程处理自 PowerShell 语言诞生之初就已存在,允许在远程系统之间运行命令和接收输出。传统上,PowerShell Remoting 是在 WinRM 上构建的,但随着 PowerShell 版本的更新,SSH 协议现在也可用于促进 Linux 系统之间的远程处理。在本文中,您将了解如何开始使用 PowerShell 远程处理,重点关注 PowerShell 版本 5.1 及更高版本。

要求和设置

三种现代 PowerShell 变体仍在广泛使用。强烈建议迁移到 PowerShell 7,因为这是一个长期版本 (LTS) 并且是最新的。话虽这么说,但在某些情况下这还不可行。

电源外壳 5.1

这是与 Windows 操作系统捆绑在一起的最后一个版本的 Windows PowerShell。许多公司仍在使用,远程处理工作有一些简单的要求。

    • Windows Server 2016 上预装了 .NET 4.6.2
    • 预装适用于 Windows Server 2016 及更高版本
  • 在本文中,我们重点关注 Windows Server 2016,因为早期版本已被 Microsoft 视为生命周期结束。

    请注意,默认情况下,Windows PowerShell 将创建一个 microsoft.powershell 会话端点。当未指定其他会话端点时,Enter-PSSessionNew-PSSession 将使用此端点。由于较新版本的 PowerShell 不会创建或覆盖此端点,因此如果不连接到 Windows PowerShell,则必须指定端点名称才能连接到正确的远程实例。默认 Windows PowerShell 配置的示例如下(图 1)。

    [玩转系统] PowerShell 远程处理入门

    图 1:默认的 Windows PowerShell 配置

    PowerShell 核心 (6.x)

    PowerShell Core 附带了开箱即用所需的 .NET Core 文件。要使 WinRM PowerShell 远程处理正常工作,需要执行一些步骤。早期版本的 PowerShell Core 没有适当的 Enable-PSRemoting 支持,因此需要一个安装脚本,用于将 pwrshplugin.dll 文件移动到 System32 并注册端点。在 PowerShell Core 的最新版本中,Enable-PSRemoting 确实可以正常工作注册 DLL 和特定于版本的端点(如图 2 所示)。

    运行Enable-PSRemoting后,会注册正确的版本特定端点,在本例中为 6.2.5(图 3)。

    [玩转系统] PowerShell 远程处理入门

    图 2:在 PowerShell Core 中启用 WinRM PowerShell 远程处理

    [玩转系统] PowerShell 远程处理入门

    图 3:PowerShell Core 的版本特定端点

    指定-SkipNetworkProfileCheck参数的原因是任何公共网络都会提前结束Enable-PSRemoting命令。只要您了解其后果并对其进行适当控制,那么运行此命令就是安全的。

    电源外壳7

    同样,最新版本的 PowerShell 不需要Install-PowerShellRemoting.ps1 脚本不再为 Enable-PSRemoting 注册所需的所有内容,如下图所示4、通过运行 Enable-PSRemoting

    [玩转系统] PowerShell 远程处理入门

    图 4:PowerShell 7 中 Enable-PSRemoting cmdlet 的输出

    然后注册以允许远程处理的端点名称特定于最新的 PowerShell 版本,在本例中为 7.0.1。 (见图 5)。

    [玩转系统] PowerShell 远程处理入门

    图 5:使用 PowerShell 7 注册远程处理的端点名称列表

    SSH 远程处理要求

    在 PowerShell Core 和 PowerShell 7 中,现在可以使用 SSH 作为端点。要使 SSH 远程处理正常工作,需要满足一些要求和步骤。

    • PowerShell 核心 6.x 或 PowerShell 7.x
    • ssh.exe(客户端)和 sshd.exe(服务器)
    • Windows 10 build 1809 和 Windows Server 2019,用于内置 SSH(客户端)

    设置 SSH 端点

    设置 PowerShell 以使用 SSH 需要以下步骤和配置。

      • OpenSSH for Windows 可直接在 Windows 10(1809 或更高版本)和 Windows Server 2019 中作为可选功能使用(请访问 Thomas Maurer 的博客,了解如何在 Windows 10 和 Windows Server 上安装它的说明)
    • 在 Linux 上,您可以根据您的平台安装 OpenSSH
  • 配置 SSH 子系统以在远程计算机上托管 PowerShell 进程
  • 运行以下命令来安装所需的功能(OpenSSH.Client 可能已安装)、服务配置和防火墙规则。

    # Install the OpenSSH Client and Server 
    Add-WindowsCapability -Online -Name 'OpenSSH.Client~~~~0.0.1.0' 
    Add-WindowsCapability -Online -Name 'OpenSSH.Server~~~~0.0.1.0' 
    
    # Initial Configuration of SSH Server 
    Start-Service -Name 'sshd' 
    Set-Service -Name 'sshd' -StartupType 'Automatic' 
    
    # Confirm the Firewall rule is configured. It should be created automatically by setup. 
    Get-NetFirewallRule -Name '*ssh*' | Format-Table -AutoSize 
    
    # There should be a firewall rule named "OpenSSH-Server-In-TCP", which should be enabled 
    New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22

    最后,为了使 SSH 服务器能够与 PowerShell 远程处理配合使用,必须正确设置子系统。

    notepad $Env:ProgramData\ssh\sshd_config
    # Make sure that the subsystem line goes after the existing SFTP subsystem line 
    Subsystem powershell c:/progra~1/powershell/7/pwsh.exe -sshs -NoLogo -NoProfile 
    
    # Change c:/progra~1/powershell/7/pwsh.exe to c:/progra~1/powershell/6/pwsh.exe for PowerShell Core 
    PasswordAuthentication yes 
    
    # Below is optional but recommended to allow use of public/private keys 
    PubkeyAuthentication yes

    最后,重新启动 SSH 服务器:

    Restart-Service -Name 'sshd'

    Windows 附带的 OpenSSH 服务器版本存在错误。它要求任何文件路径都使用 8.3 短名称,因此 c:/progra~1。要验证您使用的 8.3 短名称是否正确,可以使用以下命令为 Program Files(安装了 PowerShell 6 或 7 的位置)检索该短名称:

    Get-CimInstance Win32_Directory -Filter 'Name="C:\Program Files"' | Select-Object EightDotThreeFileName

    PowerShell 远程处理的示例命令

    那么您可以使用 PowerShell Remoting 做什么以及它如何变得有用?很多时候,系统管理员需要从多个系统收集信息或同时在多个系统上运行命令来修复问题或部署新包。借助 PowerShell Remoting,这很容易完成。

    收集信息

    在下面的场景中,让我们远程连接到 PowerShell 7 远程端点并获取正在运行的服务。

    $Params = @{ 
    		"ComputerName" = 'Host1' 
    		"ConfigurationName" = 'PowerShell.7' 
    		"ScriptBlock" = { 
    				Get-Service | Where-Object Status -EQ 'Running' | Format-Table -AutoSize 
    				} 
    		} 
    Invoke-Command @Params

    运行命令

    也许我们需要重新启动一项服务,在本例中是 Print Spooler。

    $Params = @{ 
    		"ComputerName" = 'Host1' 
    		"ConfigurationName" = 'PowerShell.7' 
    		"ScriptBlock" = { 
    				Get-Service -Name 'Spooler' | Restart-Service 
    				} 
    		} 
    Invoke-Command @Params

    结论

    PowerShell Remoting 是一个功能强大的工具,通过添加跨平台功能和可选的 SSH 连接的使用,它成为系统管理员更加灵活和重要的工具。

    您需要 登录账户 后才能发表评论

    取消回复欢迎 发表评论:

    关灯