Hi. I would like to get help (if possible) with either a AD or powershell question. If i need to get a list from a specific OU and users with experation date of their accounts, and from there automate it every mont and have that list sent to me. Is this possible? Preffered in beforehand this account expire at this date. How can i do this
-
Account Experation date
-
This will retrieve users from a specific OU and their account expiration date.
Get-ADUser -Filter * -SearchBase "OU=Production,OU=Employees,DC=ITPROTVDEMO,DC=COM" -Properties AccountExpirationDate | ft Name,AccountExpirationDate
I would create a script and use a scheduled task to run it every month.
In the script you would output the results to a file, and then use
Send-MailMessage
to send an email, and attach the output file.From Microsoft:
You must specify a Simple Mail Transfer Protocol (SMTP) server or the Send-MailMessage command fails. Use the SmtpServer parameter or set the $PSEmailServer variable to a valid SMTP server. The value assigned to $PSEmailServer is the default SMTP setting for PowerShell
Something like this
Send-MailMessage -From 'User01 <user01@fabrikam.com>' -To 'User02 <user02@fabrikam.com>', 'User03 <user03@fabrikam.com>' -Subject 'Sending the Attachment' -Body "Forgot to send the attachment. Sending now." -Attachments .\data.csv -Priority High -DeliveryNotificationOption OnSuccess, OnFailure -SmtpServer 'smtp.fabrikam.com'
I wasn't clear on the last part of your question, what else do you want to do?
Mike Rodrick
Edutainer, ITProTV**if the post above has answered the question, please mark the topic as solved.
-
$CurrentDate = Get-Date $users = Get-ADUser -Filter * -SearchBase "OU=Production,OU=Employees,DC=ITPROTVDEMO,DC=COM" -Properties AccountExpirationDate foreach ($user in $users){ $DaysToExpire = ($user.AccountExpirationDate - $CurrentDate).Days if (($user.AccountExpirationDate -ne $null) -and ($DaysToExpire -lt 7)) { # $user.SamAccountName + "'s account expires on " + ($user.AccountExpirationDate).ToShortDateString() $user.SamAccountName + "'s account expires in " + $DaysToExpire + " days" } }
This should find users in an OU whos accounts expire in less than 7 days. I was playing with a couple of different output options, whether to show expiration date or days till expiration.
Next would be to output to a file, and then send as attachment.
Mike Rodrick
Edutainer, ITProTV**if the post above has answered the question, please mark the topic as solved.
-
$CurrentDate = Get-Date $users = Get-ADUser -Filter * -SearchBase "OU=Production,OU=Employees,DC=ITPROTVDEMO,DC=COM" -Properties AccountExpirationDate $path = (New-Item -Type File -Path "C:\expiringAccts.txt" -force).FullName foreach ($user in $users){ $DaysToExpire = ($user.AccountExpirationDate - $CurrentDate).Days if (($user.AccountExpirationDate -ne $null) -and ($DaysToExpire -lt 7)) { # $user.SamAccountName + "'s account expires on " + ($user.AccountExpirationDate).ToShortDateString() ($user.SamAccountName + "'s account expires in " + $DaysToExpire + " days") >> $path } }
Added a line to create a new file, and append foreach results to the file. This would assume you are ok overwriting the file each time you run the script. I figured that would be ok since you want to run it once a week, and last weeks results don't matter.
Now just need to email the file as an attachment.
Mike Rodrick
Edutainer, ITProTV**if the post above has answered the question, please mark the topic as solved.
-
Awsome, i´ll have a got at it today to see how it works for me.
Thanks alot, much apreciated