To upload a file to an SFTP server using PowerShell, you can use the PSSFTP module or WinSCP with its automation API. Here’s a basic example using WinSCP:
- Download and install WinSCP from its official site.
- Load the WinSCP .NET assembly in PowerShell:
powershell
Copy code
Add-Type -Path “C:\Program Files (x86)\WinSCP\WinSCPnet.dll”
- Set up the session and upload the file:
powershell
$session = New-Object WinSCP.Session
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.HostName = “sftp.server.com”
$sessionOptions.UserName = “username”
$sessionOptions.Password = “password”
$sessionOptions.SshHostKeyFingerprint = “ssh-rsa 2048 …”
$session.Open($sessionOptions)
$session.PutFiles(“C:\path\to\local\file.txt”, “/remote/path/”).Check()
$session.Dispose()
This script authenticates to the SFTP server, uploads the file, and then closes the session. Make sure to replace the placeholders with your actual details.