none
Powershell script sends e-mail in ISE but not from powershell command line RRS feed

  • Question

  • I have created a script that generates a report of all users who do not have photos in Active Directory. The script runs and sends an e-mail to a distribution list. It runs fine in ISE, but I cannot run it from command line or as a scheduled task. Any suggestions what I am doing wrong?

    #Starts Powershell Snap-In
    ############################################################################

    Start-Transcript -path c:\Scripts\StaffWithoutPhotos.txt
    add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010 -erroraction silentlyContinue

    #---------------------------------------------------------------------------


    $mailboxes = $null

    $mailboxes = @(Get-Mailbox -OrganizationalUnit "Staff User Accounts" -ResultSize Unlimited | Where {$_.HasPicture -eq $False})

    Write-Host $mailboxes

    $report = @()

    foreach ($mailbox in $mailboxes)
    {
    $mbObj = New-Object PSObject
        $mbObj | Add-Member -MemberType NoteProperty -Name "Display Name" -Value $mailbox.DisplayName
        $report += $mbObj
    }

    Write-Host "Diagnostic Output"
    Write-Host "Before Mailbox value check"
    Write-Host $report
    Write-Host "After Mailbox Value check"

    if ($mailboxes -ne $null)
    {
        Write-Host $report

        #Send the report generated to the e-mail list specified above
        ############################################################################

        $style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
        $style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
        $style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
        $style = $style + "TD{border: 1px solid black; padding: 5px; }"
        $style = $style + "</style>"

        #Start-Sleep -Seconds 5
        $smtpserver = "MAIL.contoso.com"

        $msg = new-object Net.Mail.MailMessage
        $smtp = new-object Net.Mail.SmtpClient($smtpServer)
        $msg.From = "exchangereports@contoso.com"
        $msg.To.Add("DL.StaffPhotos@contoso.com")
        #$msg.To.Add("jtest@contoso.com") #Used for testing
        $msg.Subject = "Staff without Photos report"
        $msg.Body = "This report contains the list of users who do not currently have photos."
        $msg.Body += "<p>"
        $msg.Body += "After photos are available for these users please submit a request to have their accounts updated or send an e-mail to <a href='mailto:ithelpdesk@contoso.com?Subject=Staff%20Photo%20Updates' target='_top'> ITHelpDesk@contoso.com </a> with <b>Staff Photo Updates</b> in the subject."
        $msg.Body += "<p>"
        $msg.Body += "This report runs on Fridays and will only send a message if we have Staff without Photos. For more information please contact your systems administrator."
        $msg.Body += "<p>"

        $msg.Body += $Report | ConvertTo-Html -Head $Style

        $msg.IsBodyHTML = $true
        $smtp.Send($msg)
    }
    Stop-Transcript

    Friday, April 11, 2014 5:56 PM

Answers

  • So after researching further and not receiving any more replies I decided to rewrite the script and attempt to use the send-mailmessage function. I have been unable to determine the reason for the original not script not working. We use a very similar script to my original code which works to generate a report and e-mail that report as an attachment and it works without any problems.

    After re-writing the script and using the send-mailmessage function I was able to get this report to work as expected. This has now been set up as a scheduled task to run on a server once a week. Here is the final code:

    Start-Transcript -path c:\Scripts\StaffWithoutPhotos.txt
    #Starts Powershell Snap-In
    ############################################################################
    add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010 -erroraction silentlyContinue

    #---------------------------------------------------------------------------

    $mailboxes = $null

    $mailboxes = @(Get-Mailbox -OrganizationalUnit "contoso Staff User Accounts" -ResultSize Unlimited | Where {$_.HasPicture -eq $False})

    Write-Host $mailboxes

    $report = @()

    foreach ($mailbox in $mailboxes)
    {
    $mbObj = New-Object PSObject
        $mbObj | Add-Member -MemberType NoteProperty -Name "Display Name" -Value $mailbox.DisplayName
        $report += $mbObj
    }

    Write-Host "Diagnostic Output"
    Write-Host "Before Mailbox value check"
    Write-Host $report
    Write-Host "After Mailbox Value check"

    if ($mailboxes -ne $null)
    {
        Write-Host $report

        #Send the report generated to the e-mail list specified above
        ############################################################################

        $style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
        $style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
        $style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
        $style = $style + "TD{border: 1px solid black; padding: 5px; }"
        $style = $style + "</style>"

        $smtpserver = "MAIL.contoso.com"
        $smtpFrom = "exchangereports@contoso.com"
        $smtpTo = "DL.StaffPhotos@contoso.com" 
        $messageSubject = "Staff without Photos report"

        $Body = "This report contains the list of users who do not currently have photos."
        $Body += "<p>"
        $Body += "After photos are available for these users please submit a request to the Help Desk to have their accounts updated or send an e-mail to <a href='mailto:ithelpdesk@contoso.com?Subject=Staff%20Photo%20Updates' target='_top'> ITHelpDesk@contoso.com </a> with <b><u>Staff Photo Updates</b></u> in the subject."
        $Body += "<p>"
        $Body += "This report runs on Fridays and will only send a message if we have Staff without Photos. For more information please contact your systems administrator."
        $Body += "<p>"

        $Body += $Report | ConvertTo-Html -Head $Style


        #Send the report
        send-mailmessage -from "$smtpFrom" -to "$smtpTo" -subject "$messageSubject" -body "$Body" -smtpServer "$smtpserver" -BodyAsHtml

    }
    Stop-Transcript

    • Marked as answer by JeeNz Tuesday, May 13, 2014 3:29 PM
    Tuesday, May 13, 2014 3:29 PM

