This repository has been archived by the owner on Nov 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ad-user.ps1
177 lines (163 loc) · 5.59 KB
/
ad-user.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<#
.SYNOPSIS
Gets security information of specified users from Active Directory (AD).
.DESCRIPTION
This script queries the infrastructure masters of available domains under
the default AD forest to fetch information of the specified 'SamAccountName'
or 'EmailAddress' properties. In addition, the 'ProxyAddresses' property is
also searched for email address aliases.
.EXAMPLE
PS C:\> .\ad-user.ps1 -InFile <ifile>
Read file and print output to console.
.EXAMPLE
PS C:\> .\ad-user.ps1 -InFile <ifile> -OutFile <ofile>
Read file and print output to file.
.EXAMPLE
PS C:\> Write-Host "<user1>`n<email-addr2>" | .\ad-user.ps1
Pipe data and print output to console.
.EXAMPLE
PS C:\> Get-Content <ifile> | .\ad-user.ps1
Pipe data data read from a file and print output to console.
.EXAMPLE
PS C:\> Get-Content <ifile> | .\ad-user.ps1 -OutFile <ofile>
Pipe data read from a file and print output to file.
.NOTES
Author: Diogo Fernandes
URL: https://github.com/diogo-fernan/
Requires: PowerShell v2.0
#>
Param (
[Parameter(
HelpMessage = "Input 'SamAccountName' and/or 'EmailAddress' file.",
Mandatory = $true,
Position = 1,
ValueFromPipeline = $true
)]
# [ValidateScript({ Test-Path $_ })]
[Alias('InFile')]
[string]$ifile,
[Parameter(
HelpMessage = "Output Comma Separated Value (CSV) file.",
Position = 2
)]
[Alias('OutFile')]
[string]$ofile
)
import-module activedirectory
if ($Input) { $data = [string]$Input }
else {
if (Test-Path $ifile) { $data = [string](Get-Content $ifile) }
else { throw [System.IO.FileNotFoundException] "$ifile not found" }
}
if ($data -ne $null) {
import-module activedirectory
$data = $data -split "[\n\s\t,;:]"
$dom = (Get-ADForest).Domains
if ($dom -ne $null) {
$dc = $dom | foreach {(Get-ADDomain $_).InfrastructureMaster}
$data = $data | ? { $_ } | sort -uniq | % { $_.Trim() }
$prop = @(
"SamAccountName",
"UserPrincipalName",
"CanonicalName",
"Created",
"Title",
"Office",
"Country",
"Department",
"Enabled",
"LastBadPasswordAttempt",
"LockedOut",
"LogonCount",
"MemberOf",
"PasswordExpired",
"PasswordLastSet",
"PasswordNeverExpires",
"PasswordNotRequired",
"ProxyAddresses",
"SID"
)
$propEx = @(
"MemberOf",
"ProxyAddresses"
)
if ($ofile) {
Remove-Item $ofile 2>$null
}
function ArrayToHash ($a) {
begin { $h = [ordered]@{} }
process { $a | foreach { $h[$_] = $null } }
end { return $h }
}
function Run-ADQuery {
Param (
[Parameter(Mandatory=$true)]
[string]$iserver,
[Parameter(Mandatory=$true)]
[string]$istr
)
Begin { $o = $null }
Process {
try {
$o = Get-ADUser `
-Server $iserver `
-Filter "SamAccountName -eq '$istr' -or EmailAddress -eq '$istr' -or UserPrincipalName -eq '$istr' -or ProxyAddresses -like '*$istr*'" `
-Properties $prop `
| Select $prop
} catch [Microsoft.ActiveDirectory.Management.ADException],
[Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException],
[System.TimeoutException] { }
foreach ($i in $o) {
foreach ($j in ($i.PSObject.Properties | Where-Object {$propEx -contains $_.Name})) {
$tmp = $i | Select -expand $j.Name
$i.PSObject.Properties.Remove($j.Name)
$i | Add-Member NoteProperty $j.Name ($tmp -join ",")
}
}
}
End { $o }
}
function Query-ADUser {
Param (
[Parameter(Mandatory=$true)]
[string]$istr
)
Begin { $o = $null }
Process {
foreach ($i in $dc) {
$o = Run-ADQuery $i $istr
if ($o -ne $null) {
break
}
}
if ($o -eq $null) {
$v = ArrayToHash $prop
$v['SamAccountName'] = $istr
$o = New-Object PSObject -Prop $v
}
}
End { return $o }
}
function Main {
Param (
[Parameter(Mandatory=$true)]
[string[]]$id,
[string]$of
)
$data = @()
foreach ($i in $id) {
$data += Query-ADUser $i
}
if ($of) {
$data | Export-CSV -Path $of -NoTypeInformation
} else {
$data | Format-List | Out-String
}
# Out-File -FilePath ofile -Encoding unicode -Append
# Format-Table -Wrap, Format-List
}
Main $data $ofile
} else {
Write-Host "no domains found under the default forest."
}
}