-
Notifications
You must be signed in to change notification settings - Fork 14
/
Push-FTP.ps1
123 lines (110 loc) · 9.34 KB
/
Push-FTP.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
function Push-FTP
{
<#
.Synopsis
Pushes items to an FTP server
.Description
Pushes a directory to an FTP server
.Example
Push-Ftp -Path c:\Example -Include *.aspx
.Link
Get-FTP
#>
[OutputType([Nullable])]
param(
# The local path
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
[string]$Path,
# If set, will only include items that match the wildcard
[string[]]$Include,
# If set, will exclude items that match the wildcard
[string[]]$Exclude,
# The FTP server
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
[Uri]$FTP,
# The credentail used to connect to the FTP server
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
[Management.Automation.PSCredential]$Credential
)
begin {
$progressId = Get-Random
}
process {
$webClient = New-Object Net.Webclient
$files = Get-ChildItem $path -Recurse
#region Filter the file list
$remainingFiles = foreach ( $item in $files) {
if ($include) {
$found = $false
$found =
foreach ($i in $include) {
if ($item.Fullname -ilike "$i") {
$true
break
}
}
if (-not $found) {
continue
}
}
if ($exclude) {
$found = $false
$found =
foreach ($ex in $exclude) {
if ($item.Fullname -ilike "$ex") {
$true
break
}
}
if ($found) {
continue
}
}
$relativePath = $item.Fullname -ireplace $path.Replace("\","\\").Replace(":", "\:"),""
if ($item.PsIsContainer) {
# Pre-create the directory structure. To do this, we need to use the direct FTP methods
Write-Progress "Creating Directories" $item.Fullname -Id $progressId
$relativePath = $relativePath -replace "\\", "/"
$url = "$ftp".TrimEnd("/") + $relativePath
$url = $url.TrimEnd("/") + "/"
$ftpRequest = [Net.WebRequest]::Create($url)
$ftpRequest.Credentials = New-Object Net.NetworkCredential $Credential.Username, $Credential.GetNetworkCredential().Password
$ftpRequest.Method = [Net.WebRequestMethods+Ftp]::MakeDirectory
try {
$ftpresult = $ftpRequest.GetResponse()
$null = $ftpresult
} catch {
Write-Verbose "Error creating directory $ftp - $($item.Fullname)"
}
} else {
$item
}
}
#endregion Filter the file list
$c = 0
#region Upload the files via FTP
foreach ($f in $remainingFiles) {
$item = $f
$relativePath = $f.Fullname.Replace($path,"")
$perc = $c * 100 /@($remainingFiles).Count
$c++
Write-Progress "Uploading Files" $item.Fullname -PercentComplete $perc -Id $progressId
# Upload the file
$relativePath = $relativePath -replace "\\", "/"
$domainSubpath = $Credential.GetNetworkCredential().Domain
$domainSubpath = "$ftp".Substring(("$ftp".IndexOf($domainSubpath) + $domainSubpath.Length))
$domainSubpath = $domainSubpath.TrimEnd("/") + $relativePath
$url = "ftp://$($Credential.GetNetworkCredential().Username):$($Credential.GetNetworkCredential().Password)@$($Credential.GetNetworkCredential().Domain)$domainSubpath"
try {
# Net.Webclient has the most clean way to actually upload files, regardless of strangeness in the underyling server.
$webClient.UploadFile($url, "$($item.Fullname)")
} catch{
Write-Verbose "Error uploading $($item.Fullname) to $domainSubpath"
}
}
#endregion Upload the files via FTP
}
end {
Write-Progress "Uploading Files" "Completed" -Completed -Id $progressId
}
}