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:
Enable-VMIntegrationService -Name 'Guest Service Interface' –VMName <VMName>
After you enable the guest service, you can start pushing files into a VM from the Hyper-V Server by using –FileSource Host parameter.
Copy-VMFile -Name DEMO01 -SourcePath .\HyperV-isAwesome.zip –DestinationPath 'C:\VMCopyFiles\HyperV-isAwesome.zip' -FileSource Host –CreateFullPath
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 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!!!
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.
$VM = "POSH-DIRECT" $S = New-PSSession -VMName $VM -Credential "~\Administrator" Enter-PSSession -Session $S [POSH-DIRECT]: PS C:\Users\Administrator\Documents> Get-ChildItem D:\Guest\ [POSH-DIRECT]: PS C:\Users\Administrator\Documents> Exit Copy-Item -ToSession $S -Path C:\Host\PowerShell-Direct-IsAwesome.zip -Destination D:\Guest\ Enter-PSSession -Session $S [POSH-DIRECT]: PS C:\Users\Administrator\Documents> Get-ChildItem D:\Guest\ Directory: D:\Guest Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 4/21/2016 12:27 PM 22 PowerShell-Direct-IsAwesome.zip [POSH-DIRECT]: PS C:\Users\Administrator\Documents> Exit Remove-Item C:\Host\PowerShell-Direct-IsAwesome.zip -Confirm:$false Get-ChildItem C:\Host\ Copy-Item -FromSession $S -Path D:\Guest\PowerShell-Direct-IsAwesome.zip -Destination C:\Host Get-ChildItem C:\Host\ Directory: C:\Host Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 4/21/2016 12:27 PM 22 PowerShell-Direct-IsAwesome.zip
Thanks to the Hyper-V team for delivering value added services and capabilities on an ongoing basis!