![](/img/trans.png)
[英]How to pull AD user email then pass it to Function to send email using that ADuser email
[英]How would I send an email to myself if the user's manager field is empty in AD?
您好,我是 PowerShell 的新手,我正在嘗試弄清楚如何向用戶經理發送電子郵件。 在下面的腳本中,如果在 AD 中,管理器字段不為空,我可以發送通知,但是當它為黑色時,我會收到一條錯誤消息。 如果用戶的經理是空白的,我如何讓它向我自己發送 email? 出於某種原因,當我運行此代碼時,我不斷收到第 233 行經理 email 是 null。
代碼位於: https://stackoverflow.com/a/69731370/18947133
Import-Module ActiveDirectory
$mailprops = @{
from = "test@domainname.com
smtpserver = "server.domain.com"
bodayashtml =$true
}
$startDate = Get-Date
$enddate= $startdate.addDays(x)
$props = @{ filer = "Account expirationDate -gt '$startdate' - and AccountExpiratioDate -lt '$endDate'"
Properties ='Accountexpirationdate', '經理' }
$Users = Get-ADuser @props
$bodyScriptBlock = {
# Group all users by their Manager
$用戶 | 組對象管理器 | ForEach對象{
# Get this Manager's email address
$managerEmail = (Get-ADUser $_.Name -Properties EmailAddress).EmailAddress
# Create a string[] with the user's Name and their Account's Expiration Date
$userList = foreach($user in $_.Group)
{
'- {0} expires on {1}' -f $user.Name, $user.AccountExpirationDate
}
# Execute the Body scriptblock passing this user's list
$body = & $bodyScriptBlock -UserList $userList
# Set the remaing values for Mail props
$mailProps.To = $managerEmail
$mailProps.Subject = "Account Expiration Notification for Managees" # ??? No idea what to put here
$mailProps.Body = $body
# Send the email with the list of users
Send-MailMessage @mailProps
你有沒有嘗試過
if ($null -eq $ManagerEmail)
{$ManagerEmail = "yourEmail@domain.com"}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.