遠程 PowerShell 的詳盡入門指南

有没有想过从工作站轻松地在远程机器上运行 PowerShell 命令?嗯,通过 PowerShell Remoting 就可以实现。通过远程 PowerShell,您可以以可持续的方式在多个远程系统上运行命令和脚本。

听起来有趣吗?继续阅读,了解如何以 PowerShell 的方式驾驭这些远程机器。

前提条件

本教程将进行实际演示。如果您想跟着操作,请确保您至少有三台安装了 PowerShell 7 的计算机。其中一台将是您的管理计算机,另外两台将是远程机器。

本教程将使用以下计算机。

  • A Windows 10 PC with PowerShell 7 installed as the management computer. You will launch remote commands from this machine.
  • A Windows 10 PC with PowerShell 7 installed. This machine will be the endpoint for remote PowerShell over WinRM.
    • A user account on the remote Windows machine with local administrator rights. This tutorial uses a user account called ma.

在Windows上啟用遠程PowerShell超WinRM

安裝PowerShell 7時,有一個選項可啟用PowerShell遠程,您可能當時已啟用它。但為了確保,請按照以下步驟啟用遠程PowerShell。

1. 登錄到遠程Windows 10 PC。

2. 以管理員身份打開PowerShell。

3. 執行以下命令以啟用遠程PowerShell。

Enable-PSRemoting

指令執行多項配置更改,將在控制台中顯示。

Enabling Remote PowerShell on Windows over WinRM

在SSH上啟用Linux上的遠端PowerShell

WinRM僅適用於Windows,這意味著您無法使用它在Linux上啟用遠端PowerShell。相反,您可以在Linux上透過SSH啟用遠端PowerShell。假設您已安裝PowerShell,請按照以下步驟進行。

1. 登入到遠端Linux系統並開啟終端機會話。

2. 在文字編輯器中打開SSH伺服器配置文件。可以使用您喜歡的文字編輯器,例如Gedit、Sublime或Nano。此示例使用Gedit。

sudo nano /etc/ssh/sshd_config

3. 接下來,在文件中添加以下行。此行創建一個SSH子系統,用於托管PowerShell進程。

Subsystem powershell /usr/bin/pwsh -sshs -NoLogo
Create an SSH subsystem for PowerShell on Linux

4. 關閉編輯器。

5. 最後,執行以下命令重新啟動SSH伺服器。

sudo systemctl restart sshd

在本機計算機上添加遠端PowerShell受信任主機

此時,您的遠端計算機已準備好接收命令。但是,如果它們不在受信任主機列表上,您的管理計算機可能會拒絕連接到遠端PowerShell主機,為避免可能的遠程問題,您必須將遠端主機添加到受信任主機中。

在管理計算機上以系統管理員身份開啟PowerShell。

運行以下的winrm指令。此指令將您的遠端Windows(192.168.8.107)和Linux(192.168.8.171)機器新增至您電腦的信任主機清單。

winrm set winrm/config/client '@{TrustedHosts=" 192.168.8.171,192.168.8.107"}'
Adding remote machines as Trusted Hosts

在遠端電腦上執行指令

所有準備就緒後,您可以在此部分執行遠端機器上的指令。

1. 在管理電腦上開啟一個新的PowerShell會話。

2. 現在,執行以下的Invoke-Command指令以列出以ma使用者身份運行的遠端機器上的前五個服務。 要運行的指令顯示在ScriptBlock開關的大括號之間,如下所示。

Invoke-Command `
    -ComputerName 192.168.8.107 `
    -Credential 'ma' `
    -ScriptBlock { Get-Process | Select-Object -First 5 }
Listing the processes on the remote PowerShell Windows host

3. 要在遠端Linux主機上執行相同的操作,請改為運行以下指令。請注意,-HostName取代了-ComputerName-UserName取代了-ComputerName。這些參數告訴Invoke-Command cmdlet 目標是一個SSH主機。

Invoke-Command `
    -HostName 192.168.8.171 `
    -UserName 'test' `
    -ScriptBlock { Get-Process | Select-Object -First 5 }
Listing the processes on the remote PowerShell Linux host

在遠端電腦上執行腳本

您也可以使用Invoke-Command指令在遠端電腦上執行PowerShell腳本檔案。但是,您不會指定-ScriptBlock參數,而是指定-FilePath參數,後面是腳本的本地路徑。

1. 首先,在您的本地電腦上建立一個名為nametime.ps1的PowerShell腳本檔案。

