我有一个脚本,它监视AD中的用户证书。它工作正常,但有一点我无法理解。 以下是IF部分:
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
}
}
}
身体设置是
$emailbody = @()
$emailbody += "Certificate bla bla bla expires in $date"
$emailbody += "bla bla bla bla bla"
$emailbody = $emailbody | Out-String
问题是当我从任务调度程序运行脚本时,用户通知没有任何$date
和空的html表。它错过了。但是当我从ISE启动此脚本时,第一次运行结果会丢失$date
和表,但第二个运行按预期工作,$date
和表填充。
这有什么不对吗?
答案 0 :(得分:0)
通过替换变量来解决这个问题。