functions/Copy-RemoteFile.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function Copy-RemoteFile {
    [CmdletBinding()]
    param($source, $target, $tosession, [switch][bool]$recurse, [switch][bool]$force) 
    
    if ((get-item $source).psiscontainer) {
        $recurse = $true    
    }
    if ($recurse -and (get-item $source).psiscontainer) {
        $force = $true
    }
    if (!$recurse -and ($target.EndsWith("/") -or $target.EndsWith("\"))) {
        $target = join-path $target (split-path -leaf $source)
    }
    $hash = $null
    if (!$recurse) { 
        $hash = @(@{
            Hash = (Get-FileHash $source).Hash
            FullName = $source.fullname
            RelativePath = $source
        })
    }
    else {
        $hash = get-childitem $source -Recurse -File | % {
            @{ 
                Hash = (get-filehash $_.FullName).Hash
                FullName = $_.fullname
                RelativePath = $_.FullName.Replace($source, "")
            }
        }
    }
    
    $a = @{}
    if ($tosession -ne $null) { $a.Session = $tosession }
    $srvInfo = icm `
    -ArgumentList @(@{
        FullName = $target
        Hash = $hash
        Recurse = $recurse
    }) `
    -ScriptBlock {
        param($p)
        if ($p.recurse) { $dir = $p.fullname } 
        else { $dir = split-path $p.FullName }
        if (!(test-path $dir)) { $null = mkdir $dir }

        if ($p.hash -ne $null -and (Test-Path $p.fullname)) {
            if ($p.psiscontainer) {                
                return get-childitem $source -Recurse -File | % {
                    @{ 
                        Hash = (get-filehash $_.FullName).Hash
                        FullName = $_.fullname
                        RelativePath = $_.FullName.Replace($dir, "")
                    }
                } 
            }   
            else {         
                $srvHash = Get-FileHash $p.fullname
                return @(@{ 
                    Hash = $srvHash.Hash
                    FullName = $p.fullname
                    RelativePath = $p.FullName.Replace($dir, "")
                })
            }            
            
        }
        return @(@{ 
            Hash = $null
            FullName = $p.fullname
        })
    } `
    -ErrorAction Stop @a
    
    $localfiles = $hash
    $remotefiles = $srvInfo
    foreach($f in $localfiles) {
        $srvInfo = $remotefiles | ? { $_.RelativePath -eq $f.RelativePath }
        if ($srvInfo -ne $null) { $srvHash = $srvInfo.Hash }
        $hash = $f.Hash
        $a = @{}
        if ($tosession -ne $null) { $a.ToSession = $tosession }
        if ($srvHash -eq $null -or $srvHash -ne $hash) {
            write-verbose "file hashes differ: local($hash) != remote($srvhash) for file $source -> $($srvinfo.fullname)"
           # if ($recurse) {
           # $source = $source.trimend("/").trimend("\") + "/*"
           # cp -path "$source" -destination $target @a -Recurse:$recurse -Force:$force -Container:$recurse -ErrorAction Stop
           # }
           # else {
                cp -LiteralPath $f.Fullname -destination $target @a -Recurse:$recurse -Force:$force -Container:$recurse -ErrorAction Stop 
           # }
        } else {
            write-verbose "skipping unmodified file $source"
        }
    }
}