Last active
June 1, 2023 10:30
-
-
Save wvargas-ods/bdbb8a090b70845203b90c2723311c64 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
# Powershell can't SFTP by itself, so it's necessary to install a client on Windows to use it as a bridge. | |
# In my case, I decided to install WinSCP (I think it's a good option): | |
# * https://stackoverflow.com/questions/38732025/upload-file-to-sftp-using-powershell/38735275#38735275 | |
# * https://winscp.net/eng/download.php | |
# Here are some links about how Get-ChildItem works so you can get and filter the files that you need: | |
# * https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7.1 | |
# * https://www.pdq.com/blog/using-get-childitem-find-files/ | |
### | |
# Load WinSCP .NET assembly | |
# Set the path where the WinSCPnet.dll exists | |
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll" | |
# Variables | |
$files = Get-ChildItem -Path "C:\Users\your_user\Shortcuts\" -Name | |
$HostName = "hostname" | |
$UserName = "username" | |
$Password = "password" | |
$SshKey = "ssh-rsa 2048 xxxxxxxxxxx...=" | |
# Setup session options | |
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{ | |
HostName = $HostName | |
UserName = $UserName | |
Password = $Password | |
Protocol = [WinSCP.Protocol]::ftp | |
# Protocol = [WinSCP.Protocol]::Sftp | |
# SshHostKeyFingerprint = $SshKey | |
} | |
$session = New-Object WinSCP.Session | |
try | |
{ | |
# Connect | |
$session.Open($sessionOptions) | |
Write-Output "Connected to $HostName" | |
ForEach ($file in $files) | |
{ | |
# Get file from folder and ulpload it | |
$session.PutFiles("C:\Users\your_user\Downloads\$file", "/home/your_user/downloads/$file").Check() | |
Write-Output "$file Uploaded" | |
} | |
} | |
finally | |
{ | |
# Disconnect, clean up | |
$session.Dispose() | |
Write-Output "Disconnected from $HostName" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment