-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathGet-Account.ps1
More file actions
325 lines (278 loc) · 9.69 KB
/
Get-Account.ps1
File metadata and controls
325 lines (278 loc) · 9.69 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<#
.SYNOPSIS
Report the account information for the given username and specified domain.
.DESCRIPTION
Can report on either a local user account or an ActiveDirectory account.
.PARAMETER All
Report all local users.
.PARAMETER Username
The account username to report. Default is $env:USERNAME.
.PARAMETER Domain
The ActiveDirectory domain to use. Default is $env:USERDOMAIN.
.PARAMETER SID
If specified then return only the Sid
#>
param(
[string] $Username = $env:USERNAME,
[string] $Domain = $env:USERDOMAIN,
[switch] $SID,
[switch] $All
)
Begin
{
$script:LabelWidth = 15
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# Helpers...
function ConvertFileTime
{
param($value)
try {
return [datetime]::FromFileTimeUtc($value)
}
catch {
return '<error in ConvertFileTime>'
}
}
function ConvertADSLargeInteger
{
param([object] $adsLargeInteger)
try {
$highPart = $adsLargeInteger.GetType().InvokeMember("HighPart", [System.Reflection.BindingFlags]::GetProperty, $null, $adsLargeInteger, $null)
$lowPart = $adsLargeInteger.GetType().InvokeMember("LowPart", [System.Reflection.BindingFlags]::GetProperty, $null, $adsLargeInteger, $null)
$bytes = [System.BitConverter]::GetBytes($highPart)
$tmp = [System.Byte[]]@(0,0,0,0,0,0,0,0)
[System.Array]::Copy($bytes, 0, $tmp, 4, 4)
$highPart = [System.BitConverter]::ToInt64($tmp, 0)
$bytes = [System.BitConverter]::GetBytes($lowPart)
$lowPart = [System.BitConverter]::ToUInt32($bytes, 0)
return $lowPart + $highPart
}
catch {
return '<error in ConvertADSLargeInteger>'
}
}
function FormatEnabledString
{
param($user, $value)
if ($user.Enabled) {
"$($PSStyle.BrightWhite)$value$($PSStyle.Reset)"
} else {
"$($PSStyle.BrightBlack)$value$($PSStyle.Reset)"
}
}
function WriteHanging
{
param(
[Parameter(Mandatory)] [string]$Label,
[Parameter(Mandatory)] [string[]]$Items,
[ConsoleColor]$Forecolor = 'DarkGray'
)
$Label = ("{0,-$LabelWidth}: " -f $Label)
$Label | Write-Host -NoNewline
$indent = $Label.Length
$width = $host.UI.RawUI.WindowSize.Width - $indent
$margin = ' ' * $indent
$line = '' # first line is already prefixed with Label
foreach ($item in $items)
{
$item = $item.Trim()
if ($item) {
if ($line.Length + $item.Length + 2 -gt $width) {
Write-Host $line -ForegroundColor $Forecolor
$line = $margin # subsequent lines get a margin prefix
} else {
$line = $line.Trim().Length -eq 0 ? "$line$item" : "$line, $item"
}
}
}
if ($line.Length -gt $margin.Length) {
Write-Host $line -ForegroundColor $Forecolor
}
}
function GetLocalSid
{
$account = Get-LocalUser -Name $Username
if (!$account) { Throw 'Account not found' }
$account.SID
}
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# Reporters...
function ReportLocalUser
{
$account = Get-LocalUser -name $Username
if (!$account) { Throw 'Account not found' }
Write-Host('Account Name : ' + $account.Name)
Write-Host('Display Name : ' + $account.FullName)
Write-Host('Description : ' + $account.Description)
Write-Host('Principal Source : ' + $account.PrincipalSource)
Write-Host('Expires : ' + $account.AccountExpires)
Write-Host('Last Logon : ' + $account.LastLogon)
Write-Host('Password Set : ' + $account.PasswordLastSet)
Write-Host('Password Locked : ' + (-not $account.UserMayChangePassword))
Write-Host('Enabled : ' + $account.Enabled)
Write-Host('SID : ' + $account.SID)
$groups = @()
Get-LocalGroup | % `
{
if ((get-localgroupmember $_.Name | select -property name | ? { $_ -match "\\$username" }) -ne $null)
{
$groups += $_.Name
}
}
write-host("Groups : {0}" -f ($groups -join ', '))
}
function GetDomainSid
{
$user = New-Object System.Security.Principal.NTAccount($Domain, $Username)
if (!$user) { Throw 'Account not found' }
$sidval = $user.Translate([System.Security.Principal.SecurityIdentifier])
$sidval.Value
}
function ReportDomainUser
{
$formatter = "{0,-$LabelWidth}: {1}"
$entry = New-Object System.DirectoryServices.DirectoryEntry('GC://' + $Domain)
$searcher = New-Object System.DirectoryServices.DirectorySearcher($entry)
$searcher.Filter = '(&((&(objectCategory=Person)(objectClass=User)))(samaccountname=' + $Username + '))'
$found = try
{
$searcher.FindAll() | % `
{
$properties = $_.properties
Write-Host
Write-Host ('-' * 80)
($formatter -f 'Account','') | Write-Host -NoNewline
$properties.samaccountname[0] | Write-Host -ForegroundColor Cyan -NoNewline
" ($($properties.adspath[0]))" | Write-Host -ForegroundColor DarkGray
($formatter -f 'Principal name',$properties.userprincipalname[0]) | Write-Host
($formatter -f 'Display name',$properties.displayname[0]) | Write-Host
$title = $properties.title[0]
if (![string]::IsNullOrWhiteSpace($title)) {
($formatter -f 'Title',"$($PSStyle.Italic)$title$($PSStyle.Reset)") | Write-Host -NoNewline
$att = $properties.extensionattribute8[0] # I think this is worker|manager|something|something...
if (![string]::IsNullOrWhiteSpace($att)) { Write-Host " ($att)" -NoNewline }
$company = $properties.company[0]
if (![string]::IsNullOrWhiteSpace($company)) { Write-Host " [$company]" -NoNewline }
Write-Host
}
$dept = $properties.department[0]
$group = $properties.extensionattribute9[0]
if (![string]::IsNullOrWhiteSpace($group)) { $dept = "$dept, $group" }
$org = $properties.extensionattribute3[0]
if (![string]::IsNullOrWhiteSpace($org)) { $dept = "$dept, $org" }
($formatter -f 'Department',$dept) | Write-Host
$manager = [string]($properties.manager[0])
if (!([String]::IsNullOrWhiteSpace(($manager)))) {
if ($manager.StartsWith('CN=')) {
$manager = $manager.Split(',')[0].Split('=')[1]
($formatter -f 'Manager',$manager) | Write-Host
}
}
$mail = $properties.mail[0]
($formatter -f 'Mail',$mail) | Write-Host
if ($properties.proxyaddresses.count -gt 0) {
$aliases = @()
$properties.proxyaddresses | foreach {
$alias = $_.ToLower().StartsWith('smtp:') ? $_.Substring(5) : $_
if ($alias -ne $mail) { $aliases += $alias }
}
if ($aliases.count -gt 0) {
WriteHanging -Label 'Mail aliases' -Items ($aliases | sort)
}
}
$address = "{0}, {1} {2}, {3}, {4}" -f $properties.streetaddress[0],$properties.l[0],$properties.st[0],$properties.postalcode[0],$properties.c[0]
($formatter -f 'Address',$address) | Write-Host
($formatter -f 'Telephone',$properties.telephonenumber[0]) | Write-Host
$mobile = $properties.mobile[0]
if (![string]::IsNullOrWhiteSpace($mobile)) { ($formatter -f 'Mobile',$mobile) | Write-Host }
$lastlogin = ConvertFileTIme $properties.lastlogontimestamp[0]
($formatter -f 'Last login',$lastlogin) | Write-Host
$lastset = ConvertFileTime $properties.pwdlastset[0]
($formatter -f 'Pwd last set',$lastset) | Write-Host -NoNewline
$days = ((Get-Date) - (Get-Date $lastset)).Days
$color = if ($days -lt 30) { 'Green' } elseif ($days -lt 60) { 'Yellow' } else { 'Red' }
Write-Host " ($days days ago)" -ForegroundColor $color
($formatter -f 'Last modified',$properties.whenchanged[0]) | Write-Host # no conversion needed, already string!
$sid = New-Object System.Security.Principal.SecurityIdentifier($properties.objectsid[0], 0)
($formatter -f 'SID',$sid) | Write-Host
if ($properties.directreports.count -gt 0) {
$reports = @()
$properties.directreports | foreach {
if ($_.StartsWith('CN=')) { $reports += $_.split(',')[0].split('=')[1] }
}
if ($reports.count -gt 0) {
Write-Host
WriteHanging -Label 'Direct reports' -Items ($reports | sort)
}
}
if ($properties.memberof.count -gt 0) {
$ships = @()
$properties.memberof | foreach {
if ($_.StartsWith('CN=')) { $ships += $_.split(',')[0].split('=')[1] }
}
if ($ships.count -gt 0) {
Write-Host
WriteHanging -Label 'Member of' -Items ($ships | sort)
}
}
$true
}
}
catch
{
write-host $_.Exception.Message -ForegroundColor Red
$true
}
if (!$found) {
Write-Host ... Count not find user "$domain\$username" -ForegroundColor Yellow
}
}
function ReportAllUsers
{
Get-LocalUser | `
Select-Object Name, FullName, Enabled, PasswordExpires, Description, Sid | `
Format-Table `
@{ Label = 'Name'; Expression = { FormatEnabledString $_ $_.Name } },
@{ Label = 'FullName'; Expression = { FormatEnabledString $_ $_.FullName } },
@{ Label = 'Enabled'; Expression = { FormatEnabledString $_ $_.Enabled } },
@{
Label = 'PasswordExpires'
Expression = { FormatEnabledString $_ $_.PasswordExpires.ToShortDateString() }
},
@{
Label = 'Description'
Expression = { FormatEnabledString $_ (($_.Description.ToCharArray() | select -First 30) -join '') }
},
@{ Label = 'Sid'; Expression = { FormatEnabledString $_ $_.Sid } } `
-AutoSize
}
}
Process
{
if ($All)
{
ReportAllUsers
}
elseif ($Domain -eq $env:COMPUTERNAME)
{
if ($SID) { GetLocalSid } else { ReportLocalUser }
}
else
{
if ($SID) { GetDomainSid } else { ReportDomainUser }
}
}
<#
if ($System)
{
# Get SID from username
$objUser = New-Object System.Security.Principal.NTAccount("SYSTEM")
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
ReportLocalUser -userSID $strSID.Value
return
# # Get username from SID
# $objSID = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-18")
# $objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
# $objUser.Value
}
#>