powershell如何组装AD域数据为表格并发送邮件
时间: 2024-06-13 12:08:46 浏览: 7
以下是使用PowerShell组装AD域数据为表格并发送邮件的步骤:
1.首先,使用Get-ADUser命令获取所需的AD域数据。例如,获取所有名字以“John”开头的用户的姓名和电子邮件地址:
```powershell
$users = Get-ADUser -Filter {Name -like "John*"} -Properties Name, EmailAddress
```
2.使用ConvertTo-Html命令将数据转换为HTML表格。例如:
```powershell
$table = $users | Select-Object Name, EmailAddress | ConvertTo-Html -Fragment
```
3.使用Send-MailMessage命令发送电子邮件。例如:
```powershell
$smtpServer = "smtp.contoso.com"
from = "admin@contoso.com"
$to = "john@contoso.com"
$subject = "AD User Report"
$body = "Please find the attached report."
$attachment = "C:\report.html"
Send-MailMessage -SmtpServer $smtpServer -From $from -To $to -Subject $subject -Body $body -Attachments $attachment
```
完整的PowerShell脚本如下:
```powershell
$users = Get-ADUser -Filter {Name -like "John*"} -Properties Name, EmailAddress
$table = $users | Select-Object Name, EmailAddress | ConvertTo-Html -Fragment
$smtpServer = "smtp.contoso.com"
$from = "admin@contoso.com"
$to = "john@contoso.com"
$subject = "AD User Report"
$body = "Please find the attached report."
$attachment = "C:\report.html"
Send-MailMessage -SmtpServer $smtpServer -From $from -To $to -Subject $subject -Body $body -Attachments $attachment
```
相关推荐
![ps1](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)