-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFTPUpload.ps1
197 lines (159 loc) · 13.7 KB
/
FTPUpload.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
param(
[string][Parameter(Mandatory=$true)]$sourcePath,
[string][Parameter(Mandatory=$true)]$serverName,
[string][Parameter(Mandatory=$true)]$username,
[string][Parameter(Mandatory=$true)]$password,
[string][Parameter(Mandatory=$true)]$remotePath,
[string][Parameter(Mandatory=$true)]$useBinary,
[string][Parameter(Mandatory=$true)]$ignoreUnchangedFiles,
[string]$excludeFilter,
[string][Parameter(Mandatory=$true)]$deleteOldFiles,
[string][Parameter(Mandatory=$true)]$deploymentFilesOnly
)
Write-Verbose "Starting FTPUpload Script" -Verbose
Write-Verbose "sourcePath = $sourcePath" -Verbose
Write-Verbose "username = $username" -Verbose
Write-Verbose "serverName = $serverName" -Verbose
Write-Verbose "remotePath = $remotePath" -Verbose
Write-Verbose "useBinary = $useBinary" -Verbose
Write-Verbose "ignoreUnchangedFiles = $ignoreUnchangedFiles" -Verbose
Write-Verbose "deleteOldFiles = $deleteOldFiles" -Verbose
Write-Verbose "deploymentFilesOnly = $deploymentFilesOnly" -Verbose
try {
if ([string]::IsNullOrEmpty($sourcePath) -eq $true) {
Throw "Mandatory parameter sourcePath is missing. Script halted."
}
if ([string]::IsNullOrEmpty($serverName) -eq $true) {
Throw "Mandatory parameter serverName is missing. Script halted."
} else {
if ($serverName.StartsWith("ftp://") -eq $false) {
$serverName = "ftp://" + $serverName
}
}
if ([string]::IsNullOrEmpty($remotePath) -eq $true) {
Throw "Mandatory parameter remotePath is empty or missing. Script halted."
} else {
if ($remotePath.EndsWith("/") -eq $false) {
# Make sure that remote path ends with slash.
$remotePath = $remotePath + "/"
}
}
} catch {
Throw
}
Import-Module -force "$PSScriptRoot\library\PSFTP.psm1"
function IsDeploymentFile($name) {
if ($name.ToLower().StartsWith('/obj/') -eq $true) {
return $false
}
if ($name.ToLower().StartsWith('/my project/') -eq $true) {
return $false
}
# Retrieving the filee xstension
$fileExtension = [System.IO.Path]::GetExtension($name).toLower()
# Checking to see if known extensions are found.
if ($fileExtension -eq '.vb') { return $false }
if ($fileExtension -eq '.cs') { return $false }
if ($fileExtension -eq '.vbproj') { return $false }
if ($fileExtension -eq '.csproj') { return $false }
if ($fileExtension -eq '.user') { return $false }
if ($fileExtension -eq '.vspscc') { return $false }
# Extension was not excluded.
return $true
}
$Session = $null
if ([string]::IsNullOrEmpty($username) -eq $false) {
#Encrypts password
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$creds = new-object System.Management.Automation.PSCredential($username, $secpasswd)
# Sets the ftp connection and creates a session
if ($useBinary -eq $true) {
Set-FTPConnection -Credentials $creds -Server $serverName -Session DefaultFTPSession -UsePassive -UseBinary -ignoreCert -KeepAlive
} else {
Set-FTPConnection -Credentials $creds -Server $serverName -Session DefaultFTPSession -UsePassive -ignoreCert -KeepAlive
}
$Session = Get-FTPConnection -Session DefaultFTPSession
} else {
if ($useBinary -eq $true) {
Set-FTPConnection -Server $serverName -Session DefaultFTPSession -UsePassive -UseBinary -ignoreCert -KeepAlive
} else {
Set-FTPConnection -Server $serverName -Session DefaultFTPSession -UsePassive -ignoreCert -KeepAlive
}
$Session = Get-FTPConnection -Session DefaultFTPSession
}
# If flag is set - old destination files are deleted.
if ($deleteOldFiles -eq $true) {
#Remove-FTPDirectory -path $remotePath -Session $Session
Remove-FTPItem -Session $Session -Path $remotePath -Recurse
Add-FTPDirectory -path $remotePath
}
Write-Host "Creating directories..."
# Makes sure that the destination folder has all folders created in the correct structure.
# All folders are created.
foreach($dir in (Get-ChildItem -Recurse -path "$sourcePath" | ?{ $_.PSIsContainer })) {
$directory = $dir.FullName.Substring($sourcePath.Length)
$directory = [regex]::Replace($directory, '\\', '/')
if ($deploymentFilesOnly -eq $true) {
if (IsDeploymentFile($directory) -eq $true) {
Write-Host $directory
if ($deleteOldFiles -eq $true) {
Add-FTPDirectory -path $remotePath -NewFolder $directory
} else {
Add-FTPDirectory -path $remotePath -NewFolder $directory -SuppressErrors $true
}
}
} else {
Write-Host $directory
if ($deleteOldFiles -eq $true) {
Add-FTPDirectory -path $remotePath -NewFolder $directory
} else {
Add-FTPDirectory -path $remotePath -NewFolder $directory -SuppressErrors $true
}
}
}
$fileList = $null
# Finding files that needs to be uploaded.
$excludeArray = $excludeFilter -replace "'","" -split ","
if ([String]::IsNullOrEmpty($excludeFilter) -eq $true) {
$fileList = (Get-ChildItem -path "$sourcePath" -Recurse | ? { !$_.PSIsContainer })
} else {
$fileList = (Get-ChildItem -path "$sourcePath" -Recurse -Exclude $excludeArray | ? { !$_.PSIsContainer })
}
Write-Host "Uploading files..."
# All files are uploaded.
if ($ignoreUnchangedFiles -eq $true) {
Write-Host "Ignoring unchanged files..."
# All files are uploaded.
foreach ($item in $fileList) {
$filename = $item.FullName.Substring($sourcePath.Length)
$filename = [regex]::Replace($filename, '\\', '/')
# call for deployment files validation
if ($deploymentFilesOnly -eq $true) {
if ((Is-Deployment-File -name $filename) -eq $false) {
continue
}
}
# call for changed files validation
if ($ignoreUnchangedFiles -eq $true) {
if ((Has-Changes -Path $remotePath -File $item -Filename $filename) -eq $false) {
continue
}
}
# upload the file
Upload-File -Path $remotePath -File $item -Filename $filename
}
} else {
foreach($item in $fileList){
$filename = $item.FullName.Substring($sourcePath.Length)
$filename = [regex]::Replace($filename, '\\', '/')
if ($deploymentFilesOnly -eq $true) {
if (IsDeploymentFile($filename) -eq $true) {
Write-Host $filename
Add-FTPItem -Path $remotePath -LocalPath $item.FullName -Overwrite -RemoteFileName $filename
}
} else {
Write-Host $filename
Add-FTPItem -Path $remotePath -LocalPath $item.FullName -Overwrite -RemoteFileName $filename
}
}
}