title: Powershell script works in Powershell but fails in Task Scheduler
body: <p>I have a PowerShell script that sends an email via SMTP.  The script runs fine inside Powershell ISE, but fails in Task Scheduler.  I am on Windows Server 2012.  I have other Powershell scripts that I run on this server using the exact same setup, but those scripts do not send an email.  The return code I see in Task Scheduler is (0xFFFD0000) and I cannot find any information on this.  I have the task set to run with highest privileges and I have checked that the executionpolicy is RemoteSigned.  Anybody run into this before?</p>
  
  <p>Here is the command in the task: </p>
  
  <pre><code>powershell -f "c:\scripts\EmailTest.ps1"
  </code></pre>
  
  <p>Here is the script:</p>
  
  <pre><code>$EmailFrom = "user@domain.com"
  $EmailTo = "someone@somewhere.com"
  $Subject = "Email Subject" 
  $Body = @"
  Person,
  
  Some message here
  
  Thanks,
  User
  "@
  
  $SMTPServer = "smtp.domain.com" 
  $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) 
  $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("user@domain.com", "password"); 
  $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
  </code></pre>
  
  <p>Update:
  I was able to resolve the issue.  Apparently I had an additional line in the script that was commented out.  I'm not sure why this would cause an error but once I removed that commented out line it ran fine in Task Scheduler.  the comment looked like this and was just below the other $EmailTo declaration in the above script:</p>
  
  <pre><code>#$EmailTo = "someone@somewhere.com"
  </code></pre>