[英]How would I send an email to myself if the user's manager field is empty in AD?
Hello I am new to PowerShell and I am trying to figure out how to send emails to user's manager.您好,我是 PowerShell 的新手,我正在尝试弄清楚如何向用户经理发送电子邮件。 In the script below I am able to send the notification if in AD,manager field is not empty, but when is black I get an error message.
在下面的脚本中,如果在 AD 中,管理器字段不为空,我可以发送通知,但是当它为黑色时,我会收到一条错误消息。 How do I make it where it sends an email to myself, if the user's manager is blank?
如果用户的经理是空白的,我如何让它向我自己发送 email? For some reason when I run this code, I keep getting line 233 manager email is null.
出于某种原因,当我运行此代码时,我不断收到第 233 行经理 email 是 null。
code is located here: https://stackoverflow.com/a/69731370/18947133代码位于: 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', 'Manager' } Properties ='Accountexpirationdate', '经理' }
$Users = Get-ADuser @props
$bodyScriptBlock = {
# Group all users by their Manager
$Users | $用户 | Group-Object Manager |
组对象管理器 | ForEach-Object {
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
Have you tried你有没有尝试过
if ($null -eq $ManagerEmail)
{$ManagerEmail = "yourEmail@domain.com"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.