I had to convert an old ps script from old school FTP to SFTP that uploads files in a few different "local" directories and throws them in the root of our web server. After some digging, I came up with this script. Because the files are in different paths, I used a variable and separate command for each. It works just fine, but I was wondering if there is a more elegant or best practice way of doing that.
# SFTP Upload of Inventory From CSV files to WPEngine SFTP. Requires installation of Posh-SSH
# Install-Module -Name Posh-SSH (https://github.com/darkoperator/Posh-SSH)
# Set the credentials
$Password = ConvertTo-SecureString 'passwordgoeshere' -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ('usernamegoeshere', $Password)
# Set local file path and SFTP path
$FilePath1 = "D:\Data\SW\SWRUN\BA\BAUPC.CSV"
$FilePath2 = "D:\Data\SW\SWRUN\MI\MIUPC.CSV"
$FilePath3 = "D:\Data\SW\SWRUN\NM\NMUPC.CSV"
$FilePath4 = "D:\Data\SW\SWRUN\SS\SSUPC.CSV"
$SftpPath = '/'
# Set the Hostname of the SFTP server
$SftpServer = 'domain.sftp.wpengine.com'
# Load the Posh-SSH module
Import-Module Posh-SSH
# Establish the SFTP connection
$ThisSession = New-SFTPSession -ComputerName $SftpServer -Credential $Credential -AcceptKey -Port 2222
# Upload the files to the SFTP path
Set-SFTPFile -SessionId ($ThisSession).SessionId -Localfile $FilePath1 -RemotePath $SftpPath -Overwrite
Set-SFTPFile -SessionId ($ThisSession).SessionId -Localfile $FilePath2 -RemotePath $SftpPath -Overwrite
Set-SFTPFile -SessionId ($ThisSession).SessionId -Localfile $FilePath3 -RemotePath $SftpPath -Overwrite
Set-SFTPFile -SessionId ($ThisSession).SessionId -Localfile $FilePath4 -RemotePath $SftpPath -Overwrite
#Disconnect all SFTP Sessions
Get-SFTPSession | % { Remove-SFTPSession -SessionId ($_.SessionId) }