PowerShell Direct: a new way to manage your VMs

With the arrive of Windows Server 2016, and Windows 10, Microsoft has introduced a new cool feature, very interested for everyone that want do remote tasks with virtual machines. PowerShell Direct allows IT admins to run PowerShell session inside a virtual machine from Hyper-V in a direct mode, without networking or similar. This means invoke a script also if the network is not available; in same cases the hosts are separated from server/clients or there’s no way to open a remote session of PowerShell due security policies. For this reason PowerShell Direct it’s a “good to know”.

Requirements

To use PowerShell Direct is necessary 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 use to open the session
  • The Virtual Machine must have at least a valid user profile configured
  • The session must opened as local admin of Hyper-V host
  • It’s necessary insert valid credentials for Virtual Machine

Create a Remote Session

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

Figure 1 – New PS Session

Once you are inside the session, you are able to work into the virtual machine and run PowerShell cmdlets in the same way of local, this means that you can use the PS Modules installed; as showed in figure 2, we can use docker commands without problem although the role is not present into Hyper-V host.

Figure 2 – Remote Commands

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

Run Complex Commands

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

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

Figure 3 – Invoke Command

Copy Files

Not only command or script but also a way to transfer files from host to the virtual machine. This can be achive with cmdlet Copy-Item but is required open a persistent session in order to avoid interruption in case you close the window; the goal is use the same session every time without re-insert the credentials. This is an example:

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

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

Figure 4 – File Copy

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

Veeam B&R and PowerShell Direct

To increase performance and security, Veeam Backup & Replication use a component called Guest Interaction Proxy, that provides these tasks:

  • Application-aware processing
  • Guest file system indexing
  • Transaction logs processing

The process is execute and called in each VM and allows the communication also if the environment are separated.

Figure 5 – Guest Interaction Proxy

Bear in mind that this component is available only for license Enterprise and Enterprise Plus. If you are interested to learn more about GIP, check this article:  https://helpcenter.veeam.com/docs/backup/hyperv/guest_interaction_proxy.html?ver=95. As I said previously, PowerShell Direct can resolve the network issues and this is a great point in case the communication between host and virtual machine is missing. Don’t forget the requirements to use the component.

Figura 6 – PS Direct in Backup & Replication

Conclusion

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

Do you like the article? Post it via social!