Если вы хотите использовать SFTP, я использую приведенный ниже код для автоматической загрузки некоторых файлов на FTP-сайт:
Прежде всего, вам необходимо скачать библиотеки winscp SFTP powershell.
https://winscp.net/eng/download.php
затем извлеките содержимое в том же месте, где находится сценарий.
Тогда в вашем скрипте вы должны добавить:
# Load WinSCP .NET assembly
# Give the path the dll file is located so powershell can call it.
Add-Type -Path "C:\Path where the dll file is located\WinSCPnet.dll"
# Setup session options
# Add all the properties the session needs to be established such as username password hostname and fingerprint.
# The username and password must be in plaintext.
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "HostName"
UserName = "UserName"
Password = "Password"
SshHostKeyFingerprint = "SSH fingerprint"
Затем, после завершения сеанса с этими учетными данными, вы должны выполнить следующий шаг копирования файлов.
# Open a session to the Host
# Try to connect to the Host with the credentials and information provided previously.
# Upload the file from a specific path to the path on the host.
# And then close the session and clean up the session trace data.
# $session.Dispose() -> If session was opened, closes it, terminates underlying WinSCP process, deletes XML log file and disposes object.
$session = New-Object WinSCP.Session
Try
{
# Connect to the SFTP site
$session.Open($sessionOptions)
# Upload the files from local disk to the destination
$session.PutFiles("Path of the file you want to upload", "/import/").Check()
}
Finally
{
# Disconnect, clean up
$session.Dispose()
}
Вероятно, есть более простой способ с Power Shell 6, который может сделать больше с операционными системами Unix / Linux, но на данный момент я не использовал его.