Created
June 14, 2019 03:15
-
-
Save imfioki/aada7734912dd044a659e1f7d8a86079 to your computer and use it in GitHub Desktop.
Uploading files to SFTP server using Powershell, WinSCP .NET Assembly, AWS SSM Parameter Store, SSH keys, and monitoring via AWS SNS notifications
This file contains 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
# Download and install .NET assembly at: https://winscp.net/eng/downloads.php#additional | |
# This process will send SNS notifications upload upload failure. | |
# Define connection parameters and globals | |
$server = '<127.0.0.1>' | |
$sftpUser = '<sftp_user>' | |
$ssmParam = '<sftp_password>' | |
$awsRegion = '<us-east-1>' | |
$snsTopic = '<sns_topic>' | |
$snsSubject = 'An error has occurred in production SFTP[IAM]' | |
$sftp_pass = (Get-SSMParameter -Region $awsRegion -Name $ssmParam -WithDecryption $true).Value | |
# This is the host key fingerprint of the server you're connecting to, NOT your private key. | |
$sshHostKeyFingerprint = '<ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx>' | |
# This is the location of your private key. Tidy up those key permissions (; | |
$sshPrivateKeyPath = "C:\location\to\key\file.ppk" | |
# Define the notification function | |
# The function takes the type of problem and the error contents as parameters in order to customize text and response procedure. | |
function notify($problem,$currErr){ | |
if($problem -eq "delete"){ | |
$message = @" | |
Greetings, | |
An error has occurred in the upload of the IAM reports to SFTP. Please see: | |
**** | |
$currErr.Failures | |
**** | |
Please investigate and escalate as neccessary. | |
"@} | |
elseif($problem -eq "upload"){ | |
Write-Host $currErr | |
$message = @" | |
Greetings, | |
The follow error has occurred when attempting to upload data to the SFTP server in the IAM process. | |
**** | |
$currErr | |
**** | |
Please investigate and escalate as neccessary. | |
"@} | |
# Send SNS Notification with custom message contents. | |
Publish-SNSMessage -TopicArn $snsTopic -Message $message -Subject $snsSubject -Region $awsRegion | |
} | |
# Wrap SFTP in try block to catch exceptions in upload process | |
try | |
{ | |
# Load WinSCP .NET assembly | |
Add-Type -Path "C:\sftp\WinSCPnet.dll" | |
# Setup session options | |
$sessionOptions = New-Object WinSCP.SessionOptions | |
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp | |
$sessionOptions.HostName = $server | |
$sessionOptions.UserName = $sftp_user | |
$sessionOptions.SshPrivateKeyPath = $sshPrivateKeyPath | |
$sessionOptions.SshHostKeyFingerprint = $sshHostKeyFingerprint | |
$session = New-Object WinSCP.Session | |
try | |
{ | |
# Connect and clear credentials from memory | |
$session.Open($sessionOptions) | |
$sessionOptions = $null | |
# Define transfer options for upload process | |
$transferOptions = New-Object WinSCP.TransferOptions | |
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary | |
# Upload file 1 | |
$transferResult = $session.PutFiles("C:\Export.csv", "/Export.csv", $False, $transferOptions) | |
# Report on success | |
foreach ($transfer in $transferResult.Transfers){Write-Host "Upload of $($transfer.FileName) succeeded"} | |
# Throw on any error | |
$transferResult.Check() | |
# Upload file 2 | |
$transferResult = $session.PutFiles("C:\Export.json", "/Export.json", $False, $transferOptions) | |
# Throw on any error | |
$transferResult.Check() | |
# Report on success | |
foreach ($transfer in $transferResult.Transfers){Write-Host "Upload of $($transfer.FileName) succeeded"} | |
} | |
# Due diligence in cleanup saves many turtles | |
finally{$session.Dispose()} | |
exit 0 | |
} | |
catch | |
{ | |
# Call notify function to send notifications via SNS | |
notify -problem "upload" -currErr $_.Exception.Message | |
exit 1 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment