get-date doesn't work from task scheduler
<p>I have a script, that monitors user certificates in AD. It works fine, but one thing I can't understand.
Here's the IF section:</p>

<pre><code>If ($Certs.expiresin -contains $days)
{
$strings = $Certs | where {$_.expiresin -eq $days}
$table = $strings | select @{n='Username';e={$_.Fullname}}, @{n='Email';e={$_.subject}}, @{n='Expires';e={$_.expiresafter}}
Send-MailMessage @toadmin -Body $toadminbody -BodyAsHtml -Encoding $encoding

foreach ($string in $strings)     {
$To = $string.subject
$smtpsettings = @{
To = $To
From = $From
Subject = $Subject
SmtpServer = $SmtpServer
} 
 if ($string.subject -like '*@*' )
{
$date = Get-date $string.ExpiresAfter -f D
Send-MailMessage @smtpsettings -body $emailbody -Encoding $encoding
}
   }
 }
</code></pre>

<p>Body settings is</p>

<pre><code>$emailbody = @()
$emailbody += "Certificate bla bla bla expires in $date"
$emailbody += "bla bla bla bla bla"
$emailbody = $emailbody | Out-String
</code></pre>

<p>The problem is when I run script from task scheduler, user notification comes without any <code>$date</code> and with an empty html table. It just missed. But when I start this script from ISE, the first run result comes with missing <code>$date</code> and table, but the second one works as intended, <code>$date</code> and table are filled.
Whats wrong with it?</p>