I have a paperless system for mileage reimbursement sheets. We have had some issues with people submitting multiple sheets and to help supervisors check for that I created a PowerShell script that does an SQL query and creates a text file with all of the supervisors. Then, it reads that list and runs another SQL query to get all of their employees names and date ranges of previously submitted mileage sheets, saves that to a CSV file and emails it to the supervisor so they can check it when approving the next set of sheets.
When I run the script from the command line it works great. I want to schedule it to run weekly. When I test it, however, it hangs. It creates the first file of supervisors. After doing some testing, (I commented out the section that sends mail) it hangs sending the first email message. I have the task scheduled to run with the same credentials I used to create the credentials file. Any suggestions?
Here is what I have to send mail
Param($User,$File)
$User="System_Mangler@familyenrichment.cc"
$password = Get-Content "SystemMangler.txt" | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential($user,$password)
That is at the very beginning of the script. This is in the loop that sends mail. For testing purposes I am having it send everything to me rather than users.
$From = "System_Mangler@familyenrichment.cc"
$To = "ebosworth@familyenrichment.cc"
$Attachment = "c:\backup\tools\testing.csv"
$Subject = "Mileage Date Ranges"
$Body = "Here is a list of your employees and dates of previously submitted mileage sheets. When approving mileage sheets, Please check to make sure this is not a duplicate. `r`n Thank you `r`n The System Mangler"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
Send-MailMessage -From $From -To $To -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -UseSsl -Port $SMTPPort `
-Credential $Credential -Attachments $Attachment