Public/Copy-FileToVirtualMachine.ps1
|
<#
.SYNOPSIS Copies a file from the host to a virtual machine. .DESCRIPTION This function uses the Copy-VMFile cmdlet to copy a file from the host machine to a specified virtual machine. You need to call Register-VirtualMachineAliases before using this function to set the WDK VM name and credentials. .PARAMETER SourcePath The path of the file on the host machine that you want to copy to the virtual machine. .PARAMETER DestinationPath The path on the virtual machine where you want to copy the file. .PARAMETER FileSource Specifies the source of the file to copy. This parameter is required and should be set to "Host" to indicate that the file is being copied from the host machine. .EXAMPLE # Register the virtual machine aliases for the WDK VM Register-VirtualMachineAliases -Name "Windows 11 WDK Environment" -Credential (Import-Clixml "${env:USERPROFILE}\.cert\wdkcert.xml") # Copy a file from the host to the WDK VM Copy-FileToVirtualMachine -SourcePath "C:\path\to\file.txt" -DestinationPath "C:\destination\path\file.txt" -FileSource Host .NOTES Requires the Hyper-V PowerShell module. The user must have the necessary permissions to access the virtual machine and copy files. Use the Register-VirtualMachineAliases function to set up the virtual machine aliases before using this function. #> function Copy-FileToVirtualMachine { [CmdletBinding()] param( [Parameter(Mandatory = $true, Position = 1)] [string] $SourcePath, [Parameter(Mandatory = $true, Position = 2)] [string] $DestinationPath, [Parameter(Mandatory = $True, Position = 0)] [Microsoft.HyperV.PowerShell.CopyFileSource] $FileSource ) if (-not $script:VirtualMachineManager) { throw "Virtual Machine aliases are not registered. Run Register-VirtualMachineAliases first." } Copy-VMFile -CreateFullPath -VMName $script:VirtualMachineManager.VMName @PsBoundParameters } |