locked
Perform different action if variable returns NULL RRS feed

  • Question

  • I have a script that I hobbled together that sends an email with every PDF in a specific folder once a report from a different program produces them as output.  Sometimes, the reporting program does not produce any output and therefore, no PDF files are attached to the email.  However, the function is called automatically as a nightly task, and the e-mail is still sent without attachments, but the subject line still reads as though there are attachments and this is causing some confusion.  I am new to PowerShell, and I was wondering if there was a way to have the function exit and call a different function if there are no files in the target folder to attach, or to have the equivalent of an IF/THEN/ELSE conditional statement that forks the execution. Here is the entire function (real names and email addresses replaced with bogus ones for security).  I am NOT a programmer by profession, so I apologize for my ignorance and form in advance.

    Function SendPDFList
    {
    $Username = "someone@somewhere.net"
    $pwfile = "E:\backup\BackupScripts\EmailPassword.txt"
    $credentials=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, (Get-Content $pwfile | ConvertTo-SecureString)
    $EmailTo = "Some Suit <SomeSuit@somewhere.fu>"
    $EmailFrom = "Overworked SysAdmin <SysAdmin@somewhere.fu>"
    $Subject = "Daily TPS Reports in PDF" 
    $Body = "Please See Attached TPS Reports"
    $PDFPath = "E:\TPSReportDump" 
    $SMTPServer = "smtp.somewhere.fu" 
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $files=Get-ChildItem($PDFPath)
    Foreach($pdf in $files)
     {
    $attachment = New-Object System.Net.Mail.Attachment –ArgumentList E:\TPSReportDump\$pdf
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
    $SMTPClient.EnableSsl = $true 
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($credentials.UserName, $credentials.Password); 
    
    
      }
      $SMTPClient.Send($SMTPMessage)
    }

    Thank you in advance!

    TD



    Tom Davidson

    Friday, March 30, 2018 8:50 PM

All replies

  • I have a script that I hobbled together that sends an email with every PDF in a specific folder once a report from a different program produces them as output.  Sometimes, the reporting program does not produce any output and therefore, no PDF files are attached to the email.  However, the function is called automatically as a nightly task, and the e-mail is still sent without attachments, but the subject line still reads as though there are attachments and this is causing some confusion.  I am new to PowerShell, and I was wondering if there was a way to have the function exit and call a different function if there are no files in the target folder to attach, or to have the equivalent of an IF/THEN/ELSE conditional statement that forks the execution. Here is the entire function (real names and email addresses replaced with bogus ones for security).  I am NOT a programmer by profession, so I apologize for my ignorance and form in advance.

    Function SendPDFList
    {
    $Username = "someone@somewhere.net"
    $pwfile = "E:\backup\BackupScripts\EmailPassword.txt"
    $credentials=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, (Get-Content $pwfile | ConvertTo-SecureString)
    $EmailTo = "Some Suit <SomeSuit@somewhere.fu>"
    $EmailFrom = "Overworked SysAdmin <SysAdmin@somewhere.fu>"
    $Subject = "Daily TPS Reports in PDF" 
    $Body = "Please See Attached TPS Reports"
    $PDFPath = "E:\TPSReportDump" 
    $SMTPServer = "smtp.somewhere.fu" 
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $files=Get-ChildItem($PDFPath)
    Foreach($pdf in $files)
     {
    $attachment = New-Object System.Net.Mail.Attachment –ArgumentList E:\TPSReportDump\$pdf
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
    $SMTPClient.EnableSsl = $true 
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($credentials.UserName, $credentials.Password); 
    
    
      }
      $SMTPClient.Send($SMTPMessage)
    }

    Thank you in advance!

    TD



    Tom Davidson

    Just add IF/else statement

    $files=Get-ChildItem($PDFPath)
    
    if ($files.count -gt 0 )
    {
    
    Foreach($pdf in $files)
     {
    $attachment = New-Object System.Net.Mail.Attachment –ArgumentList E:\TPSReportDump\$pdf
    $SMTPMessage.Attach
    
    ....
    
    }

    Friday, March 30, 2018 9:25 PM
  • .. and try to use send-mailmessage cmdlet... a minor hint  

    Friday, March 30, 2018 9:34 PM