-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind-AdUser.ps1
More file actions
65 lines (65 loc) · 2.52 KB
/
Find-AdUser.ps1
File metadata and controls
65 lines (65 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Returns UserPrincipalName, First Name and Display Name as $Upn, $FName and $DName to be used elsewhere in a script
Function Find-AdUser ($Uinput) {
If (-not ($UInput) ){
# Enter part of name
$Uinput = Read-Host -prompt "Enter part of a users name to find"
}
# Display results
[array]$users = Get-ADUser -Filter {anr -like $Uinput} -properties emailAddress
# If no matches found
If ($users.count -lt 1) {
Write-Host `t"Sorry, no matching users found" -fore red
$choice = Read-Host -prompt "Try again? [Yes] or No"
while ("y", "yes", "n", "no", "" -notcontains $choice) {
$choice = Read-Host "Please enter yes or no"
}
if ("y", "yes", "" -contains $choice) {
. Find-AdUser
}
ElseIf ("n", "no" -contains $choice) {return}
}
Else {
# If multiple matches found
If ($users.count -gt 1) {
# Loop through them and add to an object
$results = Foreach ($u in $users) {
[pscustomobject]@{
Number = ([array]::indexof($users, $u))+1
Name = $u.name
Email = $u.emailAddress
}
}
# Output the results using a custom format
$originalColor = $Host.UI.RawUI.ForegroundColor
Write-Host `n"Found the following possible matches:" -fore green
$format = @{Label="Number"; Expression={If($_.number % 2){[console]::ForegroundColor="white";$_.number}Else{[console]::ForegroundColor="cyan";$_.number}}; width=15; Alignment="center"},
@{Label="Name"; Expression={$_.name}; width=30},
@{Label="Email Address"; Expression={$_.email}; width=50}
$results | ft $format
$Host.UI.RawUI.ForegroundColor = $originalColor
# Prompt for a choice from the above results
$Uchoice = Read-Host -prompt `n"Enter a number from above"
If (1..$users.count -notcontains $Uchoice) {
Write-Host "Nice try, now enter a number from above"
Do {
$Uchoice = Read-Host -prompt "Number"
}
Until (1..$users.count -contains $Uchoice)
}
Write-Host `n`t"$($users[[int]$Uchoice -1].name) ($($users[[int]$Uchoice -1].userPrincipalName)) " -nonewline
Write-Host "selected"`n -fore green
$Upn = $($users[[int]$Uchoice -1].userPrincipalName)
$FName = $($users[[int]$Uchoice -1].givenName)
$DName = $($users[[int]$Uchoice -1].name)
}
# If only one match was found
Else {
$Uchoice = 0
Write-Host `n`t"$($users[[int]$Uchoice].name) ($($users[[int]$Uchoice].userPrincipalName))" -nonewline
Write-Host "selected"`n -fore green
$Upn = $($users[[int]$Uchoice].userPrincipalName)
$FName = $($users[[int]$Uchoice -1].givenName)
$DName = $($users[[int]$Uchoice -1].name)
}
}
}