Powershell 构造带有表格的HTML邮件

网友投稿 878 2022-09-12

Powershell 构造带有表格的HTML邮件

Powershell 构造带有表格的HTML邮件

场景

定期查询AD中的用户,过滤出所有被锁定的账号,生成一个表格,并发送给IT同学。

实现

直接上脚本

## 查询所有锁定用户的SamAccountName $account_locked = (Search-ADAccount -LockedOut -SearchBase "ou=employees,dc=123,dc=com" -ResultSetSize 1000).SamAccountName ## $Report = @() ## 定一个空数组 Foreach ($i in $account_locked){ $userinfo = Get-ADUser -id $i -Properties Name,SamAccountName,DisplayName,mail,LastLogonDate $displayname = $userinfo.DisplayName $email = $userinfo.mail $samaccount = $i $lastlogondate = $userinfo.LastLogonDate ## 自定义个PS对象遍历存储所有用户信息 $Report += New-Object -TypeName psobject -Property @{ SamAccountName = $samaccount DisplayName = $displayname Email = $email LastLogonDate = $lastlogondate } } $Report = $Report | Sort-Object LastLogonDate ## 定义一个数据表 $DataTable = New-Object System.Data.DataTable "DataTable" $col1 = New-Object system.Data.DataColumn LogonName,([string]) $col2 = New-Object system.Data.DataColumn DisplayName,([string]) $col3 = New-Object system.Data.DataColumn Email,([string]) $col4 = New-Object system.Data.DataColumn LastLogonDate,([string]) ## 增加列 $DataTable.Columns.Add($col1) $DataTable.Columns.Add($col2) $DataTable.Columns.Add($col3) $DataTable.Columns.Add($col4) ## 增加数据行 foreach ($entry in $Report){ $row = $DataTable.NewRow() $row.LogonName = $entry.SamAccountName $row.DisplayName = $entry.DisplayName $row.Email = $entry.Email $row.LastLogonDate = $entry.LastLogonDate $DataTable.Rows.Add($row) } #Write-Output $DataTable.Rows ## 构造html表头 $HtmlTable = "

" ## 构造html表体 foreach ($row in $DataTable) { $HtmlTable += "" } ## 表尾 $HtmlTable += "
登录名 显示名 邮箱 后登陆日期
" + $row.LogonName + " " + $row.DisplayName + " " + $row.Email + " " + $row.LastLogonDate + "
" #Write-Output $HtmlTable Function SendEmail { $From = "shi.zhenning@hello.com" $To = "shi.zhenning@hellog.com" $Subject = "账号锁定通知" $Body = $HtmlTable $smtpServer = "mail.hello.com" $smtpPort = 587 $username = "username" $password = "password" $SMTPMessage = New-Object System.Net.Mail.MailMessage($From, $To, $Subject, $Body) $SMTPMessage.IsBodyHTML=$true $SMTPClient = New-Object Net.Mail.SmtpClient($smtpServer, $SmtpPort) $SMTPClient.EnableSsl = $true $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($username, $password); $SMTPClient.Send($SMTPMessage) } SendEmail

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:企业如何低成本解决软件许可证正版化难题
下一篇:获取本机的IP地址和mac地址(主机如何根据对方的IP地址获取MAC地址?)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~