- Create a custom role with Azure role-based access control (Azure RBAC) using PowerShell - Wed, Jan 20 2021
- Step by step Deploying Docker Container to Azure using Azure CLI - Wed, Sep 2 2020
- Install Docker offline on Windows Server 2016 - Thu, Dec 6 2018
Using the enhanced session mode
Enhanced session mode lets a virtual machine (VM) connection tool (VMConnect.exe) connect to VMs using Remote Desktop Protocol (RDP). This allows redirecting local devices and resources to guest VMs. Generation 1 and 2 VMs both support it, and no network connectivity is required between the Hyper-V host and the guest VM. Currently Windows Server 2016, Windows Server 2012 R2, Windows 10, and Windows 8.1 support this feature.
Enable Enhanced Session Mode
Enhanced connection mode is enabled by default for a Hyper-V host running on Windows 10 and 8.1. However, Windows Server 2016 and 2012 R2 require enabling it. You can do so manually via Hyper-V Manager, and there is also PowerShell cmdlet to achieve this:
Set-VMHost -EnableEnhancedSessionMode $True
How to transfer files using enhanced session mode
After enabling enhanced session mode, connecting to the VM by right-clicking the VM and clicking Connect presents you with a dialogue box to configure connection settings:
Now click Show Options to open the advanced settings. From the Display tab, check the box Save my settings for future connections to this virtual machine.
Next, select Local Resources and then click More. (Please note that the Printers and Clipboard settings are enabled by default. The clipboard lets you simply copy and paste text or files and folders from the host to guest VMs and vice versa.)
Now select Local Resources from the Hyper-V host and choose the disks or other plug-and-play devices you wish to make available to the VMs, and then click OK. Here I have selected just the E: drive for demonstration purpose; Smart cards is checked by default.
Once you are done with modifying the connection settings, click Connect, and then log in to the VM.
Resources selected in the previous steps are now available to the VM, and you can simply copy data to or from this drive by copying and pasting it.
Using the PowerShell cmdlet Copy-VMFile
To copy files to a VM using the Copy-VMFile cmdlet, we first have to enable Guest Services under Integration Services for a VM. Luckily, there is a PowerShell one-liner for this:
Enable-VMIntegrationService -Name "Guest Service Interface" -VMName "ADDNSDHCP"
Now we are now ready to copy the file from the host (source) to the guest VM (destination) using the following command:
Copy-VMFile -Name "VMName" -SourcePath 'E:\OS\Windows 7\Win7x64 GSP1RMCNENXVOL_EN_DVD.iso' -DestinationPath "C:\OS\Windows7.iso" -FileSource Host CreateFullPath
The CreateFullPath parameter ensures creation of the destination folder if it doesn't exist. Note that the cmdlet can only copy files and not folders. You can work around this by compressing folders to a zipped file.
Mount the virtual hard disk to the host machine
Update: Some readers reported that mounting the virtual disk corrupted the disk.
If you can afford to shut down the VM, copying the file to the VM by mounting the virtual hard drive is the easiest and fastest way. Use the following steps:
- Connect to the VM, log in, and then shut down the VM.
- Navigate to the location where the virtual hard disk for this VM is stored.
- Right-click the virtual disk file and select Mount. It will map the VHDX file as a local disk drive.
You can now copy files to the virtual hard disk. When done copying the data, right-click the mounted disk drive and click Eject to dismount the virtual hard disk.
Now start the VM, and you can use the copied data inside the VM.
Using network share
You can also use network shares or UNC paths to access data. Thus, network connectivity between the Hyper-V host and the guest VM is mandatory. For this to work, you'll need an external virtual switch connected to the VM, and the Hyper-V host and guest VM should be on the same IP subnet.
Creating a virtual switch is simple. Right-click the Hyper-V host and select Virtual Switch Manager.
Next, follow these steps. Select New virtual network switch, select the network type as External, and click Create Virtual Switch.
Give the virtual switch a descriptive name, select the network adapter on the Hyper-V host this switch should connect to, and then click OK.
After creating the external-type virtual switch, connect the VM to this switch. Now the Hyper-V host and the guest VM are like two computers connected to same switch.
Next configure the IP address on both the Hyper-V host and the guest VM. Make sure that the Hyper-V host and the guest VM have IP addresses in the same subnet. For example, if the Hyper-V Host has the assigned IP address 192.168.1.10 with the subnet mask 255.255.255.0, the guest VM should have the IP address 192.168.1.x with the subnet 255.255.255.0.
The Hyper-V host and the guest VM can now communicate. Now create a shared folder on the Hyper-V host and access it from the guest VM using its UNC path.
Subscribe to 4sysops newsletter!
Conclusion
The most popular way for copying files between a Hyper-V host and a guest is using the enhanced session mode. A lesser-known approach is the PowerShell cmdlet Copy-VMFile, mounting a virtual hard disk or using a network share to exchange data between the host and the guest VM.
Hyper-V creates a base virtual hard disk, and then checkpoints (diffs) are created on top of that. Mounting the virtual hard disk and writing to it seems to corrupt checkpoints.
I found the same thing. I mounted the VHD, copied the files, then was unable to start the VM with the error: "The application encountered an error while attempting to change the state of the VM. Checkpoint operation failed. Cannot create the checkpoint."
That really helped
Any solution to this?