通过1封电子邮件将未登录超过30天的用户名电子邮件列表发送给其经理

时间:2015-02-02 20:34:19

标签: powershell-v4.0

我有一个脚本,我被要求写,让我完全难过。我们正在尝试禁用未登录超过30天的用户。在禁用之前,我们希望在闲置16天后拉出列表,并通知管理员如果他们在接下来的两周内没有登录,该帐户将被停用。我有它工作,它发出并发送电子邮件给管理员在每个单独的电子邮件中的ID,但他们更愿意,如果我可以发送1封电子邮件与该经理下的所有用户的列表。有任何想法吗?我无法在网上找到任何东西。以下是我到目前为止:

$whencreated = ((Get-Date).AddDays(-16)).Date
$whenlastlogon = ((Get-Date).AddDays(-16)).Date
$users = Get-QADUser -Enabled -SizeLimit 0 -IncludedProperties LastLogonTimeStamp,DistinguishedName,ExtensionAttribute9,extensionAttribute8,extensionattribute7,whencreated,companycode,pwdlastset,manager,mail,samaccountname -SearchRoot "ou=test2,ou=people,dc=company,dc=com" | where {$_.whencreated -le $whencreated -and $_.lastlogontimestamp -le $whenlastlogon } | Group-Object Manager
select Name,LastLogonTimestamp,manager,samaccountname | sort name | foreach {
$to = Get-ADUser $_.manager -Properties Mail | select -Expand mail 
$smtpserver = "mailhub.company.com"
$From = "AccountDisableNotification <AccountDisableNotification@company.com>" 
$subject = "Account $($_.SamAccountName) has not logged into the (company) Windows Domain in 14 days" 
$body = "Hello,<br><br>"
$body += "In line with (registrar) requirements, the (department) area is reviewing IDs that have not accessed Active Directory (Windows) for more than 30 days. You are receiving this email because you are the owner of the account in question.<br><br>"
$body += "$($_.Name), $($_.samaccountname), has not been used for at least two (2) weeks. If the ID is not used within another two weeks, the ID will be disabled.<br><br>"
$body += "This change will require a request to be submitted to the (department) to have the ID's re-enabled prior to use.<br><br>"
$body += "Thank you.<br><br><br>"
$body += "<b><big><font color=red>If the ID(s) is no longer needed and can be deleted from the system, please submit a  request to have the ID removed.<b><br><br><br></font>"
$body += "<b>Please do not reply to the e-mail address above. No one will respond to any correspondence to this post only address.<br><br><br>"   
Send-MailMessage -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -SmtpServer $smtpserver

0 个答案:

没有答案
相关问题