Skip to content

Commit ca90514

Browse files
committed
get-account now reports local accounts too
1 parent fca0e0f commit ca90514

File tree

1 file changed

+69
-32
lines changed

1 file changed

+69
-32
lines changed

Modules/Scripts/Get-Account.ps1

+69-32
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,86 @@
11
<#
22
.SYNOPSIS
33
Report the account information for the given username and specified domain.
4-
get-account -username foo -domain bar
4+
5+
.DESCRIPTION
6+
Can report on either a local user account or an ActiveDirectory account.
7+
8+
.PARAMETER username
9+
The account username to report.
10+
11+
.PARAMETER domain
12+
The ActiveDirectory domain to use. Default is $env:USERDOMAIN.
513
#>
614
param(
715
$username = $(throw "Please specify a username"),
816
$domain = $env:USERDOMAIN)
917

1018
if ($domain -eq $env:COMPUTERNAME)
1119
{
12-
Write-Host
13-
Write-Host 'Get-Account currently only works for AD domains' -ForegroundColor Red
14-
return
15-
}
20+
# local computer account...
21+
22+
$account = Get-LocalUser -name $username
23+
if (!$account)
24+
{
25+
Throw 'Account not found'
26+
}
27+
28+
Write-Host('Account Name : ' + $account.Name)
29+
Write-Host('Display Name : ' + $account.FullName)
30+
Write-Host('Description : ' + $account.Description)
31+
Write-Host('Principal Source : ' + $account.PrincipalSource)
32+
Write-Host('Expires : ' + $account.AccountExpires)
33+
Write-Host('Last Logon : ' + $account.LastLogon)
34+
Write-Host('Password Set : ' + $account.PasswordLastSet)
35+
Write-Host('Password Locked : ' + (-not $account.UserMayChangePassword))
36+
Write-Host('Enabled : ' + $account.Enabled)
37+
Write-Host('SID : ' + $account.SID)
1638

17-
$found = $false
18-
$entry = New-Object System.DirectoryServices.DirectoryEntry('GC://' + $domain)
19-
$searcher = New-Object System.DirectoryServices.DirectorySearcher($entry)
20-
$searcher.Filter = '(&((&(objectCategory=Person)(objectClass=User)))(samaccountname=' + $username + '))'
21-
try
39+
$groups = @()
40+
Get-LocalGroup | % `
41+
{
42+
if ((get-localgroupmember $_.Name | select -property name | ? { $_ -match "\\$username" }) -ne $null)
43+
{
44+
$groups += $_.Name
45+
}
46+
}
47+
write-host("Groups : {0}" -f ($groups -join ', '))
48+
}
49+
else
2250
{
23-
$searcher.FindAll() | % `
24-
{
25-
$properties = $_.GetDirectoryEntry().Properties
26-
Write-Host('Account Name : ' + $properties['sAMAccountName'].Value)
27-
Write-Host('Display Name : ' + $properties['displayName'].Value)
28-
Write-Host('Mail : ' + $properties['mail'].Value)
29-
Write-Host('Telephone : ' + $properties['telephoneNumber'].Value)
30-
31-
$manager = [string]($properties['manager'].Value)
32-
if (!([String]::IsNullOrEmpty($manager))) {
33-
if ($manager.StartsWith('CN=')) {
34-
$manager = $manager.Split(',')[0].Split('=')[1]
35-
Write-Host('Manager : ' + $manager)
51+
# ActiveDirectory account...
52+
53+
$found = $false
54+
$entry = New-Object System.DirectoryServices.DirectoryEntry('GC://' + $domain)
55+
$searcher = New-Object System.DirectoryServices.DirectorySearcher($entry)
56+
$searcher.Filter = '(&((&(objectCategory=Person)(objectClass=User)))(samaccountname=' + $username + '))'
57+
try
58+
{
59+
$searcher.FindAll() | % `
60+
{
61+
$properties = $_.GetDirectoryEntry().Properties
62+
Write-Host('Account Name : ' + $properties['sAMAccountName'].Value)
63+
Write-Host('Display Name : ' + $properties['displayName'].Value)
64+
Write-Host('Mail : ' + $properties['mail'].Value)
65+
Write-Host('Telephone : ' + $properties['telephoneNumber'].Value)
66+
67+
$manager = [string]($properties['manager'].Value)
68+
if (!([String]::IsNullOrEmpty($manager))) {
69+
if ($manager.StartsWith('CN=')) {
70+
$manager = $manager.Split(',')[0].Split('=')[1]
71+
Write-Host('Manager : ' + $manager)
72+
}
3673
}
74+
75+
$found = $true
3776
}
77+
}
78+
catch
79+
{
80+
}
3881

39-
$found = $true
82+
if (!$found)
83+
{
84+
Write-Host ... Count not find user "$domain\$username" -ForegroundColor Yellow
4085
}
4186
}
42-
catch
43-
{
44-
}
45-
46-
if (!$found)
47-
{
48-
Write-Host ... Count not find user "$domain\$username" -ForegroundColor Yellow
49-
}

0 commit comments

Comments
 (0)