-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetUsersByUserID.ps1
executable file
·66 lines (58 loc) · 2.1 KB
/
GetUsersByUserID.ps1
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
66
function Get-ByUserID {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]
$FileName
)
if(Test-Path $FileName)
{
$users = Import-Csv $FileName
}
else
{
Write-Error "The file $FileName does not exist. Please type the path again"
return
}
foreach($u in $users)
{
try
{
$result = Get-AdUser -Filter "(samAccountName -eq '$($u.samAccountName)')" `
-Properties samAccountName,GivenName,Surname,title,mail,department `
| select samAccountName,GivenName,Surname,title,mail,department -ErrorAction Stop
if($result -eq $null)
{
# This will create a dummy object with some data that can
# then be used to filter out accounts in an excel worksheet
$UserInfo = [ordered]@{
'Username' = "not found";
'First Name' = $($u.Firstname);
'Last Name' = $($u.Lastname);
'Title' = "not found";
'Email' = "not found"
'Department' = "not found";
}
Write-Warning "No account information found for $($u.Firstname) $($u.Lastname)"
}
else
{
$UserInfo = [ordered]@{
'Username' = $result.samAccountname;
'First Name' = $result.GivenName;
'Last Name' = $result.Surname;
'Title' = $result.title;
'Email' = $result.mail;
'Department' = $result.department;
}
Write-Verbose "Account information found for $($u.Firstname) $($u.Lastname)."
}
$obj = New-Object -TypeName psobject -Property $UserInfo
Write-Output $obj
}
catch
{
Write-Verbose "Unable to find user $($u.FirstName) $($u.LastName)"
}
}
}