我正在尝试使用 Windows 2016 服务器上的 Posh-SSH PowerShell 模块从 SFTP 会话中捕获日志。我正在尝试捕获 SFTP 会话并将其发送到电子邮件正文中。
$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 = "user0@domain.com", "user1@domain.com"
$From = "sender@domain.com"
Send-MailMessage -To $To -From $From -SmtpServer $SMTP -Body $Body -Subject $Subject
#Remove the session
Get-SFTPsession | Remove-SFTPSession | Out-Null
enter code here
在上面的脚本中,请在此处查看注释#Need Help,我需要帮助。
谢谢, 苏迪普