Last active
September 11, 2018 22:29
-
-
Save Chirishman/1f419de8f0592395e19bc9ea50dfa03c to your computer and use it in GitHub Desktop.
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-UserMailboxMigrationState { | |
Param( | |
[string]$SamAccountName, | |
[int]$CreatedWindow | |
) | |
Add-Type -AssemblyName System.DirectoryServices.AccountManagement | |
$DirectoryEntry = [System.DirectoryServices.DirectoryEntry]::New() | |
$DirectorySearcher = [System.DirectoryServices.DirectorySearcher]::New($DirectoryEntry) | |
$UnwrapProperties = @( | |
@{ | |
N = 'name' | |
E = {[string]$_.name} | |
}, | |
@{ | |
N = 'samaccountname' | |
E = {[string]$_.samaccountname} | |
}, | |
@{ | |
N = 'whencreated' | |
E = {[datetime]::Parse($_.whencreated)} | |
}, | |
@{ | |
N = 'mailnickname' | |
E = {[string]$_.mailNickname} | |
}, | |
@{ | |
N = 'msexchhomeservername' | |
E = {[string]$_.msexchhomeservername} | |
}, | |
@{ | |
N = 'homemdb' | |
E = {[string]$_.homemdb} | |
}, | |
@{ | |
N='legacyexchangedn' | |
E={[string]$_.legacyexchangedn} | |
} | |
) | |
$IdentifyMailboxState = @( | |
'name', | |
'samaccountname', | |
'whencreated', | |
@{ | |
N = 'hasmailname' | |
E = {[bool]$_.mailNickname} | |
} | |
@{ | |
N = 'haslocalmailbox' | |
E = {[bool]$_.homemdb} | |
}, | |
@{ | |
N = 'hascloudmailbox' | |
E = {$_.legacyexchangedn -match 'ou=External'} | |
} | |
) | |
$SimplifyState = @( | |
'name', | |
'samaccountname', | |
'whencreated', | |
@{ | |
N='MailboxState' | |
E = {-join$(('OnPrem'*$_.haslocalmailbox);('o365'*$_.hascloudmailbox))} | |
} | |
) | |
$UnwrapProperties.n | %{ | |
[void]$DirectorySearcher.PropertiesToLoad.Add($_) | |
} | |
$DirectorySearcher.PageSize = 2000 | |
$QueryWrapper = @('(&',')') | |
$ClassUser = "(objectClass=user)(objectCategory=Person)" | |
$AccountNameMatch = "(samaccountname=$SamAccountName)" | |
$HasMailAlias = "(mailNickname=*)" | |
$HasHomeDB = "(homemdb=*)" | |
$WhenCreated = "(whenCreated>=1$([datetime]::Now.AddDays(-$CreatedWindow).ToString("yyyyMMddHHmmss.sZ")))" | |
$IsEnabled = '(!(userAccountControl:1.2.840.113556.1.4.803:=2))' | |
$DirectorySearcher.Filter = $( | |
$QueryWrapper[0] | |
$ClassUser | |
$IsEnabled | |
if ($SamAccountName) { | |
$AccountNameMatch | |
} | |
if ($CreatedWindow){ | |
$WhenCreated | |
} | |
$QueryWrapper[1] | |
) -join $null | |
$SearchResults = $DirectorySearcher.FindAll() | ForEach-Object {New-Object PSObject -Property $_.Properties} | select $UnwrapProperties | select $IdentifyMailboxState | select $SimplifyState | |
$SearchResults | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment