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

[玩转系统] 使用 JEA

作者:精品下载站 日期:2024-12-14 02:58:42 浏览:13 分类:玩电脑

使用 JEA


本文介绍了连接和使用 JEA 端点的各种方法。

交互使用 JEA

如果您正在测试 JEA 配置或为用户执行简单的任务,则可以像使用常规 PowerShell 远程会话一样使用 JEA。对于复杂的远程处理任务,建议使用隐式远程处理。隐式远程处理允许用户在本地操作数据对象。

要以交互方式使用 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 具有隐式远程处理模型,可让您从远程计算机导入代理 cmdlet 并与它们交互,就像它们是本地命令一样。这篇嘿,脚本专家!博客文章中解释了隐式远程处理。使用 JEA 时,隐式远程处理非常有用,因为它允许您在完整语言模式下使用 JEA cmdlet。您可以使用制表符补全、变量、操作对象,甚至使用本地脚本针对 JEA 端点自动执行任务。每当您调用代理命令时,数据都会发送到远程计算机上的 JEA 端点并在那里执行。

隐式远程处理通过从现有 PowerShell 会话导入 cmdlet 来工作。您可以选择使用您选择的字符串作为每个代理 cmdlet 的名词前缀。该前缀允许您区分用于远程系统的命令。将在本地 PowerShell 会话期间创建并导入包含所有代理命令的临时脚本模块。

# 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 cmdlet 的限制,某些系统可能无法导入整个 JEA 会话。要解决此问题,只需通过向 -CommandName 参数显式提供命令名称即可从 JEA 会话导入所需的命令。未来的更新将解决在受影响的系统上导入整个 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 保留来自隐式远程处理的代理 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 运行空间。

// 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();
}

将 JEA 与 PowerShell Direct 结合使用

Windows 10 和 Windows Server 2016 中的 Hyper-V 提供 PowerShell Direct,该功能允许 Hyper-V 管理员使用 PowerShell 管理虚拟机,无论虚拟机上的网络配置或远程管理设置如何。

您可以将 PowerShell Direct 与 JEA 结合使用,为 Hyper-V 管理员提供对 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。这使他们能够浏览文件系统并了解有关您的操作系统环境的更多信息。要锁定 Hyper-V 管理员并限制他们只能使用 PowerShell Direct 和 JEA 访问 VM,您必须拒绝 Hyper-V 管理员的 JEA 帐户的本地登录权限。

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

取消回复欢迎 发表评论:

关灯