Advertisement
Werezwolf

Powershell AD PW Changer

Oct 6th, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #*=============================================================================
  2. #* Script Name: adpwchange2014.ps1
  3. #* Created: 2014-10-07
  4. #* Author:
  5. #* Purpose: This is a simple script that queries AD users.
  6. #* Reference Website: http://theboywonder.co.uk/2012/07/29/executing-powershell-using-php-and-iis/
  7. #*
  8. #*=============================================================================
  9.  
  10. #*=============================================================================
  11. #* PARAMETER DECLARATION
  12. #*=============================================================================
  13. param(
  14. [string]$base64_username,
  15. [string]$base64_newpassword,
  16. [string]$base64_oldpassword,
  17. [string]$rand
  18. )
  19.  
  20. #*=============================================================================
  21. #* IMPORT LIBRARIES
  22. #*=============================================================================
  23.  
  24. if ((Get-Module | where {$_.Name -match "ActiveDirectory"}) -eq $null){
  25.     #Loading module
  26.     Write-Host "Loading module AcitveDirectory..."
  27.     Import-Module ActiveDirectory
  28.     }else{
  29.     write-output "Error: Please install ActiveDirectory Module"
  30.     EXIT
  31.     NUL
  32.     Stop-Process -processname powershell*
  33.     }
  34. #*=============================================================================
  35. #* PARAMETERS
  36. #*=============================================================================
  37. $username = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($base64_username))
  38. $newpassword = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($base64_newpassword))
  39. $oldpassword = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($base64_oldpassword))
  40.    
  41. #*=============================================================================
  42. #* INITIALISE VARIABLES
  43. #*=============================================================================
  44. # Increase buffer width/height to avoid PowerShell from wrapping the text before
  45. # sending it back to PHP (this results in weird spaces).
  46. $pshost = Get-Host
  47. $pswindow = $pshost.ui.rawui
  48. $newsize = $pswindow.buffersize
  49. $newsize.height = 1000
  50. $newsize.width = 300
  51. $pswindow.buffersize = $newsize
  52.  
  53. #*=============================================================================
  54. #* EXCEPTION HANDLER
  55. #*=============================================================================
  56.  
  57. #*=============================================================================
  58. #* FUNCTION LISTINGS
  59. #*=============================================================================
  60.  
  61.     Function Test-ADAuthentication {
  62.         Param($Auth_User, $Auth_Pass)
  63.         Write-Output "Running Function Test-ADAuthenication"
  64.         $domain = $env:USERDOMAIN
  65.        
  66.         Add-Type -AssemblyName System.DirectoryServices.AccountManagement
  67.         $ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
  68.         $pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext($ct, $domain)
  69.         $pc.ValidateCredentials($Auth_User, $Auth_Pass).ToString()
  70.         }
  71.    
  72.     Function Set-ADAuthentication{
  73.         Param($Auth_User,$Auth_OldPass, $Auth_NewPass)
  74.         Write-Output "Running Function Set-ADAuthenication"
  75.         $domain = $env:USERDOMAIN
  76.         $SecureString_NewPass = ConvertTo-SecureString $Auth_NewPass -AsPlainText -Force
  77.         $SecureString_OldPass = ConvertTo-SecureString $Auth_OldPass -AsPlainText -Force
  78.         #Running -whatif to simulate results
  79.         #Therefore we expect "Failed: Validation of Password Change" as it was not changed
  80.         Set-ADAccountPassword -Identity $Auth_User -NewPassword $SecureString_NewPass -OldPassword $SecureString_OldPass -PassThru
  81.        
  82.         $authentication = Test-ADAuthentication $Auth_User $Auth_NewPass
  83.         if ($authentication -eq $TRUE) {
  84.             Write-Output "Success:$rand Validation of Password Changed"
  85.             }elseif ($authentication -eq $FALSE) {
  86.             Write-Output "Failed:$rand Validation of Password Change"
  87.             }else{
  88.                 Write-Output "Error: EOS"
  89.                 EXIT
  90.                 NUL
  91.                 Stop-Process -processname powershell*
  92.                 }
  93.         }
  94.  
  95. #*=============================================================================
  96. #* Function: function1
  97. #* Purpose: This function does X Y Z
  98. #* =============================================================================
  99.  
  100. #*=============================================================================
  101. #* END OF FUNCTION LISTINGS
  102. #*=============================================================================
  103.  
  104. #*=============================================================================
  105. #* SCRIPT BODY
  106. #*=============================================================================
  107. Write-Output $PSVersionTable
  108. Write-Output "  "
  109. $authentication = Test-ADAuthentication "$username" "$oldpassword"
  110. if ($authentication -eq $TRUE) {
  111.     Set-ADAuthentication $username $oldpassword $newpassword
  112.     }elseif ($authentication -eq $FALSE) {
  113.     Write-Output "Failed:$rand Authentication"
  114.     }else {Write-Output "Error: EOS"
  115.     EXIT
  116.     NUL
  117.     Stop-Process -processname powershell*
  118.     }
  119.    
  120. #*=============================================================================
  121. #* SCRIPT Exit
  122. #*=============================================================================
  123. Write-Output "End Of Script"
  124. EXIT
  125. NUL
  126. Stop-Process -processname powershell*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement