Improve upload-file.ps1

This commit is contained in:
Markus Fleschutz 2021-04-26 12:44:33 +02:00
parent 3ea4432a38
commit dc7dd8d410

View File

@ -9,32 +9,38 @@ param($File = "", $URL = "", $Username = "", $Password = "")
if ($File -eq "") { $File = read-host "Enter local file to upload" } if ($File -eq "") { $File = read-host "Enter local file to upload" }
if ($URL -eq "") { $URL = read-host "Enter URL of FTP server" } if ($URL -eq "") { $URL = read-host "Enter URL of FTP server" }
if ($Username -eq "") { $Username = read-host "Enter login username" } if ($Username -eq "") { $Username = read-host "Enter username for login" }
if ($Password -eq "") { $Password = read-host "Enter login password" } 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
try { try {
$StopWatch = [system.diagnostics.stopwatch]::startNew() $StopWatch = [system.diagnostics.stopwatch]::startNew()
# check local file first: # check local file:
if (-not(test-path "$File" -pathType leaf)) { throw "Can't access file: $File" }
$FullPath = Resolve-Path "$File" $FullPath = Resolve-Path "$File"
$Filename = (Get-Item $File).Name if (-not(test-path "$FullPath" -pathType leaf)) { throw "Can't access file: $FullPath" }
$FileSize = (Get-Item $File).Length $Filename = (Get-Item $FullPath).Name
"Local file: $FullPath ($FileSize bytes)" $FileSize = (Get-Item $FullPath).Length
"⏳ Uploading 📄$Filename ($FileSize bytes) to $URL ..."
$request = [Net.WebRequest]::Create("$URL/$Filename") # prepare request:
$request.Credentials = New-Object System.Net.NetworkCredential("$Username", "$Password") $Request = [Net.WebRequest]::Create("$URL/$Filename")
$request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile $Request.Credentials = New-Object System.Net.NetworkCredential("$Username", "$Password")
$request.EnableSSL = $false $Request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$request.KeepAlive = $true $Request.EnableSSL = $EnableSSL
$request.UseBinary = $true $Request.UseBinary = $UseBinary
$request.UsePassive = $true $Request.UsePassive = $UsePassive
$Request.KeepAlive = $KeepAlive
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$IgnoreCert}
$fileStream = [System.IO.File]::OpenRead("$FullPath") $fileStream = [System.IO.File]::OpenRead("$FullPath")
$ftpStream = $request.GetRequestStream() $ftpStream = $Request.GetRequestStream()
"Uploading ..." $Buf = New-Object Byte[] 32KB
$Buf = New-Object Byte[] 64KB
while (($DataRead = $fileStream.Read($Buf, 0, $Buf.Length)) -gt 0) while (($DataRead = $fileStream.Read($Buf, 0, $Buf.Length)) -gt 0)
{ {
$ftpStream.Write($Buf, 0, $DataRead) $ftpStream.Write($Buf, 0, $DataRead)
@ -47,9 +53,10 @@ try {
$fileStream.Dispose() $fileStream.Dispose()
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds [int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ uploaded $Filename to $URL in $Elapsed sec." "✔️ uploaded 📄$Filename to $URL in $Elapsed sec."
exit 0 exit 0
} catch { } catch {
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" [int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0]) after $Elapsed sec."
exit 1 exit 1
} }