Simple Network Authentication

Friday, July 25, 2014
<#
Purpose:    Validate network credentials, preventing lockouts.
Notes:            This is a pretty simple script, accepting only one parameter($Creds).
                  Store your credentials in a variable with Get-Credential, and pass it
                  to this function.
Caveat(s):  None that I am aware of.
#>
 
Function ValidateCredentials($Creds){
            Add-Type -AssemblyName System.DirectoryServices.AccountManagement
            $Info = $Creds.GetNetworkCredential()
            if($info.Domain -eq ''){
                  $info.Domain = $env:USERDOMAIN
                  }
            $TypeDomain = [System.DirectoryServices.AccountManagement.ContextType]::Domain
            Try{
                  $PC = New-Object System.DirectoryServices.AccountManagement.PrincipalContext $TypeDomain,$info.Domain
                  $PC.ValidateCredentials($info.UserName,$info.Password)
                  Write-Verbose ("Authentication Success")
                  }
            Catch{
                  Write-Error ("Authentication Failed")
                  }
            }

No comments:

Post a Comment

Please keep all comments clean!