2. 在您喜歡的腳本編輯器中打開腳本,並添加以下代碼。之後保存文件。

# nametime.ps1
hostname
Get-Date
Create the PowerShell script

3. 使用 Invoke-command cmdlet 執行,並使用 -FilePath 參數。 -FilePath 應該指向腳本文件。 在此示例中,腳本文件位於相同的工作目錄中。

# 通過 WinRM 在遠端 PowerShell 上調用腳本
Invoke-Command `
    -ComputerName 192.168.8.107 `
    -Credential 'ma' `
    -FilePath .\nametime.ps1

# 通過 SSH 在遠端 PowerShell 上調用腳本
Invoke-Command `
    -HostName 192.168.8.171 `
    -UserName 'test' `
    -FilePath .\nametime.ps1
Invoke a script on remote PowerShell hosts

管理遠端計算機互動

在前一節中,您看到如何在遠端計算機上運行個別命令。 在本節中,您將學習打開一個交互式會話,以便您可以像在本地一樣在遠端計算機上運行多個命令。

1. 使用 Enter-PSSession 命令在位於 192.168.8.107 的機器上以 ma 用戶身份開始遠端會話。

Enter-PSSession -ComputerName 192.168.8.107 -Credential ma

在提示符下輸入遠端用戶帳戶密碼,ma,如下所示。

Entering an interactive remote session

您將看到一個新的提示符,如下所示。 方括號內的 IP 地址表示遠端 PowerShell 主機。

Viewing a remote prompt

3. 使用 dir 命令列出當前工作目錄中的文件,如下所示。 您可以在會話中執行任意多個 PowerShell 命令 dir

dir

目錄的內容應顯示在屏幕上,如下截圖所示。

Linting the files on a remote machine

4. 一旦完成管理遠端計算機,請執行下面的 Exit-PSSession cmdlet 退出會話。

Exit-PSSession

退出後,您應該返回到本地提示符,如下截圖所示

Exiting a remote session

斷開和重新連接遠端 PowerShell 會話

可將會話持久存儲,作為交互會話或一次性命令的替代方法。存儲會話使您能夠在不失去上下文的情況下在會話之間切換。在本節中,您將學習創建和管理持久會話的方法。

1. 使用 New-PSSession cmdlet 在遠端計算機 192.168.8.107 上創建會話,以用戶 ma (-Credential)。將會話保存在變量 $var 中,如下所示。

$var = New-PSSession -ComputerName 192.168.8.107 -Credential ma

2. 執行變量 $var,以查看其內容,如下所示。

$var

如下所示,您應該看到一個具有 RemoteMachine ComputerTypeState 設置為 Opened 的會話對象。

Displaying the details of a session

3. 執行 Enter-PSSession 命令以進入存儲在 $var 中的會話。

Enter-PSSession $var

如果一切順利,提示符將更改為遠端提示符,而不會有進一步的輸出,如下所示。

Entering a persistent remote session

4. 在會話內創建一個變量,$rem1。使用任何適合您的變量名稱和標識符字符串。稍後您將使用此變量來確定是否已連接到相同的會話。

$rem1 = "same session"

5. 執行以下命令以退出遠端 PowerShell 會話。

Exit-PSSession
Disconnecting from a persistent remote session

6. 重新建立遠端 PowerShell 會話。

Enter-PSSession $var

7. 在遠端會話提示符處執行 $rem1 變量,如下所示,以查看其值。

$rem1

请确认您在退出远程 PowerShell 会话前看到之前设置的相同值,以确认您已重新连接到相同的会话。

Viewing the value of a session variable

8. 运行以下命令Exit-PSSession以再次退出会话。

Exit-PSSession

9. 最后,使用Invoke-Command并将session开关设置为$var,在相同的远程会话中显示远程计算机的主机名。这是在相同会话中运行命令的另一种方式,无需手动连接、断开连接和重新连接到单个会话。

Invoke-Command -Session $var -ScriptBlock {hostname}
Displaying the hostname of a remote machine

10. 运行Remove-PSSession以彻底删除存储在$var中的会话。

Remove-PSSession $var

结论

恭喜!通过完成本教程,您已经学会了如何使用远程 PowerShell 管理 Windows 或 Linux 系统。本指南旨在让您初步了解。为什么不深入研究Linux 上的 PowerShell,通过远程会话深入了解呢?

Source:
https://adamtheautomator.com/remote-powershell/