title: I have a working powershell script that hangs sending mail when run with the Task Scheduler
body: <p>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.</p>
  
  <p>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?</p>
  
  <p>Here is what I have to send mail </p>
  
  <pre><code>Param($User,$File)
  $User="System_Mangler@familyenrichment.cc"
  $password = Get-Content "SystemMangler.txt" | ConvertTo-SecureString 
  $credential = New-Object System.Management.Automation.PsCredential($user,$password)
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>$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
  </code></pre>