hero_powershell

With the arrival of Windows Server 2016 and Windows 10, Microsoft has introduced a new cool feature that is very interesting for anyone who wants to do remote tasks on virtual machines. PowerShell Direct allows IT admins to run PowerShell sessions inside a virtual machine from Hyper-V in a direct mode, without networking or similar. This means you can invoke a script even if the network is not available, the hosts are separated from server/clients or there’s no way to open a remote session of PowerShell due to security policies. For this reason, PowerShell Direct is a “good to know” option.

Requirements

To use PowerShell Direct is necessary to check these requirements:

  • The Virtual Machine must running Windows 10 or Windows Server 2016
  • The Virtual Machine must running in the same host that you want to use to open the session
  • The Virtual Machine must have at least a valid user profile configured
  • The session must be opened as local admin of Hyper-V host
  • It’s necessary to insert valid credentials for Virtual Machine

Create a Remote Session

To open a new remote session, use the command Enter-PSSession -VMName yourvm – as shown in figure 1.

2019_01_03_directps-01

Figure 1 – New PS Session

Once you are inside the session, you are able to work on the virtual machine and run PowerShell cmdlets in the same way as you would locally.  This means that you can use the PS Modules installed; as shown in figure 2 or we can use docker commands without any issues, unless the role is not present on the Hyper-V host.

2019_01_03_directps-02

Figure 2 – Remote Commands

Note: Keep in mind that all the connections based on Enter-PSSession are temporary and this means that if you close the window, it will be necessary to enter the credentials again.

Run Complex Commands

The classic connection can be used to run easy and simple tasks, while if you need to execute advanced scripts, it’s possible use the Invoke-Command. This cmdlet is perfect for running a complex script stored in a repository (locally or remotely). It’s able to be used two ways:

  • Command: Invoke-Command -VMName nomevm -ScriptBlock { Get-Service }
  • Script: Invoke-Command -VMName nomevm -FilePath “C:\hyperv-folder\script.ps1”

2019_01_03_directps-03

Figure 3 – Invoke Command

Copy Files

Not only can you run commands or scripts, but you can also transfer files from the host to the virtual machine. This can be achieved with the “Copy-Item” cmdlet,  but it is required to open a persistent session in order to avoid interruption in case you close the window.  The goal is to use the same session every time without re-inserting the credentials. As is this example:

$VM = New-PSSession -VMName nomevm -Credential (Get-Credential)

Copy-Item -ToSession $VM -Path C:\hyperv-folder\app.exe -Destination C:\guest-vm\

2019_01_03_directps-04

Figure 4 – File Copy

The performance behind this task is very interesting because thanks to the VMBus, we can link all the drivers and the layers to allow the communication. The result is a high speed copy process, but don’t forget to consider what kind of files you want to transfer and also the VM performance (a .vhdx file located into an SSD is much faster than a file located on a HDD).

Conclusion

PowerShell Direct allows IT admins to execute and automate the management tasks without the requirement to interact with operating system via GUI.

(Italian, French, German, Spanish, Portuguese (Brazil))



PowerShell Direct: a new way to manage your Hyper-V VMs
Iperius Team
*****************************************

PLEASE NOTE: if you need technical support or have any sales or technical question, don't use comments. Instead open a TICKET here: https://support.iperius.net

*****************************************

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*****************************************

PLEASE NOTE: if you need technical support or have any sales or technical question, don't use comments. Instead open a TICKET here: https://support.iperius.net

*****************************************