mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-04-23 10:28:23 +02:00
Add upload-file.ps1
This commit is contained in:
parent
992afad02c
commit
505c5e35f3
@ -158,6 +158,7 @@ turn-volume-up.ps1, turns the audio volume up (+10% by default)
|
|||||||
turn-volume-down.ps1, turns the audio volume down (-10% by default)
|
turn-volume-down.ps1, turns the audio volume down (-10% by default)
|
||||||
txt2wav.ps1, converts text into a audio .WAV file
|
txt2wav.ps1, converts text into a audio .WAV file
|
||||||
unmute-audio.ps1, unmutes audio
|
unmute-audio.ps1, unmutes audio
|
||||||
|
upload-file.ps1, uploads the local file to the given FTP server
|
||||||
voice-control.ps1, executes the PowerShell scripts by voice
|
voice-control.ps1, executes the PowerShell scripts by voice
|
||||||
weather.ps1, prints the current weather forecast
|
weather.ps1, prints the current weather forecast
|
||||||
weather-alert.ps1, checks the current weather for critical values
|
weather-alert.ps1, checks the current weather for critical values
|
||||||
|
|
@ -114,6 +114,7 @@ Mega Collection of PowerShell Scripts
|
|||||||
* [SHA1.ps1](Scripts/SHA1.ps1) - prints the SHA1 checksum of the given file
|
* [SHA1.ps1](Scripts/SHA1.ps1) - prints the SHA1 checksum of the given file
|
||||||
* [SHA256.ps1](Scripts/SHA256.ps1) - prints the SHA256 checksum of the given file
|
* [SHA256.ps1](Scripts/SHA256.ps1) - prints the SHA256 checksum of the given file
|
||||||
* [show-dir-tree.ps1](Scripts/show-dir-tree.ps1) - visualizes the given/current directory tree
|
* [show-dir-tree.ps1](Scripts/show-dir-tree.ps1) - visualizes the given/current directory tree
|
||||||
|
* [upload-file.ps1](Scripts/zip-dir.ps1) - uploads the local file to the given FTP server
|
||||||
* [zip-dir.ps1](Scripts/zip-dir.ps1) - creates a zip archive of the given directory
|
* [zip-dir.ps1](Scripts/zip-dir.ps1) - creates a zip archive of the given directory
|
||||||
|
|
||||||
📝 PowerShell Scripts for Git
|
📝 PowerShell Scripts for Git
|
||||||
|
48
Scripts/upload-file.ps1
Normal file
48
Scripts/upload-file.ps1
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#!/usr/bin/pwsh
|
||||||
|
<#
|
||||||
|
.SYNTAX upload-file.ps1 [<file>] [<URL>] [<username>] [<password>]
|
||||||
|
.DESCRIPTION uploads the local file to the given FTP server
|
||||||
|
.LINK https://github.com/fleschutz/PowerShell
|
||||||
|
.NOTES Author: Markus Fleschutz / License: CC0
|
||||||
|
#>
|
||||||
|
|
||||||
|
param($File = "", $URL = "", $Username = "", $Password = "")
|
||||||
|
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 login username" }
|
||||||
|
if ($Password -eq "") { $Password = read-host "Enter login password" }
|
||||||
|
|
||||||
|
try {
|
||||||
|
$StartTime = get-date
|
||||||
|
|
||||||
|
# check local file first:
|
||||||
|
if (-not(test-path "$File" -pathType leaf)) { throw "Can't access file: $File" }
|
||||||
|
$FullPath = Resolve-Path $File
|
||||||
|
$Filename = (Get-Item $File).Name
|
||||||
|
write-debug "Local file: $FullPath, filename: $Filename"
|
||||||
|
|
||||||
|
$request = [Net.WebRequest]::Create("$URL/$Filename")
|
||||||
|
$request.Credentials = New-Object System.Net.NetworkCredential("$Username", "$Password")
|
||||||
|
$request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
|
||||||
|
$request.EnableSSL = $false
|
||||||
|
$request.KeepAlive = $true
|
||||||
|
$request.UseBinary = $true
|
||||||
|
$request.UsePassive = $false
|
||||||
|
|
||||||
|
$fileStream = [System.IO.File]::OpenRead("$FullPath")
|
||||||
|
$ftpStream = $request.GetRequestStream()
|
||||||
|
|
||||||
|
$fileStream.CopyTo($ftpStream)
|
||||||
|
|
||||||
|
# cleanup:
|
||||||
|
$ftpStream.Dispose()
|
||||||
|
$fileStream.Dispose()
|
||||||
|
|
||||||
|
$StopTime = get-date
|
||||||
|
$TimeInterval = New-Timespan -start $StartTime -end $StopTime
|
||||||
|
write-host -foregroundColor green "OK - uploaded $File to $URL in $($TimeInterval.seconds) second(s)"
|
||||||
|
exit 0
|
||||||
|
} catch {
|
||||||
|
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||||
|
exit 1
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user