1
1
<#
2
2
. SYNOPSIS
3
3
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.
5
13
#>
6
14
param (
7
15
$username = $ (throw " Please specify a username" ),
8
16
$domain = $env: USERDOMAIN )
9
17
10
18
if ($domain -eq $env: COMPUTERNAME )
11
19
{
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 )
16
38
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
22
50
{
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
+ }
36
73
}
74
+
75
+ $found = $true
37
76
}
77
+ }
78
+ catch
79
+ {
80
+ }
38
81
39
- $found = $true
82
+ if (! $found )
83
+ {
84
+ Write-Host ... Count not find user " $domain \$username " - ForegroundColor Yellow
40
85
}
41
86
}
42
- catch
43
- {
44
- }
45
-
46
- if (! $found )
47
- {
48
- Write-Host ... Count not find user " $domain \$username " - ForegroundColor Yellow
49
- }
0 commit comments