-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsModule.psm1
More file actions
30 lines (28 loc) · 1.17 KB
/
Copy pathWindowsModule.psm1
File metadata and controls
30 lines (28 loc) · 1.17 KB
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
function ValidateLocalAccount {
param(
[string]$Username,
[string]$Password
)
$isValid = $null
try {
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$domain_role = Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty DomainRole
$contextType = if ($domain_role -eq 5 -or $domain_role -eq 4) {
[System.DirectoryServices.AccountManagement.ContextType]::Domain
} else {
[System.DirectoryServices.AccountManagement.ContextType]::Machine
}
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext($contextType, $env:COMPUTERNAME)
$user = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($pc, $Username)
if ($null -eq $user) {
Write-Host "Account $($Username) does not exist."
return $null
} else {
$isValid = $pc.ValidateCredentials($Username, $Password)
}
} catch {
Write-Host "An error occurred in function ""ValidateLocalAccount"", exiting with exception message ""$($_.Exception.Message)"""
exit(1)
}
return $isValid
}