Manage VMS from a Hyper-V host using PowerShell direct

Source: Internet
Author: User

using PowerShell Direct managing VMS from Hyper-V hosts

With PowerShell Direct, we don't have to worry about network or firewall restrictions because it works regardless of the network or remote management configuration. So, a lot of friends will ask, since it is not through the network and firewall that is how to connect it? In fact PowerShell's connection is via VMBus instead of through the network.

Before PowerShell direct, I used remote PowerShell, Remote Desktop, or Vmconnect to manage virtual machines. Remote PowerShell and Remote Desktop technologies require network connectivity because you need to provide a ip/dns name to connect to the virtual machine, and sometimes the domain security policy and firewall configuration can be a hassle. Neither Remote Desktop nor vmconnect enables you to automate tasks, and remote PowerShell can be a technique for configuration.

With PowerShell Direct, the only thing you need is a guest voucher, a user name password.

There are two ways to use PowerShell Direct:invoke-command or enter-pssession.

1 , using PowerShell direct and Invoke-command

To execute commands within a virtual machine, you can run the following command on the PowerShell console of the Hyper-V host:

Invoke-command-vmname <virtual Machine name>-scriptblock {<commands>}

This command runs an inline script block against the virtual machine. For example, you can run the next command to find the services running within the VM:

Invoke-command-vmname Hyperv-scriptblock {get-service | Where-object {$_. Status-eq ' Running '}}

As you can see in the following screen, when you run the above command, you are asked to provide the credentials of the guest operating system:

After you provide the correct credentials, you will receive a list of services running within the virtual machine:

Note If you provide a credential error, you receive some confusing error messages "provide values for the following parameters-errors that Windows PowerShell cannot handle." The remote session may have ended. “

Tip: If you need to run multiple commands, use Get-credential to store guest credentials:

$VMcred = get-credential

Invoke-command-vmname ' SHAREPOINT-VM '-credential $VMcred-scriptblock {get-service | Where-object {$_. Status-eq ' Running '}}

Invoke-command-vmname ' DC-VM '-filepath ' c:\scripts\checkreplication.ps1 '-credential $VMcred

2 , using enter-pssession directly with PowerShell

The second option is to work in an interactive session with the help of the Enter-pssession cmdlet:

Enter-pssession-vmname <virtual Machine name>

Alternatively, you can run the next command:

Enter-pssession-vmguid <VMGUID>

You must replace <VMGUID> with the GUID of the virtual machine.

You can run any command that is supported by the PowerShell host in the guest operating system. After you complete the administrative task, you must exit the session and use Exit-pssession to perform the session.

Also note that after entering the virtual session, the command prompt will start to display the name of the virtual machine as shown in.

PowerShell Direct Prerequisites

To run PowerShell Direct, you must meet the following prerequisites:

The PowerShell console on the host must be started as an administrator or by using the Hyper-V Admins group account.

Make sure that you have the credentials for the guest operating system.

The host must be running Windows 10 or Windows Server 2016.

VMS must be running Windows 10 or Windows Server 2016 (for example, you will not be able to manage Windows R2 VMs using PowerShell direct).

The virtual machine you are connecting to must be running locally.

The virtual machine must be running.

Conclusion

PowerShell Direct allows us to easily automate tasks within a virtual machine without having to log on to the guest operating system via the GUI. If you only want to execute a single command or script block, Invoke-command is usually the faster option. Interactive sessions enable you to continue to perform administrative tasks based on the output returned by the VM.

Manage VMS from a Hyper-V host using PowerShell direct

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.