-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnlockUsers_2inf_classes.psm1
96 lines (73 loc) · 2.73 KB
/
UnlockUsers_2inf_classes.psm1
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
#[Void][reflection.assembly]::loadwithpartialname('System.Windows.Forms')
#[Void][reflection.assembly]::loadwithpartialname('System.Drawing')
#cannot be used like this
using namespace System.Collections.Generic #for List
using module ".\IconB64.psm1"
Write-Verbose "IconB64 imported"
#Write-Verbose $(GetIconB64)
class UsersUnlocker {
[System.Object] $liface
#constructor
UsersUnlocker([System.Object] $liface) { #he doesn't know the exact type
$unlocker_funs = [PSCustomObject]@{
'GetLUsers'=$this.GetLUsers
'UnlockLUsers'=$this.UnlockLUsers
}
$this.liface = $liface
#this.liface accepts the fn object... (the two liface are the same in fact...)
$this.liface.GetOuterFnObject($unlocker_funs) #the function's name should be formally known...
} #constructor
#[System.Object[]] GetLUsers() {
[List[PSObject]] GetLUsers() {
return $(Get-ADUser -Filter * -Properties SamAccountname, badPwdCount, badPasswordTime, lockedout, enabled | Where-Object {$_.lockedout -eq "True"} | % {
New-Object PSObject -Property @{
username = $_.SamAccountname
badPwdCount = $_.badPwdCount
badPasswordTime = [DateTime]::FromFileTime($_.badPasswordTime)
enabled = $_.enabled
}
} | Sort-Object -Property badPasswordTime)
} #GetUsers fn
[System.Object] UnlockLUsers([System.Object[]] $UserData, [bool] $enabledonly) {
Write-Verbose "in UnlockLUsers, UserUnlocker: $UserData"
Write-Verbose "$($UserData.gettype())"
if (! $UserData) {$UserData = @()} #(!)
$message = ""
$unlockednum = 0
$UserData | % {
$message += "$($_.num) " #should be before any row...
if (! $_.enabled -and $enabledonly) {
"$($_.username) is disabled - remains locked" | Tee-Object -variable msg | Write-Verbose
Write-Verbose "in if enabled, user: $($_.username)"
$message += "$msg`r`n"
$msg = ""
return
#return instead of continue
} #if enabled
$message += "$(if (!$_.Enabled) {"(disabled) "})" #maj stava i s dvete "
$msg = ""
$username = $_.username
$global:error.clear()
Try {
Unlock-ADAccount -Identity $_.username
}
Catch {
"unlockling $username error: $($global:error.Exception.Message)" | Tee-Object -variable msg | Write-Verbose
$message += "$msg`r`n"
$msg = ""
}
Finally {
If (! $global:error) {
"$($_.username) unlocked" | Tee-Object -variable msg | Write-Verbose
$message += "$msg`r`n"
$msg = ""
$unlockednum += 1
} #if error
} #finally
} #%
return @{"message" = $message; "unlockednum" = $unlockednum}
} #UnlockLUsers fn
[void] Run() {
$this.liface.Show()
}
} #class UnlockUsers