Powershell: Using send-mailmessage after PSFTP in script - powershell

I have written a small script that are using PSFTP to connect to a FTP server, download some files and conclude by sending an email with a summary of downloaded files.
I get a problem with the send-mailmessage part, which fails with this error:
Send-MailMessage : The remote certificate is invalid according to the validation procedure.
When running the send-mailmessage alone, it works flawlessly, it is only when using it combined with PSFTP, it fails. I am guessing, that it is the due to having made a session with the ftpserver, that somehow interferes, but that session should be ended when we come to the send mail. I can't figure this out, can anybody help?
My script:
#--------- FTP server variables here ------
$User = "XXX"
$Pass = "XXX"
$EncryptedPass = ConvertTo-SecureString -String $Pass -asPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($User,$EncryptedPass)
$Server = "XXX"
#-------- Mail credentials here -----------
$User_ = "XXX"
$Pass_ = "XXX"
$EncryptedPass_ = ConvertTo-SecureString -String $Pass_ -asPlainText -Force
$Mailcredentials = New-Object System.Management.Automation.PSCredential($User_,$EncryptedPass_)
#$mailcredentials =
$Subject ="'bowkerdownload'"
$To = "XXX"
$From = "XXX"
$body = (get-content -path g:\resultout.txt) -join "`n"
$SMTP = "smtp.office365.com"
#---------File variables here --------------
$month = (Get-Date).AddMonths(-8).ToString('MMM')
$year = (Get-date).addyears(-1).ToString('yyyy')
$filesequential="_*_"
$filesize = "lc"
$filetype = ".txt"
$samlevar=($month + ''+$year + ''+$filesequential + ''+$filesize + ''+$filetype)
$filename = $samlevar.tostring()
#------ The script---------
Set-FTPConnection -Credentials $Credentials -Server $server -Session defaultftpsession -UsePassive
$Session = Get-FTPConnection -Session defaultftpsession
Get-FTPChildItem -Path /Download -filter "$filename" |
Get-FTPItem -session $Session -LocalPath g:\ -Overwrite |
Convertfrom-string -PropertyNames Kode, Status, Action, fil |
Select-Object -Property Status, action, Fil |
Out-file g:\resultout.txt;
Send-MailMessage -To $To -Subject $Subject -Body $Body -SmtpServer $SMTP -From $From -Credential $Mailcredentials -port 587 -UseSsl``

Related

Check if files exist in a FTP remote directory outside our domain

We have been asked to check if files exists in a SFTP remote directory, on a server outside our organisation, and send an email if it does exist.
I have the IP address, an account, and a password.
I found this powershell script https://www.tech2tech.fr/powershell-surveiller-des-fichiers-avec-envoi-de-mail/ , but... How can i make it work on a remote directory, using credential to access it?
I also try using ftp command, but can't find a way to check if many files exist, and not only one.
Does anyone have a solution or a way to help me?
Thanks in advance !
You need to start from here:
$cred = Get-StoredCredential -Target creds;
$SFTPSession = New-SFTPSession -ComputerName sftp.server.org -Credential $cred
Test-SFTPPath $SFTPSession "/path/filename.txt" # check if file exist
Remove-SFTPSession -SessionID 0 -Verbose
Get-SFTPSession # ensure the session is closed
Than you can add a condition and send mail:
Send-MailMessage -To “<recipient’s email>” -From “<sender’s email>” -Subject
“message subject” -Body “Some text!” -Credential (Get-Credential) -SmtpServer
“<smtp server>” -Port 587
Try using the Posh-SSH module.
You can store your password as a plain string in your script, but it would be better to use Import-Clixml to securely store credentials.
Check out online documentation of both modules.
# Install and import module
Install-Module Posh-SSH
Import-Module Posh-SSH
$host = <sftp-location>
$port = <port>
#Set Credentials
$user = <username>
$pass = ConvertTo-SecureString <password> -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ($user, $pass)
$path = <location to check>
# Initiate SFTP session
$session = New-SFTPSession -ComputerName $host -Credential $Credential -Port $port -AcceptKey:$true
# Check if files exist on specified location
Test-SFTPPath -SFTPSession $session -Path $path
# Check if more than one file
$items = Get-SFTPChildItem -SFTPSession $session -Path $path
if($items -gt 1) {
<your code>
}
# close session
Remove-SFTPSession -SFTPSession $session
Thank you all !
With your help I was able to complete the task.
Here is what I used:
Install and import module
Install-Module Posh-SSH
Import-Module Posh-SSH
$SFTPhost = "My_SFTP_Server"
$port = "22"
#Set Credentials
$user = "test_sftp"
$pass = ConvertTo-SecureString -String "******" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $pass
$path = "/Prod/saved"
# Initiate SFTP session
$session = New-SFTPSession -ComputerName $SFTPhost -Credential $Credential -Port $port -AcceptKey:$true
# Check if files exist on specified location
Test-SFTPPath -SFTPSession $session -Path $path
# Check if more than one file
$items = (Get-SFTPChildItem -SFTPSession $session -Path $path | measure-object).count
if($items_path -gt 1) {
Send-MailMessage -To 'toto <toto#MyDomain.com>' -From 'No Reply Check <no_reply_CheckFiles#MyDomain.com>' -Subject 'Files on server' -Body 'There is files in PROD repertory' -SmtpServer “My_SMTP_server_Address”
}
# close session
Remove-SFTPSession -SFTPSession $session

