-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathAccountLockOut.ps1
49 lines (41 loc) · 1.63 KB
/
AccountLockOut.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
function Get-AccountLockOut {
param(
[Parameter(Position=0, Mandatory=$true)]
[System.String]
$Identity,
[Parameter(Position=1, Mandatory=$false)]
[System.String]
$Domain = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().RootDomain.Name
)
if(([AppDomain]::CurrentDomain.GetAssemblies() |
%{$_.ManifestModule.name}) -notcontains "System.DirectoryServices.AccountManagement.dll") {
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
}
$pc = [System.DirectoryServices.AccountManagement.PrincipalContext]
$up = [System.DirectoryServices.AccountManagement.UserPrincipal]
$context = New-Object $pc -ArgumentList "Domain", $domain
$user = $up::FindByIdentity($context,$Identity)
New-Object PSObject -Property @{
Username = $Identity
IsLockedOut = $user.IsAccountLockedOut()
}
}
function Disable-AccountLockOut {
param(
[Parameter(Position=0, Mandatory=$true)]
[System.String]
$Identity,
[Parameter(Position=1, Mandatory=$false)]
[System.String]
$Domain = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().RootDomain.Name
)
if(([AppDomain]::CurrentDomain.GetAssemblies() |
%{$_.ManifestModule.name}) -notcontains "System.DirectoryServices.AccountManagement.dll") {
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
}
$pc = [System.DirectoryServices.AccountManagement.PrincipalContext]
$up = [System.DirectoryServices.AccountManagement.UserPrincipal]
$context = New-Object $pc -ArgumentList "Domain", $domain
$user = $up::FindByIdentity($context,$Identity)
$user.UnlockAccount()
}