2021-04-21 19:53:52 +02:00
|
|
|
|
<#
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.SYNOPSIS
|
|
|
|
|
upload-file.ps1 [<file>] [<URL>] [<username>] [<password>]
|
|
|
|
|
.DESCRIPTION
|
2021-08-29 17:50:03 +02:00
|
|
|
|
Uploads the local file to the given FTP server.
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.EXAMPLE
|
|
|
|
|
PS> .\upload-file.ps1
|
2021-08-29 17:50:03 +02:00
|
|
|
|
.NOTES
|
|
|
|
|
Author: Markus Fleschutz · License: CC0
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/fleschutz/PowerShell
|
2021-04-14 10:23:14 +02:00
|
|
|
|
#>
|
|
|
|
|
|
2021-07-15 15:51:22 +02:00
|
|
|
|
param([string]$File = "", [string]$URL = "", [string]$Username = "", [string]$Password = "")
|
2021-04-14 10:23:14 +02:00
|
|
|
|
|
|
|
|
|
try {
|
2021-07-15 15:51:22 +02:00
|
|
|
|
if ($File -eq "") { $File = read-host "Enter local file to upload" }
|
|
|
|
|
if ($URL -eq "") { $URL = read-host "Enter URL of FTP server" }
|
|
|
|
|
if ($Username -eq "") { $Username = read-host "Enter username for login" }
|
|
|
|
|
if ($Password -eq "") { $Password = read-host "Enter password for login" }
|
|
|
|
|
[bool]$EnableSSL = $true
|
|
|
|
|
[bool]$UseBinary = $true
|
|
|
|
|
[bool]$UsePassive = $true
|
|
|
|
|
[bool]$KeepAlive = $true
|
|
|
|
|
[bool]$IgnoreCert = $true
|
|
|
|
|
|
2021-04-15 08:41:37 +02:00
|
|
|
|
$StopWatch = [system.diagnostics.stopwatch]::startNew()
|
2021-04-14 10:23:14 +02:00
|
|
|
|
|
2021-04-26 12:44:33 +02:00
|
|
|
|
# check local file:
|
2021-04-23 09:11:39 +02:00
|
|
|
|
$FullPath = Resolve-Path "$File"
|
2021-04-26 12:44:33 +02:00
|
|
|
|
if (-not(test-path "$FullPath" -pathType leaf)) { throw "Can't access file: $FullPath" }
|
|
|
|
|
$Filename = (Get-Item $FullPath).Name
|
|
|
|
|
$FileSize = (Get-Item $FullPath).Length
|
|
|
|
|
"⏳ Uploading 📄$Filename ($FileSize bytes) to $URL ..."
|
|
|
|
|
|
|
|
|
|
# prepare request:
|
|
|
|
|
$Request = [Net.WebRequest]::Create("$URL/$Filename")
|
|
|
|
|
$Request.Credentials = New-Object System.Net.NetworkCredential("$Username", "$Password")
|
|
|
|
|
$Request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
|
|
|
|
|
$Request.EnableSSL = $EnableSSL
|
|
|
|
|
$Request.UseBinary = $UseBinary
|
|
|
|
|
$Request.UsePassive = $UsePassive
|
|
|
|
|
$Request.KeepAlive = $KeepAlive
|
|
|
|
|
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$IgnoreCert}
|
2021-04-14 10:23:14 +02:00
|
|
|
|
|
|
|
|
|
$fileStream = [System.IO.File]::OpenRead("$FullPath")
|
2021-04-26 12:44:33 +02:00
|
|
|
|
$ftpStream = $Request.GetRequestStream()
|
2021-04-14 10:23:14 +02:00
|
|
|
|
|
2021-04-26 12:44:33 +02:00
|
|
|
|
$Buf = New-Object Byte[] 32KB
|
2021-04-23 09:11:39 +02:00
|
|
|
|
while (($DataRead = $fileStream.Read($Buf, 0, $Buf.Length)) -gt 0)
|
2021-04-14 10:43:48 +02:00
|
|
|
|
{
|
2021-04-23 09:11:39 +02:00
|
|
|
|
$ftpStream.Write($Buf, 0, $DataRead)
|
2021-04-14 10:43:48 +02:00
|
|
|
|
$pct = ($fileStream.Position / $fileStream.Length)
|
|
|
|
|
Write-Progress -Activity "Uploading" -Status ("{0:P0} complete:" -f $pct) -PercentComplete ($pct * 100)
|
|
|
|
|
}
|
2021-04-14 10:23:14 +02:00
|
|
|
|
|
|
|
|
|
# cleanup:
|
|
|
|
|
$ftpStream.Dispose()
|
|
|
|
|
$fileStream.Dispose()
|
|
|
|
|
|
2021-04-21 16:43:08 +02:00
|
|
|
|
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
|
2021-06-07 20:06:55 +02:00
|
|
|
|
"✔️ uploaded 📄$Filename to $URL in $Elapsed sec"
|
2021-04-14 10:23:14 +02:00
|
|
|
|
exit 0
|
|
|
|
|
} catch {
|
2021-04-26 12:44:33 +02:00
|
|
|
|
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
|
|
|
|
|
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0]) after $Elapsed sec."
|
2021-04-14 10:23:14 +02:00
|
|
|
|
exit 1
|
|
|
|
|
}
|