PowerShell - цикл foreach каждые 1 минуту - PullRequest
0 голосов
/ 01 июня 2018

У меня есть скрипт, который запрашивает AD, и если он находит пользователей, созданных в течение 24 часов, он отправляет им электронное письмо.У меня эта часть работает нормально, но я не могу понять, почему 1) она работает построчно в окнах PowerShell, но не выполняет тот же код, что и файл .ps1;и 2) почему поле $ name в $ mailBody не отображается вообще.Он отображает $ username, но не $ name.Я выложу сообщение об ошибке под кодом.

clear
Import-Module ActiveDirectory
$secpasswd = ConvertTo-SecureString "xxxx" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("xxx@xxx.com", $secpasswd)
$PSEmailServer = "xxx@xxx.com"
$SMTPPort = 587
$SMTPUsername = "xxx@xxx.com"
$MailFrom = "xxx@xxx.com"
$MailSubject = "Your user account has been created"

#Pull users created within last 24 hours

$when =  ((get-date).AddDays(-2)).addMinutes(-0) #past 24 hours from current time
$when
$users = Get-ADUser -Filter {whenCreated -ge $when} -Properties whenCreated,mail,givenname,surname,SAMAccountName,info | Select-Object whenCreated,mail,givenname,surname,SAMAccountName,info | where info -ne true

Foreach ($user in $users) 
{
$users
$name = $user.name
$emailTo = $user.mail
$username = $user.SAMAccountName
$MailBody =" <p>Dear $name,
Your CDAT Account has been created. <br><br>
Your Username is $username <br><br>
Please contact CDAT support for further assistance. <br><br>
CDAT Support Team <br><br>

This email was auto-generated. Please do not reply. If you need assistance, please contact CDAT Support. <br><br>
Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s), and may contain confidential and privileged information. Any unauthorized review, use, disclosure, or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.<br><br>
</P>"
    Send-MailMessage -From $MailFrom -To $emailTo -Subject $MailSubject -Body $MailBody -BodyAsHtml -Port $SMTPPort -UseSsl -Credential $mycreds
    Set-AdUser -identity $username –replace @{info=”true”}


}

"PS C:\scripts> .\EmailUsers24HoursAfterAccountCreation.ps1
At C:\scripts\EmailUsers24HoursAfterAccountCreation.ps1:33 char:36
+         Set-AdUser -identity $username –replace @{info=â€trueâ€}
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~
The string is missing the terminator: ".
At C:\scripts\EmailUsers24HoursAfterAccountCreation.ps1:18 char:2
+     {
+     ~
Missing closing '}' in statement block or type definition.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...