PowerShell Email Attachment: Could not find file 'D:\20201124131044\TRACS-20201124034849\error.log environment.log RACS-20201124034849.sterr.stdout

I am trying to send out an email with only the attachments that exist in the directory. There are three sets of files i am monitoring: error.log, environment.log and a .stdout. I want to check whether each one of the files exist and if they do then add it in a list and send it out as attachments. I dont want to do anything if the files dont exist. I attempted to loop through the list of files (this is in windows) and then if the files exist send them out. My code below:
$PSEmailServer = "xxxxxxxxxxx"
$Password ="xxxxxxxxx"
$mypassword = ConvertTo-SecureString $Password -AsPlainText -Force
$mycredential = New-Object System.Management.Automation.PSCredential ("xxxxx", $mypassword)
$EmailSubject ="test"
$filepath ="D:\20201124131044\TRACS-20201124034849\"
$files = #('error.log','environment.log','TRACS-20201124034849.sterr.stdout')
$FromEmail ="xxxxxxxxxx"
if(($files | foreach{Test-path $filepath }) -contains $True){
$newfile = $filepath + $files
Send-MailMessage -From $FromEmail -To "xxxxxxx" -Subject $EmailSubject -Body "test" -Credential $mycredential -Attachments $newfile
}
else {
echo "nothing"
exit
}
when i run the above i get the following error:
Send-MailMessage : Could not find file 'D:\20201124131044\TRACS-20201124034849\error.log environment.log TRACS-20201124034849.sterr.stdout'. At C:\Users\Downloads\powershell.ps1:22 char:1
+ Send-MailMessage -From $FromEmail -To "xxxxxxx" -Subje ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Send-MailMessage], FileNotFoundException
+ FullyQualifiedErrorId : > System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.SendMailMessage
I think I need to iterate the list to attach each filename to the it's filepath and then store it in a list and then send out the email? Please advise.
You can simply use -contains or -in to find filenames in an array.
Also, I would recommend using splatting for the parameters to Send-MailMessage:
$filepath = "D:\20201124131044\TRACS-20201124034849"
$files = 'error.log','environment.log','TRACS-20201124034849.sterr.stdout'
# find files with names that can be found in the $files array
$attachments = #(Get-ChildItem -Path $filepath -File | Where-Object { $files -contains $_.Name })
# if we have any files to send
if ($attachments.Count) {
$Password = "xxxxxxxxx"
$mypassword = ConvertTo-SecureString $Password -AsPlainText -Force
$mycredential = New-Object System.Management.Automation.PSCredential ("xxxxx", $mypassword)
# use splatting for better readability
$mailParams = #{
To = "xxxxxxx"
From = "xxxxxxxxxx"
Subject = "test"
Body = "test"
Credential = $mycredential
Attachments = $attachments.FullName
SmtpServer = "xxxxxxxxxxx"
# more parameters can be added here
}
# send the email
Send-MailMessage #mailParams
}
else {
Write-Host 'Nothing to send..'
}
Try
$PSEmailServer = "xxxxxxxxxxx"
$Password ="xxxxxxxxx"
$mypassword = ConvertTo-SecureString $Password -AsPlainText -Force
$mycredential = New-Object System.Management.Automation.PSCredential ("xxxxx", $mypassword)
$EmailSubject ="test"
$attachments = Get-ChildItem "D:\20201124131044\TRACS-20201124034849" | Where-Object {$_.Name -match "error.log|environment.log|TRACS-20201124034849.sterr.stdout"}
$FromEmail ="xxxxxxxxxx"
if($attachments){
Send-MailMessage -From $FromEmail -To "xxxxxxx" -Subject $EmailSubject -Body "test" -Credential $mycredential -Attachments $attachements
}
else {
echo "nothing"
exit
}

