Last active
October 10, 2017 04:44
-
-
Save micmaher/a87e9e52bae1340ba3b4bca391830010 to your computer and use it in GitHub Desktop.
Get AD Users Created since (x) Days - include Office 365 attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Get-FreshADUser | |
<# | |
.Synopsis | |
Check for users created in AD since (x) days | |
.DESCRIPTION | |
Lists users created in AD since specified date | |
The number of days to check back is a mandatory prarmeter | |
Useful in a domain migration scenario | |
.NOTES | |
No special privileges needed other than Domain Users membership | |
Set the variable for PSEmailServer if not already set | |
.Author | |
Michael Maher on 2/2/16 | |
.EXAMPLE | |
Get-FreshADUser -days 10 -domain CONTOSO | |
samAccountName : SMITH_J | |
givenName : John | |
surName : Smith | |
displayName : Smith John (IE) | |
canonicalName : contoso.com/Dublin/Smith John | |
userPrincipalName : SmithJ@contoso.com | |
whencreated : 29/01/2016 16:42:51 | |
mail : john.smith@contoso.com | |
ProxyAddress_1 : | |
ProxyAddress_2 : | |
ProxyAddress_3 : | |
.EXAMPLE | |
Get-FreshADUser -days 10 -domain OLDDOMAIN -mailto michael.maher@contoso.com | |
#> | |
{ | |
[CmdletBinding()] | |
Param | |
( | |
# How many days back do you want to check? | |
[Parameter(mandatory=$true, | |
HelpMessage="Specify how many days you want to go back", Position=0)] | |
[ValidateRange(1,100)] | |
[Int]$days, | |
[ValidateSet('CONTOSO', 'OLDDOMAIN')] | |
[String]$domain='OLDDOMAIN', | |
[String]$mailto='michael.maher@contoso.com' | |
) | |
Begin | |
{ | |
Import-Module ActiveDirectory | |
$recpt = "<$mailto>" | |
$sender = "<noreply@contoso.com>" | |
$PSEMailServer = "mailrelay.contoso.com" | |
$subject = "User Creation Report" | |
$cutOffDate = ((Get-Date).AddDays(-$days)).Date | |
Write-Verbose "Checking for user accounts created since $cutOffDate" | |
If ($domain -eq 'CONTOSO'){ | |
Write-Verbose 'CONTOSO domain requested' | |
$searchLocation = "ou=Internal Users, dc=contoso, dc=com"} | |
Else{ | |
Write-Verbose 'OLDDOMAIN domain requested' | |
$searchLocation = "ou=Internal Users, dc=contoso, dc=ie"} | |
#$enabledAccount = "(!userAccountControl:1.2.840.113556.1.4.803:=2)" | |
} | |
Process | |
{ | |
Write-Verbose "Searching $searchLocation" | |
get-aduser -Filter {whenCreated -ge $cutOffDate} -SearchBase $searchLocation ` | |
-Properties samAccountName, givenName, surName, displayName, canonicalName, userPrincipalName, mail, whenCreated, proxyaddresses | | |
select samAccountName, givenName, surName, displayName, canonicalName, userPrincipalName, whencreated, ` | |
mail, @{L='ProxyAddress_1'; E={$_.proxyaddresses[0]}}, @{L='ProxyAddress_2';E={$_.ProxyAddresses[1]}}, @{L='ProxyAddress_3'; E={$_.proxyaddresses[2]}} | | |
Export-Csv -Path c:\Scripts\Users.csv -NoTypeInformation | |
} | |
End | |
{ | |
$body = "Users created on $domain since $cutOffDate" | |
Write-Verbose "Sending email with $body" | |
Send-MailMessage -To $recpt -From $sender` | |
-SmtpServer $PSEMailServer -Subject $subject -Body $body | |
Write-Verbose "Script Complete" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment