本文說明連線和使用 JEA 端點的各種方式。
以互動方式使用 JEA
如果您要測試 JEA 組態或為使用者執行簡單的工作,您可以使用 JEA,就像是一般 PowerShell 遠端會話一樣。 對於複雜的遠端工作,建議使用 隱式遠端技術處理。 隱式遙控讓使用者能夠在本機操作數據物件。
若要以互動方式使用 JEA,您需要:
- 您要連線的電腦名稱(可以是本機電腦)
- 在該電腦上註冊的 JEA 端點名稱
- 可存取該電腦上 JEA 端點的認證
根據該資訊,您可以使用 New-PSSession 或 Enter-PSSession Cmdlet 來啟動 JEA 會話。
$sessionParams = @{
ComputerName = 'localhost'
ConfigurationName = 'JEAMaintenance'
Credential = Get-Credential
}
Enter-PSSession @sessionParams
如果目前的使用者帳戶可以存取 JEA 端點,您可以省略 Credential 參數。
當 PowerShell 提示符變為 [localhost]: PS> 時,您知道您現在正在與遠端 JEA 工作階段互動。 您可以執行 Get-Command 來檢查可用的命令。 請洽詢系統管理員,以瞭解可用參數或允許的參數值是否有任何限制。
請記住,JEA 會話會以 NoLanguage 模式運作。 有些您通常使用 PowerShell 的方式可能無法使用。 例如,您無法使用變數來儲存數據,或檢查從 Cmdlet 傳回之物件的屬性。 下列範例示範兩種方法,讓相同的命令在 NoLanguage 模式中運行。
# Using variables is prohibited in NoLanguage mode. The following will not work:
# $vm = Get-VM -Name 'SQL01'
# Start-VM -VM $vm
# You can use pipes to pass data through to commands that accept input from the pipeline
Get-VM -Name 'SQL01' | Start-VM
# You can also wrap subcommands in parentheses and enter them inline as arguments
Start-VM -VM (Get-VM -Name 'SQL01')
# You can also use parameter sets that don't require extra data to be passed in
Start-VM -VMName 'SQL01'
當命令調用更為複雜,使此方法變得困難的情況下,請考慮使用 隱式遠端處理 或建立 自訂函式 來包裝您需要的功能。
如需NoLanguageMode的詳細資訊,請參閱about_Language_Modes。
搭配隱含式遠端操作使用 JEA
PowerShell 具有隱含遠端模型,可讓您從遠端計算機匯入 Proxy Cmdlet,並與它們互動,就像是本機命令一樣。 隱含遠程在這篇 Hey, Scripting Guy!部落格文章中說明。 使用 JEA 時,隱含遠端功能很有用,因為它可讓您在完整語言模式中使用 JEA 的命令小程序。 您可以使用 Tab 鍵補全、變數、操作物件,甚至使用本地腳本在 JEA 端點上自動化任務。 每當您叫用 Proxy 命令時,數據就會傳送至遠端電腦上的 JEA 端點,並在該處執行。
隱含遠端處理的運作方式是透過從現有的 PowerShell 會話匯入命令行小程序。 您可以選擇選擇在每個 Proxy Cmdlet 的名詞前面加上您選擇的字串。 前置詞可讓您區分遠程系統的命令。 系統會在本機 PowerShell 工作階段期間建立併匯入包含所有 Proxy 命令的暫存腳本模組。
# Create a new PSSession to your JEA endpoint
$jeaSession = New-PSSession -ComputerName 'SERVER01' -ConfigurationName 'JEAMaintenance'
# Import the entire PSSession and prefix each imported cmdlet with "JEA"
Import-PSSession -Session $jeaSession -Prefix 'JEA'
# Invoke "Get-Command" on the remote JEA endpoint using the proxy cmdlet
Get-JEACommand
這很重要
某些系統可能無法匯入整個 JEA 會話,因為預設 JEA Cmdlet 中的條件約束。 若要達成此目的,只匯入您需要的命令,並將這些命令的名稱明確指定至 -CommandName 參數。 未來的更新將解決在受影響系統上匯入整個 JEA 會話的問題。
如果您因為預設參數上的 JEA 條件約束而無法匯入 JEA 會話,請遵循下列步驟,從匯入的集合中篩選掉預設命令。 您可以繼續使用 之類的 Select-Object命令,但只會使用計算機上安裝的本機版本,而不是從遠端 JEA 工作階段匯入的本機版本。
# Create a new PSSession to your JEA endpoint
$jeaSession = New-PSSession -ComputerName 'SERVER01' -ConfigurationName 'JEAMaintenance'
# Get a list of all the commands on the JEA endpoint
$commands = Invoke-Command -Session $jeaSession -ScriptBlock { Get-Command }
# Filter out the default cmdlets
$jeaDefaultCmdlets = @(
'Clear-Host'
'Exit-PSSession'
'Get-Command'
'Get-FormatData'
'Get-Help'
'Measure-Object'
'Out-Default'
'Select-Object'
)
$filteredCommands = $commands.Name | Where-Object { $jeaDefaultCmdlets -notcontains $_ }
# Import only commands explicitly added in role capabilities and prefix each
# imported cmdlet with "JEA"
Import-PSSession -Session $jeaSession -Prefix 'JEA' -CommandName $filteredCommands
您也可以使用 Export-PSSession,從隱含遠端保存 Proxy Cmdlet。 如需隱含的遠端處理的詳細資訊,請參閱 Import-PSSession 和 Import-Module 的檔案。
以程序設計方式使用 JEA
JEA 也可用於自動化系統和使用者應用程式中,例如內部技術服務人員應用程式和網站。 這種方法與建置與不受限制 PowerShell 端點通訊的應用程式相同。 請確定此計劃的設計目的是要與 JEA 所施加的限制搭配使用。
針對簡單的一次性工作,您可以使用 Invoke-Command 在 JEA 會話中執行命令。
Invoke-Command -ComputerName 'SERVER01' -ConfigurationName 'JEAMaintenance' -ScriptBlock {
Get-Process
Get-Service
}
若要檢查連線到 JEA 會話時可使用哪些命令,請執行 Get-Command 並逐一查看結果,以檢查允許的參數。
$commandParameters = @{
ComputerName = 'SERVER01'
ConfigurationName = 'JEAMaintenance'
ScriptBlock = { Get-Command }
}
Invoke-Command @commandParameters |
Where-Object { $_.CommandType -in @('Function', 'Cmdlet') } |
Format-Table Name, Parameters
如果您要建置 C# 應用程式,您可以在 WSManConnectionInfo 物件中指定組態名稱,以建立連線到 JEA 會話的 PowerShell Runspace。
// using System.Management.Automation;
var computerName = "SERVER01";
var configName = "JEAMaintenance";
// See https://learn.microsoft.com/dotnet/api/system.management.automation.pscredential
var creds = // create a PSCredential object here
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
false, // Use SSL
computerName, // Computer name
5985, // WSMan Port
"/wsman", // WSMan Path
// Connection URI with config name
string.Format(
CultureInfo.InvariantCulture,
"http://schemas.microsoft.com/powershell/{0}",
configName
),
creds // Credentials
);
// Now, use the connection info to create a runspace where you can run the commands
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
// Open the runspace
runspace.Open();
using (PowerShell ps = PowerShell.Create())
{
// Set the PowerShell object to use the JEA runspace
ps.Runspace = runspace;
// Now you can add and invoke commands
ps.AddCommand("Get-Command");
foreach (var result in ps.Invoke())
{
Console.WriteLine(result);
}
}
// Close the runspace
runspace.Close();
}
搭配 PowerShell Direct 使用 JEA
Windows 10 和 Windows Server 2016 中的 Hyper-V 提供 PowerShell Direct,此功能可讓 Hyper-V 系統管理員使用 PowerShell 管理虛擬機,而不論虛擬機上的網路設定或遠端管理設定為何。
您可以使用PowerShell Direct 搭配 JEA,為 Hyper-V 系統管理員授與 VM 的有限存取權。 如果您失去 VM 的網路連線,而且需要數據中心管理員來修正網路設定,這非常有用。
不需要額外的設定,才能透過PowerShell Direct使用 JEA。 不過,在虛擬機內執行的客體作系統必須是 Windows 10、Windows Server 2016 或更高版本。 Hyper-V 系統管理員可以藉由在 PSRemoting cmdlet 上使用 -VMName 或 -VMId 參數來連線到 JEA 端點:
$sharedParams = @{
ConfigurationName = 'NICMaintenance'
Credential = Get-Credential -UserName 'localhost\JEAformyHoster'
}
# Entering a JEA session using PowerShell Direct when the VM name is unique
Enter-PSSession -VMName 'SQL01' @sharedParams
# Entering a JEA session using PowerShell Direct using VM ids
$vm = Get-VM -VMName 'MyVM' | Select-Object -First 1
Enter-PSSession -VMId $vm.VMId @sharedParams
為了 Hyper-V 系統管理員的使用,建議您建立一個專用的用戶帳戶,並且擁有管理系統所需的最低許可權。 請記住,即使是非特殊許可權的用戶預設也可以登入 Windows 計算機,包括使用不受限制的 PowerShell。 這可讓他們瀏覽檔系統,並深入瞭解您的OS環境。 若要鎖定 Hyper-V 系統管理員,並限制他們只使用 PowerShell Direct 搭配 JEA 存取 VM,您必須拒絕 Hyper-V 系統管理員 JEA 帳戶的本機登入許可權。