-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGet-NetUser.ps1
79 lines (62 loc) · 2 KB
/
Get-NetUser.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
Function Get-NetUser {
Param(
$UserName = '*',
[switch]$Domain
)
if(!$script:userlist){
$output = if($Domain){
net user /domain
}
else{
net user
}
$script:userlist = ($output -notmatch '--|\\\\|\.$|^$') -split '\s{2,}' | Where-Object {$_}
}
foreach($user in $UserName){
if($user -match '\*'){
if($user -eq '*'){
$script:userlist.ForEach{Get-NetUser -UserName $_}
break
}
$script:userlist |
Where-Object {$_ -like $user} |
ForEach-Object {Get-NetUser -UserName $_}
continue
}
if(!$user -or $user -eq ' '){continue}
$ht = [ordered]@{}
$output = if($Domain){
net user $user /domain
}
else{
net user $user
}
switch -Regex ($output){
' - ' {
if($_ -match '^\s+(\w.+ - \d.+$)'){
$ht[$currentproperty] += $matches[1]
}
elseif($_ -match '^([^-]+?)\s{2,}(\w.+)$'){
$currentproperty = $matches[1]
$ht[$currentproperty] = ,$matches[2]
}
continue
}
'\*' {
if($_ -match '^([^\*]+?)\s{2,}(\s?.+)$'){
if($matches[1] -eq ' '){
$ht[$currentproperty] += [regex]::Matches($matches[2],'(?<=\*)(.+?(?=\s?\*|$))').value.trim()
}
else{
$currentproperty = $matches[1]
$ht[$currentproperty] = [regex]::Matches($matches[2],'(?<=\*)(.+?(?=\s?\*|$))').value.trim()
}
}
}
'^([^\*]+?)\s{2,}([^\*]+)$' {
$ht.Add($matches[1],$matches[2])
}
}
[PSCustomObject]$ht
}
}