PowerShell/Scripts/upload-file.ps1

77 lines
2.5 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-10-04 21:29:23 +02:00
Uploads a local file to a FTP server
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2022-01-30 10:49:30 +01:00
This PowerShell script uploads a local file to a FTP server.
2021-10-16 16:50:10 +02:00
.PARAMETER File
Specifies the path to the local file
.PARAMETER URL
Specifies the FTP server URL
.PARAMETER Username
Specifies the user name
.PARAMETER Password
Specifies the password
2021-07-13 21:10:02 +02:00
.EXAMPLE
PS> .\upload-file.ps1
.LINK
https://github.com/fleschutz/PowerShell
2022-01-30 10:49:30 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
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-09-27 10:09:45 +02:00
exit 0 # success
2021-04-14 10:23:14 +02:00
} catch {
2021-04-26 12:44:33 +02:00
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0]) after $Elapsed sec."
2021-04-14 10:23:14 +02:00
exit 1
}