Advertisement
Guest User

Untitled

a guest
Jul 12th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. [cmdletbinding()]
  2. param(
  3. # Give the user the opton to user a -User command line parameter
  4. [string]$User = $(Read-Host "Enter a samAccountName")
  5. )
  6.  
  7. # Show $User details on console
  8. # Consider which properties you actually need
  9. # Using * is a memory hog and unnecesary
  10. try {
  11. $UserObject = Get-ADUser -Identity $User -Server adVM -Properties EmployeeNumber -ErrorAction Stop
  12. # Use Out-Host to work around issue with Read-Host swallowing the output
  13. $UserObject | Select Name, samAccountName, EmployeeNumber, Enabled | Out-Host
  14. } catch {
  15. Write-Error "Error encountered locating user: $($_)"
  16. Exit
  17. }
  18.  
  19. # Verify $User is not empty and reset the password with a secure string prompt
  20. # if $User exists, it will return true, no need to compare to $null
  21. If ($UserObject) {
  22. try {
  23. $newPassword = (Read-Host -Prompt "Provide New Password" -AsSecureString)
  24. # We went through all the trouble of pulling the user object above, may as well use it
  25. $UserObject | Set-ADAccountPassword -NewPassword $newpassword -Reset -ErrorAction Stop
  26. } catch {
  27. Write-Error "Error encountered setting password: $($_)"
  28. Exit
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement