Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #*=============================================================================
- #* Script Name: adpwchange2014.ps1
- #* Created: 2014-10-07
- #* Author:
- #* Purpose: This is a simple script that queries AD users.
- #* Reference Website: http://theboywonder.co.uk/2012/07/29/executing-powershell-using-php-and-iis/
- #*
- #*=============================================================================
- #*=============================================================================
- #* PARAMETER DECLARATION
- #*=============================================================================
- param(
- [string]$base64_username,
- [string]$base64_newpassword,
- [string]$base64_oldpassword,
- [string]$rand
- )
- #*=============================================================================
- #* IMPORT LIBRARIES
- #*=============================================================================
- if ((Get-Module | where {$_.Name -match "ActiveDirectory"}) -eq $null){
- #Loading module
- Write-Host "Loading module AcitveDirectory..."
- Import-Module ActiveDirectory
- }else{
- write-output "Error: Please install ActiveDirectory Module"
- EXIT
- NUL
- Stop-Process -processname powershell*
- }
- #*=============================================================================
- #* PARAMETERS
- #*=============================================================================
- $username = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($base64_username))
- $newpassword = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($base64_newpassword))
- $oldpassword = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($base64_oldpassword))
- #*=============================================================================
- #* INITIALISE VARIABLES
- #*=============================================================================
- # Increase buffer width/height to avoid PowerShell from wrapping the text before
- # sending it back to PHP (this results in weird spaces).
- $pshost = Get-Host
- $pswindow = $pshost.ui.rawui
- $newsize = $pswindow.buffersize
- $newsize.height = 1000
- $newsize.width = 300
- $pswindow.buffersize = $newsize
- #*=============================================================================
- #* EXCEPTION HANDLER
- #*=============================================================================
- #*=============================================================================
- #* FUNCTION LISTINGS
- #*=============================================================================
- Function Test-ADAuthentication {
- Param($Auth_User, $Auth_Pass)
- Write-Output "Running Function Test-ADAuthenication"
- $domain = $env:USERDOMAIN
- Add-Type -AssemblyName System.DirectoryServices.AccountManagement
- $ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
- $pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext($ct, $domain)
- $pc.ValidateCredentials($Auth_User, $Auth_Pass).ToString()
- }
- Function Set-ADAuthentication{
- Param($Auth_User,$Auth_OldPass, $Auth_NewPass)
- Write-Output "Running Function Set-ADAuthenication"
- $domain = $env:USERDOMAIN
- $SecureString_NewPass = ConvertTo-SecureString $Auth_NewPass -AsPlainText -Force
- $SecureString_OldPass = ConvertTo-SecureString $Auth_OldPass -AsPlainText -Force
- #Running -whatif to simulate results
- #Therefore we expect "Failed: Validation of Password Change" as it was not changed
- Set-ADAccountPassword -Identity $Auth_User -NewPassword $SecureString_NewPass -OldPassword $SecureString_OldPass -PassThru
- $authentication = Test-ADAuthentication $Auth_User $Auth_NewPass
- if ($authentication -eq $TRUE) {
- Write-Output "Success:$rand Validation of Password Changed"
- }elseif ($authentication -eq $FALSE) {
- Write-Output "Failed:$rand Validation of Password Change"
- }else{
- Write-Output "Error: EOS"
- EXIT
- NUL
- Stop-Process -processname powershell*
- }
- }
- #*=============================================================================
- #* Function: function1
- #* Purpose: This function does X Y Z
- #* =============================================================================
- #*=============================================================================
- #* END OF FUNCTION LISTINGS
- #*=============================================================================
- #*=============================================================================
- #* SCRIPT BODY
- #*=============================================================================
- Write-Output $PSVersionTable
- Write-Output " "
- $authentication = Test-ADAuthentication "$username" "$oldpassword"
- if ($authentication -eq $TRUE) {
- Set-ADAuthentication $username $oldpassword $newpassword
- }elseif ($authentication -eq $FALSE) {
- Write-Output "Failed:$rand Authentication"
- }else {Write-Output "Error: EOS"
- EXIT
- NUL
- Stop-Process -processname powershell*
- }
- #*=============================================================================
- #* SCRIPT Exit
- #*=============================================================================
- Write-Output "End Of Script"
- EXIT
- NUL
- Stop-Process -processname powershell*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement