我猜测用于您在代码中提供的标签的属性,但您可以在此基础上进行构建。
它使用Here-Strings、ConvertTo-Html和Splatting
# create a nice CSS style to format the table using a Here-String
$style = @'
<style>
body {font-family: Calibri, Tahoma, Helvetica, sans-serif; color: black;}
table {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
td {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
</style>
'@
# the text to start the email with. This has one placeholder {0} that will be filled in with the user's first name in the loop
$preText = @'
Hi {0},<br /><br />
On the Jan 20th a new email sign software will be installed. This will standardize all email signs across all devices.<br />
Please check the below to ensure the details are correct. If there is missing data please reply to this email to have this addressed:
'@
# the static text to end the email with
$postText = '<br />Please note that if some of the fields provided are empty do not panic the fields will be filled in.'
# Get-ADUser already returns objects with these properties:
# DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
# anything else you need to ask through the Properties parameter
$props = 'EmployeeID', 'pager', 'Company', 'MobilePhone', 'OfficePhone', 'HomePhone', 'HomePage', 'Title', 'EmailAddress'
$users = Get-ADUser -Filter * -SearchBase 'OU=_Users,OU=Head Office,OU=1_A,OU=1_Oceania,OU=L,DC=l,DC=global' -SearchScope OneLevel -Properties $props |
Where-Object {[string]::IsNullOrWhiteSpace($_.EmployeeId) -and ($null -ne $_.pager)}
foreach ($user in $users) {
# fill in the '{0}' placeholder for the leading text with the users first name
$leaderText = $preText -f $user.GivenName
# create a sub selection of the properties needed in the email
$properties = $user | Select-Object Name, EmployeeId,
@{Name = 'Job Title'; Expression = {$_.Title}},
Company,
@{Name = 'Mobile'; Expression = {$_.MobilePhone}},
@{Name = 'Direct Land Line'; Expression = {$_.HomePhone}},
@{Name = 'Main Number'; Expression = {$_.OfficePhone}},
@{Name = 'URL'; Expression = {$_.HomePage}}
# or if you prefer this syntax:
# $properties = [PsCustomObject]@{
# 'Name' = $user.Name
# 'EmployeeId' = $user.EmployeeId
# 'Job Title' = $user.Title
# 'Company' = $user.Company
# 'Mobile' = $user.MobilePhone
# 'Direct Land Line' = $user.HomePhone
# 'Main Number' = $user.OfficePhone
# 'URL' = $user.HomePage
# }
# create the email body using the style above for the table
$body = $properties | ConvertTo-Html -Head $style -PreContent $leaderText -PostContent $postText -As List
# use Splatting on cmdlets that take a lot of parameters:
$mailParams = @{
From = 'you@yourcompany.com'
To = $user.EmailAddress
Subject = 'New email sign-in software'
Body = $body -join "`r`n"
BodyAsHtml = $true
Priority = 'High'
SmtpServer = 'smtpmail.yourcompany.com'
Port = 587
# more parameters go here
}
# send the email
Send-MailMessage @mailParams
}
当然,请先在虚拟用户身上尝试一下,并确保将电子邮件发送给您自己以测试结果。