How To Copy Files Between the Guest and the Host in #HyperV With PowerShell Direct?

POSh-DIRECTV2-01.png

Introduction

In Windows Server 2012 R2, Hyper-V has a nifty feature of the Integration Components / Integration Services that allows you to inject/copy files into a running VM.
The PowerShell cmdlet to do so is Copy-VMFile.

Before you start using this cmdlet, you must have Guest Services enabled on your VM (this is not on by default).

To enable Guest Services, you can run the following cmdlet:

After you enable the guest service, you can start pushing files into a VM from the Hyper-V Server by using –FileSource Host parameter.

image

If you noticed the –FileSource parameter has only Host as an option. By adding the –CreateFullPath parameter, it creates the folder path you defined if it is not already present, and lastly if you use -Force parameter, you can overwrite an existing file.

However, Copy-VMFile allows you to push files into the Guest VM only and not vice versa.

The workaround to get files out of a running virtual machine is fully documented by Mr. Hyper-V here.
You need to create a checkpoint (aka snapshot) and mount the VHD(X). However, this option is a bit tricky, because you have to wait a little bit for the flushing of the last writes to VHD(X) file before starting the Checkpoint process – when you just wrote the file to obtain within the VM.

Introducing New PowerShell Direct Features

In Windows 10 and Windows Server 2016 Technical Preview 2, the Hyper-V team introduced a new feature called PowerShell Direct.

In short, PowerShell Direct allows you to remotely manage a Windows 10 or Windows Server 2016 Technical Preview virtual machine from a Windows 10 or Windows Server 2016 Technical Preview Hyper-V host without any network connectivity.

For more information, please refer to the following post.

The big news is that starting with Windows 10 and Windows Server 2016 Build #14280 or later… The Hyper-V team went one step further and added a new functionality into PowerShell Direct.

You can now move data between the virtual machine and the Hyper-V host with persistent PowerShell Direct sessions!!!

In order to do so, we need to use two cmdlets (New-PSSession and Copy-Item).

The New-PSSession cmdlet has a new parameter now: –VMName

We have now three cmdlets that can be used with PowerShell Direct:

    • New-PSSession –VMName <VMName> / –VMGUID <VMGUID> / –VMId <VMId>
    • Enter-PSSession –VMName <VMName> / –VMGUID <VMGUID> / –VMId <VMId>
    • Invoke-Command –VMName <VMName> / –VMGUID <VMGUID> / –VMId <VMId>

Let’s see now in action how to move files IN and OUT from a Virtual Machine.

POSh-DIRECTV2-02

Thanks to the Hyper-V team for delivering value added services and capabilities on an ongoing basis!

Cheers,
-Charbel

About Charbel Nemnom 271 Articles
Charbel Nemnom is a Microsoft Cloud Consultant and Technical Evangelist, totally fan of the latest's IT platform solutions, accomplished hands-on technical professional with over 15 years of broad IT Infrastructure experience serving on and guiding technical teams to optimize performance of mission-critical enterprise systems. Excellent communicator adept at identifying business needs and bridging the gap between functional groups and technology to foster targeted and innovative IT project development. Well respected by peers through demonstrating passion for technology and performance improvement. Extensive practical knowledge of complex systems builds, network design and virtualization.

Be the first to comment

Leave a Reply