Misc/Copy-FileToNavContainer.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<#
 .Synopsis
  Copy File to a container
 .Description
  Copies a file to a Container
 .Parameter containerName
  Name of the container to which you want to copy a file
 .Parameter localPath
  Path to the file on the host, which has to be copied
 .Parameter containerPath
  Path of the file in the Container. This cannot be a foldername, it needs to be a filename.
 .Example
  Copy-FileToNavContainer -containerName test2 -localPath "c:\temp\myfile.txt" -containerPath "c:\run\my\myfile.txt"
#>

function Copy-FileToNavContainer {
    Param (
        [Parameter(Mandatory=$true)]
        [string] $containerName,
        [Parameter(Mandatory=$true)]
        [string] $localPath,
        [Parameter(Mandatory=$false)]
        [string] $containerPath = $localPath
    )

    Process {
        if (!(Test-NavContainer -containerName $containerName)) {
            throw "Container $containerName does not exist"
        }
        Log "Copy $localPath to container ${containerName} ($containerPath)"
        $id = Get-NavContainerId -containerName $containerName 

        # running hyperv containers doesn't support docker cp
        $tempFile = Join-Path $containerHelperFolder ([GUID]::NewGuid().ToString())
        try {
            Copy-Item -Path $localPath -Destination $tempFile
            Invoke-ScriptInNavContainer -containerName $containerName -scriptblock { Param($tempFile, $containerPath)
                if (Test-Path $containerPath -PathType Container) {
                    throw "ContainerPath ($containerPath) already exists as a folder. Cannot copy file, ContainerPath needs to specify a filename."
                }
                Move-Item -Path $tempFile -Destination $containerPath -Force
            } -argumentList $tempFile, $containerPath
        } finally {
            if (Test-Path $tempFile) {
                Remove-Item $tempFile -ErrorAction Ignore
            }
        }
    }
}
Set-Alias -Name Copy-FileToBCContainer -Value Copy-FileToNavContainer
Export-ModuleMember -Function Copy-FileToNavContainer -Alias Copy-FileToBCContainer