PoshCode Logo PowerShell Code Repository

Set account password by geraldo 17 months ago
View followups from SHAN ALI | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2109"></script>download | new post

This script will allow you to set the password for an account on a local or remote machine/s. A report is then generated when done along with an error log. Scripts accepts pipeling input for the computer/s. If any errors are encountered, a log will be generated as well.

  1. Function Set-Password {
  2. #requires -version 2.0
  3.  
  4. <#
  5. .Synopsis
  6.     Allows the changing of the local account password on a local or remote machine.
  7. .Description
  8.     Allows the changing of the local account password on a local or remote machine.
  9. .Parameter computer
  10.     Computer that the password will be changed on.  Supports a single computer or collection of computers and can be processed
  11.     through the pipeline.
  12. .Parameter user
  13.     Account that will have the password changed.    
  14. .Example
  15.     Set-Password -computer 'server' -user 'Administrator'
  16.    
  17.     User will be prompted to type in the password for Administrator prior to being changed on 'server'  
  18. .Example
  19.     Set-Password -computer @('server','server2') -user 'Administrator'
  20.    
  21.     User will be prompted to type in the password for Administrator prior to being changed on 'server' and 'server2'
  22. .Example
  23.     @('server','server2') | Set-Password -user 'Administrator'
  24.    
  25.     User will be prompted to type in the password for Administrator prior to being changed on 'server' and 'server2'  
  26. .Example
  27.     Set-Password -computer (Get-Content hosts.txt) -user 'Administrator'
  28.    
  29.     User will be prompted to type in the password for Administrator prior to being changed on 'server' and 'server2'      
  30.        
  31. .Inputs
  32.     None
  33. .Outputs
  34.     None
  35. .Link
  36.    http://boeprox.wordpress.com
  37.  
  38. .Notes
  39.  NAME:      Set-Password
  40.  VERSION:   1.0
  41.  AUTHOR:    Boe Prox
  42.  Date:      26 August 2010
  43.  
  44. #>
  45.  
  46. [CmdletBinding(
  47.     SupportsShouldProcess = $True,
  48.     ConfirmImpact = 'low',
  49.         DefaultParameterSetName = 'server'
  50.     )]
  51. Param (    
  52.     [Parameter(
  53.      ValueFromPipeline=$True,
  54.      Position=0,
  55.      Mandatory=$True,
  56.      HelpMessage="List of servers")]
  57.      [ValidateNotNullOrEmpty()]
  58.     [array]$computer,
  59.     [Parameter(
  60.      ValueFromPipeline=$False,
  61.      Position=1,
  62.      Mandatory=$True,
  63.      HelpMessage="Account to change password")]
  64.      [ValidateNotNullOrEmpty()]
  65.     [string]$user    
  66.     )
  67. Begin {
  68.     Write-Verbose "Building container for report"
  69.     $arrlist = @()
  70.     Write-Verbose "Prompting for password"
  71.     $password = Read-Host "Type password -- VERIFY BEFORE CLICKING RETURN!!!"
  72.     Write-Verbose "Checking for existence of error log and clearing contents"
  73.     $errorlog = "passwordchangeerrors.txt"
  74.     If ([system.io.file]::exists($errorlog)) {
  75.         Clear-content $errorlog
  76.         }
  77.     }
  78. Process {
  79.     #Iterate through computer list
  80.     ForEach ($c in $computer) {
  81.         $temp = New-Object PSobject
  82.         Try {
  83.             Write-Verbose "Testing Connection to $($c)"
  84.             Test-Connection -comp $c -count 1 -ea stop | out-null
  85.             #Verify account exists before attempting password change
  86.             Write-Verbose "Verifying that $($user) exists on $($computer)"
  87.             $colusers = ([ADSI]("WinNT://$c,computer")).children | ? {$_.psbase.schemaClassName -eq "User"} | Select -expand Name
  88.             If ($colusers -contains $user) {
  89.                 Write-Host -foregroundcolor Green "Changing password on $c..."
  90.                 $ErrorActionPreference = 'stop'
  91.                 Try {
  92.                     #Make connection to remote/local computer and user account
  93.                     $account = [adsi]("WinNT://"+$c+"/$user, user")
  94.                     #Change password on user account
  95.                     If ($pscmdlet.ShouldProcess($($user))) {
  96.                         $account.psbase.invoke("SetPassword", $password)
  97.                         $account.psbase.CommitChanges()
  98.                         }
  99.                     Write-Verbose "Adding information to report"    
  100.                     $temp | Add-Member NoteProperty TimeStamp "$(get-date)"                  
  101.                     $temp | Add-Member NoteProperty Server $c
  102.                     $temp | Add-Member NoteProperty Account $user
  103.                     $temp | Add-Member NoteProperty Status "Password Changed"    
  104.                     $temp | Add-Member NoteProperty Notes ""
  105.                     }
  106.                 Catch {
  107.                     $errorflag = $True
  108.                     Write-Verbose "Writing errors to $($errorlog)"
  109.                     "$(get-date) :: Server:$($c) :: $($error[0].exception)" | Out-File -append $errorlog
  110.                     Write-Verbose "Adding information to report"        
  111.                     $temp | Add-Member NoteProperty TimeStamp "$(get-date)"
  112.                     $temp | Add-Member NoteProperty Server $c
  113.                     $temp | Add-Member NoteProperty Account $user
  114.                     $temp | Add-Member NoteProperty Status "Error Changing Password"
  115.                     $temp | Add-Member NoteProperty Notes $error[0]                
  116.                     }
  117.                 }
  118.         Else {
  119.             $errorflag = $True
  120.             Write-Verbose "Writing errors to $($errorlog)"
  121.             "$(get-date) :: Server:$($c) :: $($user) does not exist!)" | Out-File -append  $errorlog
  122.             Write-Verbose "Adding information to report"                  
  123.             $temp | Add-Member NoteProperty TimeStamp "$(get-date)"
  124.             $temp | Add-Member NoteProperty Server $c
  125.             $temp | Add-Member NoteProperty Account $user
  126.             $temp | Add-Member NoteProperty Status "Unable to change password"
  127.             $temp | Add-Member NoteProperty Notes  "Username does not exist"            
  128.             }                                    
  129.             }
  130.         Catch {
  131.             $errorflag = $True
  132.             Write-Verbose "Writing errors to $($errorlog)"
  133.             "$(get-date) :: Server:$($c) :: $($error[0].exception)" | Out-File -append  $errorlog
  134.             Write-Verbose "Adding information to report"          
  135.             $temp | Add-Member NoteProperty TimeStamp "$(get-date)"            
  136.             $temp | Add-Member NoteProperty Server $c
  137.             $temp | Add-Member NoteProperty Account $user
  138.             $temp | Add-Member NoteProperty Status "Error Connecting to computer"
  139.             $temp | Add-Member NoteProperty Notes $error[0]
  140.             }  
  141.         Finally {
  142.             #Merge temp report with main report
  143.             Write-Verbose "Merging report"
  144.             $arrlist += $temp
  145.             }    
  146.         }
  147.     }
  148. End {  
  149.     #Generate report to screen
  150.     Write-Verbose "Generating report"          
  151.     $arrlist
  152.     If ($errorflag) {
  153.         Write-Host -fore Yellow "Errors were reported during run, please look at $($pwd)\$($errorlog) for more details."
  154.         }
  155.     Write-Verbose "Removing password from variable `$password"    
  156.     $password = $Null        
  157.     }
  158. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me