Powershell为其所有即将到期的用户向Manager发送一封电子邮件
我试图在正文中发送一封邮件,其中包含所有即将过期且与经理匹配的用户,我遇到的问题是,脚本向每个拥有相同经理的用户发送一封邮件,我的老板对我说,这是不可能实现的。 你能帮助我在这个脚本上如何发送像这样的正文html电子邮件Powershell为其所有即将到期的用户向Manager发送一封电子邮件,powershell,active-directory,html-email,Powershell,Active Directory,Html Email,我试图在正文中发送一封邮件,其中包含所有即将过期且与经理匹配的用户,我遇到的问题是,脚本向每个拥有相同经理的用户发送一封邮件,我的老板对我说,这是不可能实现的。 你能帮助我在这个脚本上如何发送像这样的正文html电子邮件 Dear $ Manager, you have the following soon to expire users; User $Name $Email $ExpirationDays User $Name $Email $ExpirationDays User $Name
Dear $ Manager,
you have the following soon to expire users;
User $Name $Email $ExpirationDays
User $Name $Email $ExpirationDays
User $Name $Email $ExpirationDays
User $Name $Email $ExpirationDays
User $Name $Email $ExpirationDays
$Body
这是我在网上找到并根据需要编辑的实际脚本
import-module ActiveDirectory;
Get-ADUser -Filter * -Properties directReports,EmailAddress,Displayname | ForEach {
$ManagerName=$_.Displayname
If ( $_.directReports ) {
$managerEmailAddress = $_.EmailAddress
$_.directReports | ForEach {
$userDetails = Get-ADUser $_ -Properties AccountExpirationDate,accountExpires
$userName=$userdetails.Name
If( $userDetails.accountExpires -eq 0 -or $userDetails.accountExpires -eq 9223372036854775807 ) {
$sendEmail = $false
}
If ( $userDetails.AccountExpirationDate ) {
$ExpiryDate=$userDetails.AccountExpirationDate
$ExpiryDate1=$ExpiryDate.ToShortDateString()
$today=(Get-Date)
$DaysLeft=($ExpiryDate-$today).days
$Date = Get-Date -format "yyyy-MM-dd_HH-mm-ss"
$UserList = Get-ADUser -Filter {sAMAccountName -Like "*_ext" -and enabled -eq $true} -SearchBase 'DC=example,DC=local' -properties AccountExpirationDate,manager | `Where-Object{$_.AccountExpirationDate -lt (Get-Date).AddDays(7) -and $_.AccountExpirationDate -gt (Get-Date).AddDays(1) -and $_.AccountExpirationDate -ne $null} |Select Name,UserPrincipalName,AccountExpirationDate,@{Name="ManagerEmail";Expression={(get-aduser -property emailaddress $_.manager).emailaddress}}
$ReportName = "C:\Users\myusername\Desktop\ExpiringUserReports\Expiring_Users_$Date.xlsx"
$UserList | Export-Excel $ReportName -FreezeTopRow -AutoFilter -AutoSize -BoldTopRow
$Body = "
<html>
<body>
<p>Dear $ManagerName,<br>
The user account <strong>$userName</strong>, will expire in <strong>$DaysLeft days</strong>.<br>
You are being notified because our records show that you are the primary contact Manager for this user.<br>
Please take action to extend the validity before expiration.<br />
Download the following <a href='https://examplespa-my.mysite.com/:x:/g/personal/myyser_example_com/file.xslx'> Consultants Template </a> file and send back to us with updated expiration date PO.<br>
Having any issues, don't hesitate to <font color=104160 size=5> ✉</font> us.</p>
<p>Regards<br />
<img src='https://www.example.it/example/logo.jpg' ><br>
<b><span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>Help Desk
Team</span></b><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'><o:p></o:p></span><br />
<b><span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>example
</span></b><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span><br />
<p style='min-height:10px'><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span>
<span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>Via V.
</span><span style='font-size:6.0pt;font-family:
'Lucida Grande';color:#00578C'></span><br />
<span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>T.
+</span><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span><br />
<span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>F.
+39 06 </span><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span>
<p style='min-height:10px'><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span>
<span style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'><a
href='mailto:helpdesk@example.com'><span style='font-size:9.0pt'>helpdesk@example.com</span></a></span><span
style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'> </span><span
style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'></span><br />
<span style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'><a
href='http://www.example.com/'><span style='font-size:9.0pt'>www.example.com</span></a></span><span
style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'></span><br>
© ®
</body>
</html>"
If ($DaysLeft -le 2 -and $DaysLeft -ge 0) {
$sendEmail = $true
}
}
If ( $sendEmail ) {
# i am not using this right now as i don't want to spam managers while i am testing script.
send-mailmessage -To $managerEmailAddress -From helpdesk@example.com -Subject "Users Expiry notification. Action is required" -body "$Body $body1" -smtpserver "example-smtp.com" -BodyAsHtml -Attachments $ReportName
# This is i am using just to see the output
"{0,-28} {1,-20} {2,-18} {3,-8}" -f $managerEmailAddress,$userdetails.SamAccountName,$ExpiryDate1,$DaysLeft
}
$sendEmail = $false
}
}
}
导入模块ActiveDirectory;
获取ADUser-Filter*-属性directReports、EmailAddress、Displayname | ForEach{
$ManagerName=$\u.Displayname
如果($\u0.directReports){
$managerEmailAddress=$\电子邮件地址
$\ directReports | ForEach{
$userDetails=Get ADUser$\属性AccountExpirationDate,accountExpires
$userName=$userdetails.Name
如果($userDetails.accountExpires-eq 0-或$userDetails.accountExpires-eq 9223372036854775807){
$sendmail=$false
}
If($userDetails.AccountExpirationDate){
$ExpiryDate=$userDetails.AccountExpirationDate
$ExpiryDate1=$ExpiryDate.ToShortDateString()
$today=(获取日期)
$DaysLeft=($ExpiryDate-$today)。天
$Date=获取日期-格式为“yyyy-MM-dd_HH-MM-ss”
$UserList=Get ADUser-Filter{sAMAccountName-Like“*_ext”-并启用-eq$true}-SearchBase'DC=example,DC=local'-properties-AccountExpirationDate,manager | ` Where Object{$$\ AccountExpirationDate-lt(Get Date).AddDays(7)-和$\ AccountExpirationDate-gt(Get Date).AddDays(1)-和$\ AccountExpirationDate-ne$null}|选择名称、UserPrincipalName、AccountExpirationDate,@{Name=“ManagerEmail”;表达式={(get aduser-property emailaddress$\ u0.manager).emailaddress}}
$ReportName=“C:\Users\myusername\Desktop\ExpiringUserReports\Expiring\u Users\uu$Date.xlsx”
$UserList |导出Excel$ReportName-冻结TopRow-自动筛选-自动调整大小-粗体TopRow
$Body=”
亲爱的$ManagerName,
用户帐户$userName将在$DaysLeft天后过期
我们会通知您,因为我们的记录显示您是此用户的主要联系人经理。
请在到期前采取措施延长有效期。
下载以下文件并将更新的到期日PO发送回我们。
如果有任何问题,请随时与我们联系
问候
服务台
团队
例子
经由。
T
+
F
+39 06
© ®
"
如果($DaysLeft-le 2-和$DaysLeft-ge 0){
$sendmail=$true
}
}
如果($sendmail){
#我现在不使用它,因为我不想在测试脚本时对管理器发送垃圾邮件。
发送邮件-至$managerEmailAddress-发件人helpdesk@example.com-Subject“用户到期通知。需要执行操作”-body“$body$body1”-smtpserver“example smtp.com”-BodyAsHtml-附件$ReportName
#这是我用来查看输出的
“{0,-28}{1,-20}{2,-18}{3,-8}”-f$managerEmailAddress,$userdetails.SamAccountName,$expireydate1,$DaysLeft
}
$sendmail=$false
}
}
}
找到了解决方案,
这是最后的代码
import-module ActiveDirectory;
Get-ADUser -Filter * -SearchBase 'OU=TestExpire,DC=AdlabTest,DC=local' -Properties directReports,EmailAddress,Displayname | ForEach {
$ManagerName=$_.Displayname
$Body = "
<html>
<body>
<p>Dear $ManagerName<br/>
You are being notified because our records show that you are the primary contact Manager for the below listed users.<br>
<style>
TABLE {border-width: 1px; border-style: solid; border-color: black; border-=collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
TD {border-width: 1px; padding: 1px; border-style: solid; border-color: black;}
</style>
<table>
<tbody>
<tr><th>Name</th><th>UserName</th><th>AccountExpires</th></tr>";
$AddBody = "";
If ( $_.directReports ) {
#Write-Output("Processing : " + $ManagerName);
$ToEmail = $_.EmailAddress
$_.directReports | ForEach {
$userDetails = Get-ADUser $_ -Properties AccountExpirationDate,accountExpires,EmailAddress
$userName=$userdetails.Name
$userEmail=$userdetails.userPrincipalName
If( $userDetails.accountExpires -eq 0 -or $userDetails.accountExpires -eq 9223372036854775807 ) {
$sendEmail = $false
}
If ( $userDetails.AccountExpirationDate ) {
$ExpiryDate=$userDetails.AccountExpirationDate
$ExpiryDate1=$ExpiryDate.ToShortDateString()
$today=(Get-Date)
$DaysLeft=($ExpiryDate-$today).days
If ($DaysLeft -le 40 -and $DaysLeft -ge 0) {
$AddBody += "<tr><td>$userName</td> <td><a style='text-decoration:none;color: rgb(0, 0, 0);'>$userEmail</a></td> <td>$ExpiryDate1</td> </tr>";
$sendEmail = $true
}
}
}
If ( $sendEmail ) {
$Body +=$AddBody;
$Body = $Body + "</tbody>
</table><br>
Please take action as soon as possible to extend the validity before expiration.<br />
Download the following <a href='https://link_to_file.xslx'>Template</a> file and send back to us with updated expiration date PO.<br>
Having any issues, don't hesitate to <font color=104160 size=5> ✉</font> us.</p>
<p>Regards<br />
<img src='https://www.link_to_your_site/logo.jpg' ><br>
<b><span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>Help Desk
Team</span></b><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'><o:p></o:p></span><br />
<b><span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>Your Business Name</span></b><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span><br />
<p style='min-height:10px'><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span>
<span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>Your Business address </span><span style='font-size:6.0pt;font-family:
'Lucida Grande';color:#00578C'></span><br />
<span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>T.
+tout phone</span><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span><br />
<span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>F.
+your fax</span><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span>
<p style='min-height:10px'><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span>
<span style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'><a
href='mailto:helpdesk@link_your_site.com'><span style='font-size:9.0pt'>helpdesk@link_your_site.com</span></a></span><span
style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'> </span><span
style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'></span><br />
<span style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'><a
href='http://www.link_your_site.com/'><span style='font-size:9.0pt'>http://www.link_your_site.com</span></a></span><span
style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'></span><br>
© ®
</body>
</html>";
send-mailmessage -To $ToEmail -From expiry@link_your_site.comm -Subject "Users Expiry notification. Action is required" -body $Body -smtpserver "link_your_site.smtp" -BodyAsHtml -Priority High
}
}
}
导入模块ActiveDirectory;
获取ADUser-Filter*-SearchBase'OU=TestExpire,DC=AdlabTest,DC=local'-Properties directReports,EmailAddress,Displayname | ForEach{
$ManagerName=$\u.Displayname
$Body=”
亲爱的$ManagerName
我们会通知您,因为我们的记录显示您是下列用户的主要联系人经理。
表{边框宽度:1px;边框样式:实心;边框颜色:黑色;边框-=折叠:折叠;}
{边框宽度:1px;填充:3px;边框样式:纯色;边框颜色:黑色;背景颜色:#6495ED;}
TD{边框宽度:1px;填充:1px;边框样式:纯色;边框颜色:黑色;}
NameUserNameAccountExpires”;
$AddBody=“”;
如果($\u0.directReports){
#写入输出(“处理:+$ManagerName);
$ToEmail=$\电子邮件地址
$\ directReports | ForEach{
$userDetails=Get ADUser$\属性AccountExpirationDate、accountExpires、EmailAddress
$userName=$userdetails.Name
$userEmail=$userdetails.userPrincipalName
如果($userDetails.accountExpires-eq 0-或$userDetails.accountExpires-eq 9223372036854775807){
$sendmail=$false
}
If($userDetails.AccountExpirationDate){
$ExpiryDate=$userDetails.AccountExpirationDate
$ExpiryDate 1=$ExpiryDate.ToShortDateS