Public/Get-Accounts.ps1
Function Get-Accounts { <# .SYNOPSIS Gets a list of all associated accounts to the user found with the given search term .DESCRIPTION Provide a search term, and this function will search for a user account, then using the EmployeeID from that account it will find all related accounts (T0, T1, User) .PARAMETER SearchTerm Specifies the file name. .SYNTAX .INPUTS System.Object Properties SamAccountName (Active Directory Login Name) DistinguishedName (Active Directory Object Distinguished Name) EmployeeID (Team Member ID) mail (Email Address) System.String SearchTerm SamAccountName (Active Directory Login Name) DistinguishedName (Active Directory Object Distinguished Name) EmployeeID (Team Member ID) mail (Email Address) .OUTPUTS System.Object The found account [array]System.Object The list of found accounts .EXAMPLE PS> Get-UsersAccounts -SearchTerm "" .EXAMPLE PS> Get-UsersAccounts TMID .EXAMPLE PS> Get-UsersAccounts -SearchTerm "" .LINK Online version: .LINK Set-Item #> [cmdletBinding()] Param( [Parameter( ValueFromPipelineByPropertyName )] $EmployeeID, [Parameter( ValueFromPipelineByPropertyName )] [Alias("SamAccountName", "DistinguishedName", "GUID", "SID", "mail", "UserPrincipalName")] $Identity = $ENV:USERNAME, [string[]]$Properties = @("Name", "EmployeeID", "physicalDeliveryOfficeName", "Title", "mail", "msRTCSIP-PrimaryUserAddress", "CanonicalName", "DistinguishedName", "samaccountname", "UserPrincipalname", "AccountExpirationDate", "Enabled", "Manager", "badPwdCount", "LastBadPasswordAttempt", "LockedOut", "LockOutTime", "lastLogonDate", "PasswordExpired", "PasswordLastSet", "whenCreated", "whenChanged"), [string[]]$Select = @("Name", "EmployeeID", "physicalDeliveryOfficeName", "Title", "mail", "msRTCSIP-PrimaryUserAddress", "CanonicalName", "DistinguishedName", "samaccountname", "UserPrincipalname", "AccountExpirationDate", "Enabled", "Manager", "badPwdCount", "LastBadPasswordAttempt", "LockedOut", "LockOutTime", "lastLogonDate", "PasswordExpired", "PasswordLastSet", "whenCreated", "whenChanged") ) BEGIN { } PROCESS { If ($EmployeeID -and $null -ne $EmployeeID) { Write-Verbose "Searching $EmployeeID" Get-ADUser -Filter "EmployeeId -like '$EmployeeID*'" -Properties $Properties | Select-Object $Select } elseif ($Identity) { foreach ($SearchTerm in $Identity) { Write-Verbose "Getting all accounts that match $SearchTerm" $Accounts = Get-ADUser -Filter "SamAccountName -eq '$SearchTerm' -or DistinguishedName -eq '$SearchTerm' -or GUID -eq '$SearchTerm' -or SID -eq '$SearchTerm' -or EmployeeID -eq '$SearchTerm' -or mail -eq '$SearchTerm' -or UserPrincipalName -eq '$SearchTerm'" -Properties $Properties | Select-Object $Select Foreach ($Account in $Accounts) { $EmployeeID = $Account.EmployeeID if ($null -ne $EmployeeID) { Get-ADUser -Filter "EmployeeId -like '$EmployeeID*'" -Properties $Properties | Select-Object $Select } else { Write-Verbose "No EmployeeID on Account" } } } } } END { } } |