How can use Powershell Direct in HYPER-V

Windows Server 2016 and Windows 10 has a new feature in Hyper-v that is called Powershell Direct.

As IT Pro maybe has use it but let's take a look and refresh what is Powershell Direct and how can use it.

Powershell Direct it's a new way to run Powershell in Virtual Machines without need any network connectivity.

Before Powershell Direct we used to connect in Virtual Machines with the following tools

  • Remote Management tools like Powershell or Remote Desktop
  • HYPER-V Virtual Machine Connection.

Still continue to use this tools base on the requests but the above tools has the disadvantage that are base on network connectivity.

The advantage of powershell direct is that  use the Vmbus from Hyper-v to connect in Virtual Machines instead of wsman.

So when a network issue occurs or a firewall is misconfigured you can continue to manage the Virtual Machine for troubleshooting or any other configuration .

Another one reason to use powerhell direct is when you would like to use scripts in VM without network connectivity. Of course you can use Management Tools but lot of IT doing all staff with powershell .

Also in some cases maybe you don't have the ability of Management Tools.

Let's continue with some scenarios instead of words.

Requirements for Powershell Direct

As every feature and the Powershell Direct has specific requirements before use it which you can find it here:

  • The host operating system must run Windows Server 2016 or Windows 10 or higher version
  • The virtual machine must run Windows Server 2016 or Windows 10 or higher version.
  • The virtual Machine must be started and running locally in Hyper-v.
  • You must be logged into the Hyper-v as Administrator
  • The VM configuration version must be at least 8.0 in case of migration from Windows Server 2012.
  • Must be login as Local Administrator in VM 

How to connect with Powershell Direct

First of all to use Powershell Direct the relevant commands are:

  • Get-PSSession
  • Enter-PSSession
  • New-PSSession
  • Remove-PSSession
  • Invoke-Command

Let's see how can connect with Powershell Direct in VM without network connectivity

  • Open Hyper-v Manager and select a VM.
  • In my scenario i select the WSUS Virtual Machine.
  • Select your VM from your Hyper-v
  • Open 2 Powershell consoles.
  • In the first start a ping in the server with -t for continuous ping

ping wsus -t

 

  • Connect remotely in the VM and disable the Network Adapter.

 

  • Now open the Powershell and use the Enter-PSSession to open a Session in the VM/
  • As you can see i have connected successful and you can identify from the server name [WSUS]: in the beginning of the line.

Enter-PSSession -VMName wsus

 

  • Now type Get-NetAdapter to identify the network adapters and the status of them.
  • As we can see the status of the network adapter is disable.

 

  • Now type Enable-Networkadapter -Name Ethernet
  • Once again type Get-Netadapter.
  • The Status now is up.
  • Also if check the ping you will see that reply successful now.

 

With this example i hope to give you understand how can use the Powershell Direct.

 

How to copy files with Powershell Direct

Another one useful command that can use with Powershell Direct is the CopyItem.

That means that you can copy files in the VM from HYPER-V without need network connectivity.

Let's see it in action

  • Now we will use the new-pssession command because must save it in a variable and use it with Copy-Item
  • Type the following 2 commands. 
  • Change only the source and the destination from the Copy-Item

$s=new-pssession -VMName WSUS -Credential (Get-Credential)
Copy-Item C:\test.txt c:\target -ToSession $s

 

  • Go now in your VM and find the filename that you have copy.

 

I hope to help you understand how can use Powershell Direct if you don't have use it or refresh your knowledge if you don't use it very often.

If you would like more details you can read in Microsoft Docs .

I invite you to follow me on Twitter or Facebook. If you have any questions, send email at info@askme4tech.com

Disqus Comments