Skip to content

Copy-GuestFile

VMware: Copies files and folders from and to the guest OS

#Requires -Version 5.1
#Requires -Modules VMware.VimAutomation.Core
[CmdletBinding(DefaultParameterSetName = "LocalToGuest")]
Param(
    [Parameter(Mandatory = $true, ParameterSetName = "LocalToGuest")]
    [Parameter(Mandatory = $true, ParameterSetName = "GuestToLocal")]
    [string]$VIServer,
    [Parameter(Mandatory = $true, ParameterSetName = "LocalToGuest")]
    [Parameter(Mandatory = $true, ParameterSetName = "GuestToLocal")]
    [pscredential]$VICredential,
    [Parameter(Mandatory = $true, ParameterSetName = "LocalToGuest")]
    [Parameter(Mandatory = $true, ParameterSetName = "GuestToLocal")]
    [string]$VMName,
    [Parameter(Mandatory = $true, ParameterSetName = "LocalToGuest")]
    [Parameter(Mandatory = $true, ParameterSetName = "GuestToLocal")]
    [string]$Source,
    [Parameter(Mandatory = $true, ParameterSetName = "LocalToGuest")]
    [Parameter(Mandatory = $true, ParameterSetName = "GuestToLocal")]
    [string]$Destination,
    [int]$ToolsWaitSecs = 300,
    [pscredential]$HostCredential,
    [pscredential]$GuestCredential,
    [string]$HostUser,
    [string]$GuestUser,
    [securestring]$HostPassword,
    [securestring]$GuestPassword
)
Process {
    $vmServer = $null
    try {
        $vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop
        $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        $cmdArgs = @{ ErrorAction = 'Stop'; Server = $vmServer; Source = $Source; Destination = $Destination; Confirm = $false; Force = $null; ToolsWaitSecs = $ToolsWaitSecs }
        if ($PSCmdlet.ParameterSetName -eq "LocalToGuest") { $cmdArgs.Add("LocalToGuest", $null) }
        else { $cmdArgs.Add("GuestToLocal", $null) }
        $vm = Get-VM -Server $vmServer -Name $VMName -ErrorAction Stop
        $cmdArgs.Add('VM', $vm)
        if ($PSBoundParameters.ContainsKey('HostCredential')) { $cmdArgs.Add('HostCredential', $HostCredential) }
        if ($PSBoundParameters.ContainsKey('HostPassword')) { $cmdArgs.Add('HostPassword', $HostPassword) }
        if ($PSBoundParameters.ContainsKey('HostUser')) { $cmdArgs.Add('HostUser', $HostUser) }
        if ($PSBoundParameters.ContainsKey('GuestCredential')) { $cmdArgs.Add('GuestCredential', $GuestCredential) }
        if ($PSBoundParameters.ContainsKey('GuestUser')) { $cmdArgs.Add('GuestUser', $GuestUser) }
        if ($PSBoundParameters.ContainsKey('GuestPassword')) { $cmdArgs.Add('GuestPassword', $GuestPassword) }
        $result = Copy-VMGuestFile @cmdArgs
        $output = [PSCustomObject]@{
            Timestamp = $timestamp
            Status    = "Success"
            Message   = "File copied from $Source to $Destination"
        }
        Write-Output $output
    }
    catch { throw }
    finally { if ($null -ne $vmServer) { Disconnect-VIServer -Server $vmServer -Force -Confirm:$false -ErrorAction SilentlyContinue } }
}

IP address or DNS name of the vSphere server

PSCredential object for authenticating with the server

Name of the virtual machine

Source file path (absolute for guest, relative supported for local)

Destination path (absolute for guest, relative supported for local)

Time in seconds to wait for VMware Tools response

PSCredential for authenticating with the host

PSCredential for authenticating with the guest OS

User name for authenticating with the host

User name for authenticating with the guest OS

Password for authenticating with the host

Password for authenticating with the guest OS

An interactive directory of PowerShell scripts.