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