-
Notifications
You must be signed in to change notification settings - Fork 10
/
FTPModule.ps1
161 lines (149 loc) · 8.66 KB
/
FTPModule.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
function New-FtpRequest ($sourceUri, $method, $username, $password) {
$ftprequest = [System.Net.FtpWebRequest]::Create($sourceuri)
$ftprequest.Method = $method
$ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)
return $ftprequest
}
function Send-FtpRequest($ftpRequest) {
Write-Host "$($ftpRequest.Method) for '$($ftpRequest.RequestUri)' executing"
$response = $ftprequest.GetResponse()
$closed = $response.Close()
Write-Host "Response: '$($response.StatusDescription)'"
return $response
}
function Parse-Output($output, [System.Management.Automation.SwitchParameter]$file, [System.Management.Automation.SwitchParameter]$directory) {
$entities = @()
foreach ($CurLine in $output) {
$LineTok = ($CurLine -split '\ +')
$currentEntity = $LineTok[8..($LineTok.Length-1)]
if(-not $currentEntity) { continue }
$isDirectory = $LineTok[0].StartsWith("d")
if($file -and -not $isDirectory) {
$entities += $currentEntity
} elseif($directory -and $isDirectory) {
$entities += $currentEntity
}
}
return $entities
}
#Get-FtpChildItemHidden -ftpFolderPath "ftp://myHost.com/root/leaf/" -userName "User" -password "pw" -File -Directory
function Get-FtpChildItemHidden($ftpFolderPath, $userName, $password, [System.Management.Automation.SwitchParameter]$file, [System.Management.Automation.SwitchParameter]$directory) {
$ftpUrl = New-Object System.Uri($ftpFolderPath)
$getHiddenFilesScript = "$env:TMP\$([guid]::NewGuid()).dat"
$outPutEntity = "$env:TMP\$([guid]::NewGuid()).txt"
$commands = @("open $($ftpUrl.Host)", $userName, $password, "cd $($ftpUrl.PathAndQuery)", "ls -la", "bye" )
$commands | foreach {
Add-Content $getHiddenFilesScript -Value $_
}
try {
$old =$ErrorActionPreference
$ErrorActionPreference = "stop"
ftp -s:$getHiddenFilesScript > $outPutEntity
} catch {
if($_.Exception.Message -eq "Connection closed by remote host.") {
Write-Host "Failed to get hidden entities with ftp.exe for '$ftpFolderPath', usually because passive can not be used. Trying again.."
return Get-FtpChildItemHidden -ftpFolderPath $ftpFolderPath -userName $username -password $password -file:$file -directory:$directory
}
} finally {
$ErrorActionPreference = $old
Remove-Item $getHiddenFilesScript -Force
$ftpOutput = Get-Content $outPutEntity
Remove-Item $outPutEntity -Force
}
$startEntityOutput = ".."
$endEntityOutput = "226 Directory send OK."
$startIndex = $ftpOutput | where {$_.ToString().Contains($startEntityOutput)} | foreach { $ftpOutput.IndexOf($_) }
$endIndex = $ftpOutput.IndexOf($endEntityOutput) - 1
$entityOutput = @()
for($i = $startIndex+1; $i -le $endIndex; $i++) {
$entityOutput += $ftpOutput[$i]
}
$entities = @()
(Parse-Output -output $entityOutput -Directory:$directory -File:$file ) | foreach {
$entities += "$($ftpFolderPath)/$($_)"
}
return $entities
}
#Get-FtpChildItem -ftpFilePath "ftp://myHost.com/root/leaf/" -userName "User" -password "pw" -hidden $false -File
#Get-FtpChildItem -ftpFilePath "ftp://myHost.com/root/leaf/" -userName "User" -password "pw" -Directory
function Get-FtpChildItem($ftpFolderPath, $username, $password, [System.Management.Automation.SwitchParameter]$file, [System.Management.Automation.SwitchParameter]$directory, $hidden = $true) {
if($hidden) {
return Get-FtpChildItemHidden -ftpFolderPath $ftpFolderPath -userName $username -password $password -file:$file -directory:$directory
}
$ftprequest = New-FtpRequest -sourceUri $ftpFolderPath -method ([System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails + " -a") -username $username -password $password
$FTPResponse = $ftprequest.GetResponse()
$ResponseStream = $FTPResponse.GetResponseStream()
$StreamReader = New-Object System.IO.Streamreader $ResponseStream
$DirListing = (($StreamReader.ReadToEnd()) -split [Environment]::NewLine)
$StreamReader.Close()
$FTPResponse.Close()
$entities = @()
(Parse-Output -output $entityOutput -Directory:$directory -File:$file ) | foreach {
$entities += "$($ftpFolderPath)/$($_)"
}
return $entities
}
#Remove-FtpFolderWithFilesRecursive -ftpFilePath "ftp://myHost.com/root/leaf/world.mine" -userName "User" -password "pw"
function Remove-FtpFile($ftpFilePath, $username, $password) {
$ftprequest = New-FtpRequest -sourceUri $ftpFilePath -method ([System.Net.WebRequestMethods+Ftp]::DeleteFile) -username $username -password $password
$response = Send-FtpRequest $ftprequest
}
#Remove-FtpFolderWithFilesRecursive -ftpFilePath "ftp://myHost.com/root/leaf/" -userName "User" -password "pw"
function Remove-FtpDirectory($ftpFolderPath, $username, $password) {
$ftprequest = New-FtpRequest -sourceUri $ftpFolderPath -method ([System.Net.WebRequestMethods+Ftp]::RemoveDirectory) -username $username -password $password
$response = Send-FtpRequest $ftprequest
}
#Remove-FtpFolderWithFilesRecursive -ftpFilePath "ftp://myHost.com/root/" -userName "User" -password "pw"
function Remove-FtpFolderWithFilesRecursive($destinationFolder, $userName, $password) {
$files = Get-FtpChildItem -ftpFolderPath $destinationFolder -username $userName -password $password -File
foreach($file in $files) {
Remove-FtpFile -ftpFilePath $file -username $userName -password $password
}
$subDirectories = Get-FtpChildItem -ftpFolderPath $destinationFolder -username $userName -password $password -Directory
foreach($subDirectory in $subDirectories) {
Remove-FtpFolderWithFilesRecursive -destinationFolder ($subDirectory+"/") -userName $userName -pas $password
}
Remove-FtpDirectory $destinationFolder $userName $password
}
#Add-FtpDirectory -ftpFilePath "ftp://myHost.com/shouldCreate/" -userName "User" -password "pw"
function Add-FtpDirectory($ftpFolderPath, $username, $password) {
try {
$ftprequest = New-FtpRequest -sourceUri $ftpFolderPath -method ([System.Net.WebRequestMethods+Ftp]::MakeDirectory) -username $username -password $password
$response = Send-FtpRequest $ftprequest
} catch {
Write-Host "Creating folder '$ftpFolderPath' failed, maybe because this folder already exists."
}
}
#Add-FtpFile -ftpFilePath "ftp://myHost.com/folder/somewhere/uploaded.txt" -localFile "C:\temp\file.txt" -userName "User" -password "pw"
function Add-FtpFile($ftpFilePath, $localFile, $username, $password) {
$ftprequest = New-FtpRequest -sourceUri $ftpFilePath -method ([System.Net.WebRequestMethods+Ftp]::UploadFile) -username $username -password $password
Write-Host "$($ftpRequest.Method) for '$($ftpRequest.RequestUri)' complete'"
$content = $content = [System.IO.File]::ReadAllBytes($localFile)
$ftprequest.ContentLength = $content.Length
$requestStream = $ftprequest.GetRequestStream()
$requestStream.Write($content, 0, $content.Length)
$requestStream.Close()
$requestStream.Dispose()
}
#Add-FtpFolderWithFiles -sourceFolder "C:\temp\" -destinationFolder "ftp://myHost.com/folder/somewhere/" -userName "User" -password "pw"
function Add-FtpFolderWithFiles($sourceFolder, $destinationFolder, $userName, $password) {
Add-FtpDirectory $destinationFolder $userName $password
$files = Get-ChildItem $sourceFolder -File
foreach($file in $files) {
$uploadUrl ="$destinationFolder/$($file.Name)"
Add-FtpFile -ftpFilePath $uploadUrl -localFile $file.FullName -username $userName -password $password
}
}
#Add-FtpFolderWithFilesRecursive -sourceFolder "C:\temp\" -destinationFolder "ftp://myHost.com/folder/" -userName "User" -password "pw"
function Add-FtpFolderWithFilesRecursive($sourceFolder, $destinationFolder, $userName, $password) {
Add-FtpFolderWithFiles -sourceFolder $sourceFolder -destinationFolder $destinationFolder -userName $userName -password $password
$subDirectories = Get-ChildItem $sourceFolder -Directory
$fromUri = new-object System.Uri($sourceFolder)
foreach($subDirectory in $subDirectories) {
$toUri = new-object System.Uri($subDirectory.FullName)
$relativeUrl = $fromUri.MakeRelativeUri($toUri)
$relativePath = [System.Uri]::UnescapeDataString($relativeUrl.ToString())
$lastFolder = $relativePath.Substring($relativePath.LastIndexOf("/")+1)
Add-FtpFolderWithFilesRecursive -sourceFolder $subDirectory.FullName -destinationFolder "$destinationFolder/$lastFolder" -userName $userName -password $password
}
}