Powershell Script to send email after validation

I have a Powershell script that logs the result to a text file (this is working fine), now I need to update it to send email instead of a Log file (see actual script below). I'm kind of a new to powershell, hopefully you can help me out.
# details of the script
# if a file "FILE.400" is found and check the modified date, it will send a log file (txt file)
# need to update the script, to send email instead of a log file
$svr=$env:ComputerName
$date=[datetime]::Today.ToString('MM-dd-yyyy')
$filename = "FILE.400"
$DestLogs1 = "E:\sample\pfupdatedfin-$date.txt"
$DestLogs2 = "E:\sample\pfoutdatedfin-$date.txt"
$SrcPath = "E:\sample\$filename"
If ((Get-ChildItem $SrcPath).LastWriteTime -gt $date)
{
$date | Out-File $DestLogs1
Write-Output "Found latest FILE.400 file" | Out-File $DestLogs1 -Append
(Get-ChildItem -Recurse -Path $SrcPath).LastWriteTime | Out-File $DestLogs1 -Append
}
Else
{
Write-Output "FILE.400 - is not updated, please verify " | Out-File $DestLogs2
(Get-ChildItem -Recurse -Path $SrcPath).LastWriteTime | Out-File $DestLogs2 -Append
}
Send-MailMessageis pretty easy to use. The online documentation for cmdlets is very easy to find. Just enter the name of the cmdlet in your favorite search engine. Or use the Get-Help cmdlet. Here is an example how to use it just fille the Variables reasonably :
Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Port $SMTPPort -Credential $Credential # -UseSsl:$UseSsl
-useSsl is a switch so it is not necessary unless it's $true. If you want to provide a variable to a switch parameter, don't use a whitespace but a : in between.
Credentials in Powershell are stored in credential objects. You can't just use a plain text password. However don't think they are stored extremly save since the password can be easly extracted if you know how. Here is how you create the necessary credential object:
$SecPw = ConvertTo-SecureString -AsPlainText $Password -Force
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName,$SecPw
the script is working as expected, in my Home network
$svr=$env:ComputerName
$date=[datetime]::Today.ToString('MM-dd-yyyy')
$filename = "FILE.400"
$DestLogs1 = "E:\sample\pfupdatedfin-$date.txt"
$DestLogs2 = "E:\sample\pfoutdatedfin-$date.txt"
$SrcPath = "E:\sample\$filename"
# email settings
$MyEmail = "myemail#gmail.com"
$SMTP = "smtp.gmail.com"
$To = "samplereceiver#gmail.com"
$Subject1 = "JDA-Alert || Found FILE.400 file - Successful"
$Body1 = "JDA-Alert || Found FILE.400 file - Successful"
$Subject2 = "JDA-Alert || FILE.400 file - Outdated File"
$Body2 = "JDA-Alert || FILE.400 file - Outdated File"
$Creds = (Get-Credential -Credential "$MyEmail")
If ((Get-ChildItem $SrcPath).LastWriteTime -gt $date)
{
Start-Sleep 2
Send-MailMessage -To $To -From $MyEmail -Subject $Subject1 -Body $Body1 -SmtpServer $SMTP -Credential $Creds -UseSsl -Port 587 -DeliveryNotificationOption Never
}
Else
{
Start-Sleep 2
Send-MailMessage -To $To -From $MyEmail -Subject $Subject2 -Body $Body2 -SmtpServer $SMTP -Credential $Creds -UseSsl -Port 587 -DeliveryNotificationOption Never
}

