Accessory ADUser.extensionAttribute from ps1 script

I am trying to modify the ps1 script that I found on the Internet. He must check how many days are left before the password expires and send an e-mail notification to the address stored in the attribute of the Active Directory account - extensionAttribute1. It is not possible to use your own email attribute because some accounts do not have emails (system accounts that cannot use the MSA), and usually I have to notify the user and send a copy to myself to keep that in mind. Reason: some users cannot be notified by the Windows system message when they log in, because they work on the domain network via VPN (win XP). There is a code:

Import-Module ActiveDirectory #System globalization $ci = New-Object System.Globalization.CultureInfo("en-US") #SMTP server name $smtpServer = "mail.domain.local" #Creating a Mail object $msg = new-object Net.Mail.MailMessage #Creating SMTP server object $smtp = new-object Net.Mail.SmtpClient($smtpServer) #E-mail structure Function EmailStructure($to,$expiryDate,$upn) { $msg.IsBodyHtml = $true $msg.From = " notification@domain.com " $msg.To.Add($to) $msg.Subject = "Password expiration notice" $msg.Body = "<html><body><font face='Arial'>This is an automatically generated message from Exchange service.<br><br><b>Please note that the password for your account $upn will expire on $expiryDate.</b><br><br>Please change your password immediately or at least before this date as you will be unable to access the service without contacting your administrator.</font></body></html>" } #Set the target OU that will be searched for user accounts $OU = "OU=Domain,DC=domain,DC=local" $ADAccounts = Get-ADUser -LDAPFilter "(objectClass=user)" -searchbase $OU -properties PasswordExpired, PasswordNeverExpires, PasswordLastSet, Mail, Enabled | Where-object {$_.Enabled -eq $true -and $_.PasswordNeverExpires -eq $false} Foreach ($ADAccount in $ADAccounts) { $accountFGPP = Get-ADUserResultantPasswordPolicy $ADAccount if ($accountFGPP -ne $null) { $maxPasswordAgeTimeSpan = $accountFGPP.MaxPasswordAge } else { $maxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge } #Fill in the user variables $samAccountName = $ADAccount.samAccountName <-- $userEmailAddress = $ADAccount.extensionAttribute1 --> $userPrincipalName = $ADAccount.UserPrincipalName if ($ADAccount.PasswordExpired) { Write-host "The password for account $samAccountName has expired!" } else { $ExpiryDate = $ADAccount.PasswordLastSet + $maxPasswordAgeTimeSpan Write-host "The password for account $samAccountName expires on: $ExpiryDate" $TodaysDate = Get-Date $DaysToExpire = $ExpiryDate - $TodaysDate #Write-Host $DaysToExpire.Days if ($DaysToExpire.Days -lt 7) { $expiryDate = $expiryDate.ToString("d",$ci) #Generate e-mail structure and send message if ($userEmailAddress) { EmailStructure $userEmailAddress $expiryDate $userPrincipalName $smtp.Send($msg) } Write-Host "NOTIFICATION - $samAccountName :: e-mail was sent to $userEmailAddress" } } } 

But the command line does not return "extensionAttribute1". I marked it with arrows. Can anyone help with this?

+4
source share
1 answer

You need to include extensionAttribute1 in the -Properties ($ ADAccounts) parameter.

 $ADAccounts = Get-ADUser ... -Properties extensionAttribute1,PasswordExpired... 
+2
source

Source: https://habr.com/ru/post/1441293/


All Articles