-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Get-Account.ps1
190 lines (165 loc) · 5.39 KB
/
Get-Account.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
178
179
180
181
182
183
184
185
186
187
188
189
190
<#
.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 usrs
.PARAMETER Username
The account username to report.
.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
{
$esc = [char]27
function GetLocalSid
{
$account = Get-LocalUser -Name $Username
if (!$account) { Throw 'Account not found' }
$account.SID
}
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
{
$found = $false
$entry = New-Object System.DirectoryServices.DirectoryEntry('GC://' + $Domain)
$searcher = New-Object System.DirectoryServices.DirectorySearcher($entry)
$searcher.Filter = '(&((&(objectCategory=Person)(objectClass=User)))(samaccountname=' + $Username + '))'
try
{
$searcher.FindAll() | % `
{
$properties = $_.GetDirectoryEntry().Properties
#$properties
Write-Host('Account Name : ' + $properties['sAMAccountName'].Value)
Write-Host('Display Name : ' + $properties['displayName'].Value)
Write-Host('Mail : ' + $properties['mail'].Value)
Write-Host('Telephone : ' + $properties['telephoneNumber'].Value)
$lastset = [datetime]::FromFileTimeUtc((ConvertADSLargeInteger $properties['pwdLastSet'].Value))
Write-Host('Pwd last set : ' + $lastset)
#Write-Host('Member of : ' + $properties['memberOf'].Value)
$manager = [string]($properties['manager'].Value)
if (!([String]::IsNullOrEmpty($manager))) {
if ($manager.StartsWith('CN=')) {
$manager = $manager.Split(',')[0].Split('=')[1]
Write-Host('Manager : ' + $manager)
}
}
$found = $true
}
}
catch
{
}
if (!$found)
{
Write-Host ... Count not find user "$domain\$username" -ForegroundColor Yellow
}
}
function ConvertADSLargeInteger([object] $adsLargeInteger)
{
$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
}
function ReportAllUsers
{
Get-LocalUser | `
Select-Object Name, FullName, Enabled, PasswordExpires, Description, Sid | `
Format-Table `
@{ Label = 'Name'; Expression = { MakeAllUsersExpression $_ $_.Name } },
@{ Label = 'FullName'; Expression = { MakeAllUsersExpression $_ $_.FullName } },
@{ Label = 'Enabled'; Expression = { MakeAllUsersExpression $_ $_.Enabled } },
@{
Label = 'PasswordExpires'
Expression = { MakeAllUsersExpression $_ $_.PasswordExpires.ToShortDateString() }
},
@{
Label = 'Description'
Expression = { MakeAllUsersExpression $_ (($_.Description.ToCharArray() | select -First 30) -join '') }
},
@{ Label = 'Sid'; Expression = { MakeAllUsersExpression $_ $_.Sid } } `
-AutoSize
}
function MakeAllUsersExpression
{
param($user, $value)
if ($user.Enabled) { $color = '97' }
else { $color = '90' }
"$esc[$color`m$($value)$esc[0m"
}
}
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
}
#>