Send-Mailmessage timing out

I am looking to improve an old batch script and upgrade it to powershell, this is a robocopy batch script and i would like it to send a mail with the logfile attatched when it is finished. i managed to get the drive mappings and robocopy part sorted but im having some issues getting the send-mailmessage part to work
`$SourceFolder = "V:\"
$DestinationFolder = "Y:\"
$Dateandtime = Get-Date -format filedate
$password = XXXXXXXXX
$Subject = "Robocopy Results: Backup USA - NL"
$SMTPServer = "mailserver.domain.com"
$Sender = "backupusr#domain.com"
$Username = "backupusr"
$Recipients = "administrator#domain.com"
$Admin = "administrator#domain.com"
$SendEmail = $True
$IncludeAdmin = $True
$AsAttachment = $False
$Cred = new-object Management.Automation.PSCredential $Username, ($password
| ConvertTo-SecureString -AsPlainText -Force)`
This is the line that causes the script to timeout
Send-MailMessage -SmtpServer $SMTPServer -From $Sender -To $Recipients -Subject $Subject -Body "Robocopy results are attached." -DeliveryNotificationOption onFailure -UseSsl -Credential $Cred
this is the error i recieve
Send-MailMessage : The operation has timed out.
At C:\Scripts\bpcti-robocopy.ps1:113 char:1
+ Send-MailMessage -SmtpServer $SMTPServer -From $Sender -To $Recipient ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
Any help would be greatly appreciated.
The -Credential parameter takes a PSCredential object. You can use Get-Credential to interactively create one using a username and password combination. More detail here.
Alternatively, as your password is already in plaintext in your script, you can use the process as described here and construct a new PSCredential.
# Convert the plaintext password into a secure string.
$SecurePassword = ConvertTo-SecureString "XXXXXXXXX" -AsPlainText -Force
# Create a new PSCredential using the username and secure string password.
$Cred = New-Object System.Management.Automation.PSCredential ("backupusr#domain.com", $SecurePassword)
Send-MailMessage -SmtpServer mailserver.domain.com -From backupusr#domain.com -To administrator#domain.com -Subject testing -Body "Robocopy results are attached." -DeliveryNotificationOption onFailure -Credential $Cred
Beware that the username should include the #domain.com.

Send Email of PowerShell Output