All replies

  • What's happening when it runs? Are you getting any errors returned or does it simply not do anything?

    Out of interest, is there a reason you're not simply sending the message using Send-MailMessage - http://technet.microsoft.com/en-us/library/hh849925.aspx - rather than calling the .Net objects?

    Friday, April 11, 2014 7:59 PM
  • Thank you for the response

    This report is based on one that we have already created and works perfectly. That is the only reason for the .Net objects.

    The script does not give any errors when run from the CLI or in ISE, but only when it runs in ISE do I get the e-mail message. When it runs in the CLI no e-mail is generated, though the diagnostic outputs section I put in display the values expected.

    From my research is sounds like it may be a problem with a global variable but I don't know what I need to do to resolve it.

    Friday, April 11, 2014 8:50 PM
  • Ahh fair enough, I definitely can't argue with that reasoning. Why fix what already works.

    One thing that comes to mind in terms of difference between the CLI and ISE is whether there's differences between the profiles. The two of them have their own profile setups, so if something has been customised in one it won't automatically appear in the other.

    The easiest way to check is to run :

    notepad $profile

    in both the CLI and the ISE. If nothing has been setup then you'll get an error saying the file can't be found (that's fine), but if customisations have been configured they'll show in the resulting files that open, and if one has a profile but the other doesn't or they both have them and they're different than that could be your issue.

    Friday, April 11, 2014 9:00 PM
  • I attempted to run this command with both the CLI and ISE on the same system I am trying to run the script under, both result in notepad opening and an error message of "The system cannot find the path specified"
    Monday, April 14, 2014 12:25 PM
  • Ahh, do much for that idea then, if they both generated that error then neither has a custom profile setup. In which which case as far as I'm aware their settings should be the same.
    Monday, April 14, 2014 12:40 PM
  • So after researching further and not receiving any more replies I decided to rewrite the script and attempt to use the send-mailmessage function. I have been unable to determine the reason for the original not script not working. We use a very similar script to my original code which works to generate a report and e-mail that report as an attachment and it works without any problems.

    After re-writing the script and using the send-mailmessage function I was able to get this report to work as expected. This has now been set up as a scheduled task to run on a server once a week. Here is the final code:

    Start-Transcript -path c:\Scripts\StaffWithoutPhotos.txt
    #Starts Powershell Snap-In
    ############################################################################
    add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010 -erroraction silentlyContinue

    #---------------------------------------------------------------------------

    $mailboxes = $null

    $mailboxes = @(Get-Mailbox -OrganizationalUnit "contoso Staff User Accounts" -ResultSize Unlimited | Where {$_.HasPicture -eq $False})

    Write-Host $mailboxes

    $report = @()

    foreach ($mailbox in $mailboxes)
    {
    $mbObj = New-Object PSObject
        $mbObj | Add-Member -MemberType NoteProperty -Name "Display Name" -Value $mailbox.DisplayName
        $report += $mbObj
    }

    Write-Host "Diagnostic Output"
    Write-Host "Before Mailbox value check"
    Write-Host $report
    Write-Host "After Mailbox Value check"

    if ($mailboxes -ne $null)
    {
        Write-Host $report

        #Send the report generated to the e-mail list specified above
        ############################################################################

        $style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
        $style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
        $style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
        $style = $style + "TD{border: 1px solid black; padding: 5px; }"
        $style = $style + "</style>"

        $smtpserver = "MAIL.contoso.com"
        $smtpFrom = "exchangereports@contoso.com"
        $smtpTo = "DL.StaffPhotos@contoso.com" 
        $messageSubject = "Staff without Photos report"

        $Body = "This report contains the list of users who do not currently have photos."
        $Body += "<p>"
        $Body += "After photos are available for these users please submit a request to the Help Desk to have their accounts updated or send an e-mail to <a href='mailto:ithelpdesk@contoso.com?Subject=Staff%20Photo%20Updates' target='_top'> ITHelpDesk@contoso.com </a> with <b><u>Staff Photo Updates</b></u> in the subject."
        $Body += "<p>"
        $Body += "This report runs on Fridays and will only send a message if we have Staff without Photos. For more information please contact your systems administrator."
        $Body += "<p>"

        $Body += $Report | ConvertTo-Html -Head $Style


        #Send the report
        send-mailmessage -from "$smtpFrom" -to "$smtpTo" -subject "$messageSubject" -body "$Body" -smtpServer "$smtpserver" -BodyAsHtml

    }
    Stop-Transcript

    • Marked as answer by JeeNz Tuesday, May 13, 2014 3:29 PM
    Tuesday, May 13, 2014 3:29 PM