回答如何使用powershell从AD中删除某些计算机并且邮件通知执行结果


搞定…
remove-item “c:\scripts\summary report*”
Import-Module ActiveDirectory
$File = “c:\Scripts\mycomputers.txt”
$logfile=”c:\scripts\summary report_”+(get-date -format ddMMyyyy)+”.txt”

$sum_success=0
$sum_fail=0
ForEach ($Computer in (Get-Content $File))
{
Try {
Remove-ADComputer $Computer -ErrorAction Stop
Add-Content $logfile -Value “$Computer removed”
$sum_success+=1
}
Catch {
Add-Content $logfile -Value “$Computer not found because $($Error[0])”
$sum_fail+=1
}
}

$messageParameters = @{
Subject = “Remove Computer Report – $((Get-Date).ToShortDateString())”
Body = “Finally $sum_success computers removed and $sum_fail failed, please check attachment for detail”
attachment= $logfile
From = “aaaa@test.com”
To = “aaaa@test.com”
SmtpServer = “smtp.test.com”
}
Send-MailMessage @messageParameters -BodyAsHtml

×用微信扫描并分享