I'm new to PowerShell, I'm running Windows Server 2012 R2. The following Script gives me Output what I want but in the message the output is not appearing and it saying the Body cannot be "null" or "empty".
$fileToLocate = "*"
$Directories = #(
"C:\CtvFTPSite\AE1\ASN\Undelivered\",
"C:\CtvFTPSite\AE1\Invoices\Undelivered\",
"C:\CtvFTPSite\AE1\SO\Undelivered\",
"C:\CtvFTPSite\AE2\ASN\Undelivered\",
"C:\CtvFTPSite\AE2\Invoices\Undelivered\",
"C:\CtvFTPSite\AE2\SO\Undelivered\",
"C:\CtvFTPSite\BH1\ASN\Undelivered\",
"C:\CtvFTPSite\BH1\Invoices\Undelivered\",
"C:\CtvFTPSite\BH1\SO\Undelivered\",
"C:\CtvFTPSite\KW1\ASN\Undelivered\",
"C:\CtvFTPSite\KW1\Invoices\Undelivered\",
"C:\CtvFTPSite\KW1\SO\Undelivered\",
"C:\CtvFTPSite\OM1\ASN\Undelivered\",
"C:\CtvFTPSite\OM1\Invoices\Undelivered\",
"C:\CtvFTPSite\OM1\SO\Undelivered\",
"C:\CtvFTPSite\SA1\ASN\Undelivered\",
"C:\CtvFTPSite\SA1\Invoices\Undelivered\",
"C:\CtvFTPSite\SA1\SO\Undelivered\"
)
$filesUndelivered = Join-Path -Path $Directories -ChildPath $fileToLocate | Where-Object{Test-Path $_} | ForEach-Object{
Get-ChildItem $Directories -Recurse | % { Write-Host $_.FullName }
}
$Max_mins = "-5"
$Curr_date = get-date
$username = "myusername#myemail.com"
$password = Get-Content C:\security\string.txt | ConvertTo-SecureString
$cred = new-object -typename System.Management.Automation.PSCredential `
-argumentlist $username, $password
$localIpAddress = $(ipconfig | where {$_ -match 'IPv4.+\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' } | out-null; $Matches[1])
[string]$messagebody ="$filesUndelivered"
[string]$titlefailed ="Urgent You Have Files in Undelivered Folder in $localIpAddress in $env:computername"
$portno = "25"
$smtpsrv = "mail.server.com"
$smtpto = "myusername#myemail.com"
$smtpfrom ="myusername#myemail.com"
if ($filesfailed.Count)
{
foreach ($file in $filesUndelivered) {[string]$messagebody += $file.FullName + "`r`n"}
Send-MailMessage -To $smtpto -From $smtpfrom -port $portno -SmtpServer $smtpsrv -Credential $cred -Subject $titlefailed -Body $messagebody
}
Fixed
$fileToLocate = "*"
$failedpath = #(
"C:\CtvFTPSite\AE1\ASN\Undelivered\",
"C:\CtvFTPSite\AE1\Invoices\Undelivered\",
"C:\CtvFTPSite\AE1\SO\Undelivered\",
"C:\CtvFTPSite\AE2\ASN\Undelivered\",
"C:\CtvFTPSite\AE2\Invoices\Undelivered\",
"C:\CtvFTPSite\AE2\SO\Undelivered\",
"C:\CtvFTPSite\BH1\ASN\Undelivered\",
"C:\CtvFTPSite\BH1\Invoices\Undelivered\",
"C:\CtvFTPSite\BH1\SO\Undelivered\",
"C:\CtvFTPSite\KW1\ASN\Undelivered\",
"C:\CtvFTPSite\KW1\Invoices\Undelivered\",
"C:\CtvFTPSite\KW1\SO\Undelivered\",
"C:\CtvFTPSite\OM1\ASN\Undelivered\",
"C:\CtvFTPSite\OM1\Invoices\Undelivered\",
"C:\CtvFTPSite\OM1\SO\Undelivered\",
"C:\CtvFTPSite\SA1\ASN\Undelivered\",
"C:\CtvFTPSite\SA1\Invoices\Undelivered\",
"C:\CtvFTPSite\SA1\SO\Undelivered\"
)
$Max_mins = "-5"
$Curr_date = get-date
$username = "username#email.com"
$password = Get-Content C:\security\string.txt | ConvertTo-SecureString
$cred = new-object -typename System.Management.Automation.PSCredential `
-argumentlist $username, $password
$filesfailed = Get-ChildItem -Path $failedpath | Where{$_.CreationTime -lt ($Curr_date).addminutes($Max_mins)}
$localIpAddress = $(ipconfig | where {$_ -match 'IPv4.+\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' } | out-null; $Matches[1])
[string]$messagebody =""
[string]$titlefailed ="Urgent You Have Files in Undelivered Folder in $localIpAddress in $env:computername"
$portno = "25"
$smtpsrv = "mail.server.net"
$smtpto = "username#email.com"
$smtpfrom ="username#email.com"
if ($filesfailed.Count)
{
foreach ($file in $filesfailed) {[string]$messagebody += $file.FullName + "`r`n"}
Send-MailMessage -To $smtpto -From $smtpfrom -port $portno -SmtpServer $smtpsrv -Credential $cred -Subject $titlefailed -Body $messagebody
}