Skip to content

Instantly share code, notes, and snippets.

@maravedi
Last active May 26, 2016 17:37
Show Gist options
  • Save maravedi/5022597dc32b3403174ba7327b9ae5fc to your computer and use it in GitHub Desktop.
Save maravedi/5022597dc32b3403174ba7327b9ae5fc to your computer and use it in GitHub Desktop.
#######################################################################
# File: GetEmployeeDetails.ps1
# Auth: David Frazer
# Date: 05/23/2016
# Purpose: Search AD for details based on DisplayName
#######################################################################
function PrintOutput {
# Return the employee details for a successfull query
Write-Host `n;
Write-Host 'Name: ' -nonewline;
Write-Host $value.DisplayName -f white;
Write-Host 'Username: ' -nonewline;
Write-Host $value.SamAccountName -f white;
Write-Host 'Email: ' -nonewline;
Write-Host $value.UserPrincipalName -f white;
Write-Host 'Extension: ' -nonewline;
Write-Host $value.OfficePhone -f white;
Write-Host 'Position: ' -nonewline;
Write-Host $value.Description -f white;
Write-Host 'Department: ' -nonewline;
Write-Host $value.Department -f white;
$location = ${value}.physicalDeliveryOfficeName + ${value}.State;
Write-Host 'Employee Location: ' -nonewline;
Write-Host $location -f white;
$manager = [Regex]::Match($value.Manager, "(?<=\=)([a-zA-Z`'\-\d{1} ]+)(?=,)");
Write-Host 'Manager: ' -nonewline;
Write-Host $manager -f white;
Write-Host 'Employee ID: ' -nonewline;
Write-Host $value.EmployeeID -f white;
Write-Host 'SID: ' -nonewline;
Write-Host $value.SID -f white;
If ($value.LockedOut -eq 0) {
$AccountLocked = "No";
Write-Host 'Account Locked: ' -nonewline;
Write-Host $AccountLocked -f white;
} else {
$AccountLocked = "Yes";
Write-Host 'Account Locked: ' -nonewline;
Write-Host $AccountLocked -backgroundcolor red -f white;
}
If ($value.Enabled -eq 1) {
$AccountDisabled = "No";
Write-Host 'Account Disabled: ' -nonewline;
Write-Host $AccountDisabled -f white;
} else {
$AccountDisabled = "Yes";
Write-Host 'Account Disabled: ' -nonewline;
Write-Host $AccountDisabled -backgroundcolor red -f white;
}
If ($value.PasswordExpired -eq 0) {
$PasswordExpired = "No";
Write-Host 'Password Expired: ' -nonewline;
Write-Host $PasswordExpired -f white;
} else {
$PasswordExpired = "Yes";
Write-Host 'Password Expired: ' -nonewline;
Write-Host $PasswordExpired -backgroundcolor red -f white;
}
Write-Host 'Password Last Changed: ' -nonewline;
Write-Host $value.PasswordLastSet -f white;
$PwdExpDate = get-aduser -filter {SID -eq $value.SID} -Properties "msDS-UserPasswordExpiryTimeComputed" | Select-Object -Property @{Name="PasswordExpirationDate";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}
Write-Host 'Password Expiration Date: ' -nonewline;
Write-Host $PwdExpDate.PasswordExpirationDate -f white;
Write-Host `n;
}
function SearchUser {
# Adding a blank line above the prompt for input from user
Write-Host `n;
# Prompt user for the employee to search for
$input = Read-Host -Prompt "Please enter an employee's name. `n Examples:`tJohn Doe`n`t`tJohn`n`t`tDoe`nName"
if ($input -eq "exit" -Or $input -eq "quit"){
break;
}
elseif ($input -NotMatch "[ ]") {
$fullname = $False;
}
elseif ($input -match "[ ]") {
$fullname = $True;
}
# If input includes characters, an apostrophe or a single digit at the end, then continue
elseif ($input -match "[^a-zA-Z`'\-\d{1} ]") {
$fullname = $True;
{continue} | Out-Null;
}
else {
Write-Host "Invalid input. Please enter an employee's full name.";
$quit = Read-Host -Prompt "Press Enter to try again";
if ($quit -eq "exit" -Or $quit -eq "quit"){
break;
}
SearchUser;
}
if ($fullname -eq $True) {
# Try to query AD for the full name provided by the user
# If the query failes, return null
$user = $(try {Get-AdUser -Properties * -Filter {DisplayName -eq $input}} catch {$null})
# Check if the query was successfull
if ($user -ne $null){
Write-Host `n;
Write-Host @($user).length "result(s) returned.";
foreach ($value in $user){
PrintOutput;
}
} else {
# Return an error message for an unsucessful query
Write-Host `n;
Write-Host "Exmployee not found in AD." `n;
}
} elseif ($fullname -eq $False) {
# Try to query AD for the name provided by the user
# If the query failes, return null
$firstname = $(try {Get-AdUser -Properties * -Filter {GivenName -eq $input}} catch {$null})
$lastname = $(try {Get-AdUser -Properties * -Filter {sn -eq $input}} catch {$null})
# Check if the query was successfull
if ($firstname -ne $null){
Write-Host `n;
Write-Host @($firstname).length "result(s) returned.";
foreach ($value in $firstname){
PrintOutput;
}
} elseif ($lastname -ne $null){
Write-Host `n;
Write-Host @($lastname).length "result(s) returned.";
foreach ($value in $lastname){
PrintOutput;
}
} else {
# Return an error message for an unsucessful query
Write-Host `n;
Write-Host "Exmployee not found in AD." `n;
}
}
# The below menu comes from https://technet.microsoft.com/en-us/library/ff730939.aspx
# This will allow the user to re-run the search if needed or quit
$title = ""
$message = "Do you want to quit?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Exits the search script."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Remains in search script."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
switch ($result)
{
0 {exit}
1 {SearchUser}
}
}
# Call the primary function
SearchUser;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment