'Logs of Posh-SSH SFTP Session in the email
I am trying to capture the logs from the SFTP Session using Posh-SSH PowerShell module on Windows 2016 server. I am trying to capture the SFTP Session and send it across in the body of the email.
$sftpServer = "IP Address or Hostname of SFTP Server"
$sftpDestPort = "22"
$sftpRemotePath = "/"
$sftpUsername = "ftpuser"
#Import the SFTP Module & CmdLets in per session mode.
Import-Module Posh-SSH
#Define the Private Key file path
$pKeyFile = "C:\SSHKeys\Privatekey.key"
$nopasswd = new-object System.Security.SecureString
#Set Credetials to connect to server
$CredDest = New-Object System.Management.Automation.PSCredential($sftpUsername, $nopasswd)
#Remove any open sessions
Get-SFTPsession | Remove-SFTPSession | Out-Null
#Create SFTP Session
$Session = New-SFTPSession -ComputerName $sftpServer -Credential $CredDest -KeyFile $pKeyFile -Port $sftpDestPort
#Now copy the files up
$result = Set-SFTPItem -SessionId $Session.SessionId -Path "$archivefile" -Destination "$sftpRemotePath" -Force
$result.output = $value1 # Need Help Here
#Send an email
$date = Get-Date -Format dd-MMM-yyyy-hh-mm-ss
$Subject = "SFTP Upload completed on $sftpServer at $date"
$Body = "SFTP Script Ended `r`n`nFiles $archivefile copied to $sftpServer`r`n`nHost: $($Session.Host)`r`n`nSFT session Logs:`r`n"
"$value1" #Need Help here
$SMTP = "10.137.211.5"
$To = "[email protected]", "[email protected]"
$From = "[email protected]"
Send-MailMessage -To $To -From $From -SmtpServer $SMTP -Body $Body -Subject $Subject
#Remove the session
Get-SFTPsession | Remove-SFTPSession | Out-Null
enter code here
In the above script please look at comments # Need Help here, where I need the help.
Thanks, Sudeep
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
