SHARE
TWEET

Untitled

a guest Oct 18th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Update-ADPassword
  2. {
  3.   param(
  4.     [string]$Domain = $env:USERDOMAIN
  5.   )
  6.  
  7.   try {
  8.     Add-Type -AssemblyName System.DirectoryServices.AccountManagement
  9.     $ctx = [System.DirectoryServices.AccountManagement.PrincipalContext]::new('Domain', $Domain)
  10.     $acc = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($ctx, ($currCred = Get-Credential -Message 'Input your current credentials').UserName)
  11.     if($? -and @($acc.Count) -eq 1) {
  12.       $acc.ChangePassword($currCred.GetNetworkCredential().Password, $(Get-Credential -UserName $currCred.UserName -Message "Input your new password").GetNetworkCredential().Password)
  13.       Write-Host "Password successfully changed for $($currCred.UserName) in $Domain"
  14.     }
  15.     else {
  16.       if(@($acc).Count){
  17.         Write-Warning "No unambiguous account match found in $Domain"
  18.       }
  19.       else {
  20.         Write-Warning "No user account found in $Domain"
  21.       }
  22.     }
  23.   }
  24.   catch {
  25.     throw
  26.     return
  27.   }
  28.   finally {
  29.     Clear-Variable 'currCred' -PassThru |Remove-Variable
  30.   }
  31. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top