0

I have been trying to use this script as a simple boolean that checks for a file, and sends a success email, or a failure email. But I cannot for the life of me figure out why it doesn't work.

Any ideas?

$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "xxxx@xxxxx.com"
$Password = "xxxxxxx"
$to = "xxx@xxxxx.net"

if ( ([System.Io.fileinfo]'E:\GD Backup Folder\backup\Backup_*.zip').LastWriteTime.Date -ne [datetime]::Today )    {
            $message = New-Object System.Net.Mail.MailMessage
            $message.subject = "Backup Successful"
            $message.body = "Backup was successful."
            $message.to.add($to)
            $message.from = $username

            $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
            $smtp.EnableSSL = $true
            $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
            $smtp.send($message)
            write-host "Mail Sent"
}else{
            $message = New-Object System.Net.Mail.MailMessage
            $message.subject = "Backup Unsuccessful"
            $message.body = "Backup was NOT Successful - File Not Found"
            $message.to.add($to)
            $message.from = $username

            $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
            $smtp.EnableSSL = $true
            $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
            $smtp.send($message)
            write-host "Mail Sent"
}
Tim Griffith
  • 152
  • 2
  • 15
  • How do you even manage to get this bit to work? [System.Io.fileinfo]'E:\GD Backup Folder\backup\Backup_*.zip' – notjustme Feb 19 '15 at 07:51
  • It simply is looking for a file that has the same date as today, and will send an email if yes, or if no. Maybe I can't use a wildcard there? – Tim Griffith Feb 19 '15 at 15:03
  • Try running that single snippet of code on your command line. [System.Io.fileinfo]'E:\GD Backup Folder\backup\Backup_*.zip' – notjustme Feb 19 '15 at 15:05
  • Oh .. Right. So I can't use wildcards in that spot. That's weird it never gave me an error before. It just ran like normal. I figured I would do it the easy way, instead of trying to match one particular file. I thought it would be easier to just look at all of them. – Tim Griffith Feb 19 '15 at 15:30
  • My files are generated in a separate script with the filenames looking like this: Lytec_Backup_02192015.zip I am simply trying to validate that the last one, today's, is present then fire an email stating yes or no. – Tim Griffith Feb 19 '15 at 15:37
  • Since the actual file name is known you could use Test-Path. if(Test-Path -Path "E:\GD Backup Folder\backup\Backup_$([DateTime]::Today.ToString('MMddyyyy')).zip") { "Yup, it's there." } else { "Nope, not there." } – notjustme Feb 19 '15 at 15:50
  • That boolean certainly works much better THANKS! But it still won't fire an email. lol fml – Tim Griffith Feb 19 '15 at 16:01

1 Answers1

0

Send email with attachment using powershell -

     $EmailTo = "udit043.ur@gmail.com"  // abc@domain.com
     $EmailFrom = "udit821@gmail.com"  //xyz@gmail.com
     $Subject = "zx"  //subject
     $Body = "Test Body"  //body of message
     $SMTPServer = "smtp.gmail.com" 
     $filenameAndPath = "G:\abc.jpg"  //attachment
     $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
     $attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
     $SMTPMessage.Attachments.Add($attachment)
     $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
     $SMTPClient.EnableSsl = $true 
     $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("udit821@gmail.com", "xxxxxxxx");    // xxxxxx-password
     $SMTPClient.Send($SMTPMessage)
udit043
  • 1,610
  • 3
  • 22
  • 40
  • I actually could never get this working for whatever reason. I tried your way as well, early on. I ended up rewriting the whole thing in Python. Worked first try. :: shrugs :: – Tim Griffith May 08 '15 at 06:29