脚本内容如下
import-module ActiveDirectory
$csvPath = "NewEmployee.csv"
$ReadmePath = "WelcomeNewEmployees.zip"
$today=Get-Date
$yestoday=$today.AddDays(-1)
function send_email
{
#mail server configuration
$smtpServer = "smtpserver"
#$smtpUser = "username"
#$smtpPassword = "password"
#create the mail message
$mail = New-Object System.Net.Mail.MailMessage
#set the addresses
$MailAddress="mailaddress"
$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress)
#set the content
$mail.Subject = "给新员工的一封信"
$mail.Priority = "High"
$mail.Body = $MailBody
$filename = $ReadmePath
$attachment = new-Object System.Net.Mail.Attachment($filename)
$mail.Attachments.Add($attachment)
#send the message
$smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer
$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPassword
$mail.isBodyhtml = $true
$Contents = import-csv $csvPath
foreach ($Content in $Contents)
{
$MailtoAddress = $Content.Mail
$MailtoAddress
if ($MailtoAddress -ne "")
{
#Get-Member -InputObject $mail.To
$mail.To.Add($MailtoAddress)
#echo $mail
echo $MailtoAddress" mail is send..."
$ctime=get-date
$mail.To | select Address | foreach {$csend=-join($csend,$_.Address,";")}
$log=-join ($ctime," ",$csend)
echo $log >> .\log.txt
$csend=""
$smtp.Send($mail)
$mail.To.Clear()
}
}
$mail.Attachments.remove($attachment)
}
Get-Mailbox -filter {Created -gt $yestoday} -Properties sAMAccountName,displayName,mail,employeeNumber,mobile,Created | select sAMAccountName,displayName,mail,employeeNumber,mobile,Created | export-csv -Encoding "Unicode" -NoTypeInformation $csvPath
$NewEmployeeNumber = import-csv $csvPath | measure-object
$MailBody = "mailbody"
send_email