使用 PowerShell Direct 管理 Windows 虚拟机Manage Windows virtual machines with PowerShell Direct

适用于:Windows 10,Windows Server 2016 中,Windows Server 2019Applies To: Windows 10, Windows Server 2016, Windows Server 2019

可以使用 PowerShell Direct 远程管理 Windows 10、 Windows Server 2016 或 Windows Server 2019 虚拟机,从 Windows 10、 Windows Server 2016 或 Windows Server 2019 HYPER-V 主机。You can use PowerShell Direct to remotely manage a Windows 10, Windows Server 2016, or Windows Server 2019 virtual machine from a Windows 10, Windows Server 2016, or Windows Server 2019 Hyper-V host. PowerShell Direct 都允许 Windows PowerShell 管理网络配置或远程管理的 HYPER-V 主机上的设置如何或虚拟机。PowerShell Direct allows Windows PowerShell management inside a virtual machine regardless of the network configuration or remote management settings on either the Hyper-V host or the virtual machine. 这使得 Hyper-V 管理员能够更简单地自动化虚拟机管理和配置,并为其编写脚本。This makes it easier for Hyper-V Administrators to automate and script virtual machine management and configuration.

可通过两种方法运行 PowerShell Direct:There are two ways to run PowerShell Direct:

  • 创建和退出 PowerShell Direct 会话使用 PSSession cmdletCreate and exit a PowerShell Direct session using PSSession cmdlets

  • 使用 Invoke-command cmdlet 运行脚本或命令Run script or command with the Invoke-Command cmdlet

如果要管理较旧的虚拟机,请使用虚拟机连接 (VMConnect) 或为虚拟机配置虚拟网络If you're managing older virtual machines, use Virtual Machine Connection (VMConnect) or configure a virtual network for the virtual machine.

创建和退出 PowerShell Direct 会话使用 PSSession cmdletCreate and exit a PowerShell Direct session using PSSession cmdlets

  1. 在 Hyper-V 主机上,以管理员身份打开 Windows PowerShell。On the Hyper-V host, open Windows PowerShell as Administrator.

  2. 使用Enter-pssession cmdlet 连接到虚拟机。Use the Enter-PSSession cmdlet to connect to the virtual machine. 运行以下命令以创建会话使用的虚拟机名称或 GUID 之一:Run one of the following commands to create a session by using the virtual machine name or GUID:

    Enter-PSSession -VMName <VMName>  
    
    Enter-PSSession -VMGUID <VMGUID>  
    
  3. 键入虚拟机的凭据。Type your credentials for the virtual machine.

  4. 运行所需的任意命令。Run whatever commands you need to. 这些命令在与其创建了会话的虚拟机上运行。These commands run on the virtual machine that you created the session with.

  5. 完成后,使用Exit-pssession关闭会话。When you're done, use the Exit-PSSession to close the session.

    Exit-PSSession  
    

使用 Invoke-command cmdlet 运行脚本或命令Run script or command with Invoke-Command cmdlet

你可以使用 Invoke-Command cmdlet 在虚拟机上运行一组预先确定的命令。You can use the Invoke-Command cmdlet to run a pre-determined set of commands on the virtual machine. 下面是如何使用 Invoke-Command cmdlet 的示例,其中 PSTest 是虚拟机名称,而要运行的脚本 (foo.ps1) 位于 C:/ 驱动器上的脚本文件夹中:Here is an example of how you can use the Invoke-Command cmdlet where PSTest is the virtual machine name and the script to run (foo.ps1) is in the script folder on the C:/ drive:

Invoke-Command -VMName PSTest  -FilePath C:\script\foo.ps1  

若要运行单个命令,请使用 -ScriptBlock 参数:To run a single command, use the -ScriptBlock parameter:

Invoke-Command -VMName PSTest  -ScriptBlock { cmdlet }  

所需使用 PowerShell Direct?What's required to use PowerShell Direct?

若要在虚拟机上创建 PowerShell Direct 会话,To create a PowerShell Direct session on a virtual machine,

  • 虚拟机必须在主机上本地运行并已启动。The virtual machine must be running locally on the host and booted.

  • 必须以 Hyper-V 管理员身份登录主机计算机。You must be logged into the host computer as a Hyper-V administrator.

  • 必须为虚拟机提供有效用户凭据。You must supply valid user credentials for the virtual machine.

  • 主机操作系统必须至少运行 Windows 10 或 Windows Server 2016。The host operating system must run at least Windows 10 or Windows Server 2016.

  • 虚拟机必须至少运行 Windows 10 或 Windows Server 2016。The virtual machine must run at least Windows 10 or Windows Server 2016.

可以使用GET-VM cmdlet 检查是否正在使用的凭据具有 HYPER-V 管理员角色并获取一系列虚拟机在主机上本地运行并已启动。You can use the Get-VM cmdlet to check that the credentials you're using have the Hyper-V administrator role and to get a list of the virtual machines running locally on the host and booted.

请参阅See Also

Enter-PSSessionEnter-PSSession
Exit-PSSessionExit-PSSession
Invoke-CommandInvoke-Command