【发布时间】:2021-02-04 20:57:54
【问题描述】:
我有一个脚本,如果他们的帐户将在接下来的 30 天内到期,它将向员工的经理发送一封电子邮件。我正在逐个提取每个帐户 我已经代表支持部门向他们的经理发送了一封包含员工姓名和到期日期的 HTML 格式的电子邮件。
但我不想将通知邮件一一发送给他们的经理。例如,有时可能有 30 到 50 个用户从 Active Directory 向经理直接和间接报告。所以我的脚本将为每个过期用户发送邮件给经理。他们不想要这个。
我的问题是: 1- 我如何发送通知邮件这两个帐户将在接下来的 30 天内到期并批量发送给他们的经理而不是一个一个?
例如(John、Michael、Andy、Aaron 直接向 theirmanager@domain.com 汇报)
To: John, Michael, Andy, Aaron
Cc: theirmanager@domain.com
2- 我想发送 HTML 格式的邮件而不是短信。
消息:
"***The network account for $($name.name) will be expiring on $(($name.AccountExpirationDate).ToShortDateString()), as their manager, could you please confirm they will still be continuing with their role in order to extend their account.*** "
我想要的输出:
例如 HTML 表格格式
name SamAccountName AccountExpirationDate EmployeeID
John PRD192888 1/31/2021 12:00:00 AM 102928
Michael PRD192876 2/14/2021 12:00:00 AM 102924
Andy PRD192834 1/12/2021 12:00:00 AM 102934
Aaron PRD192234 1/31/2021 12:00:00 AM 102679
脚本:
$user = Search-ADAccount -AccountExpiring -TimeSpan 30.00:00:00 | Where-Object { ($_.Enabled -eq $true) -and ($_.samAccountName -notlike "SCN*") -and ($_.samAccountName -notlike "CVB*") -and ($_.samAccountName -notlike "PCN*") }
#pulling each account one by one and check to make sure they have a manager assigned. If not those names are piped into a file
foreach ($name in $user){
If (( Get-ADUser -Identity $name.SamAccountName -Properties *).Manager -eq $null) {
Write-Host "no manager listed for" $name.name ;
Add-Content -Path \\path\Folder\No_manager_listed.txt `
-Value "$(($name.AccountExpirationDate).ToShortDateString())`t $($name.name)"
}
#accounts with manager are taken one at a time by username to pull out their managers email address.
#this will also send them an email with the employee's name and expiration date on behalf of desktop Support to thier manager
else {
$manager = ( Get-ADUser -Identity $name.SamAccountName -Properties * ).Manager
$manager_email = (Get-ADUser -Identity $manager -Properties *).mail
Write-Host "sending notification email to" $manager_email;
Send-MailMessage -From DesktopSupport@urdomain.com `
-Subject "Team member account will expire soon" `
-To $manager_email `
-Body "***The network account for $($name.name) will be expiring on $(($name.AccountExpirationDate).ToShortDateString()), as their manager, could you please confirm they will still be continuing with their role in order to extend their account. If they will need an extension, please reply to this email with your approval and we can extend their accounts another 90 days. If the listed employee is to be transitioned over to a permanent FTE with urdomain, please forward your response to HumanResources@urdomain.com in order to begin the process. Thank you.*** " `
-BodyAsHtml -Priority High -SmtpServer postmaster.urdomain.com
}
}
#this section will email the file that was created from the employees with no manager and email it to desktop support
if (Test-Path \\path\Folder\No_manager_listed.txt){
Send-MailMessage -From DesktopSupport@urdomain.com `
-Subject "Expiring accounts with no manager listed report " `
-To IT-DesktopSupport@urdomain.com `
-Attachments \\path\Folder\No_manager_listed.txt `
-Body "****This an automated message. Please review the attachment.***" `
-BodyAsHtml -Priority High -SmtpServer postmaster.urdomain.com ;
Start-Sleep -Seconds "10"
Remove-Item -Path \\path\Folder\\No_manager_listed.txt
}
【问题讨论】:
标签: powershell