Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [cmdletbinding()]
- param(
- # Give the user the opton to user a -User command line parameter
- [string]$User = $(Read-Host "Enter a samAccountName")
- )
- # Show $User details on console
- # Consider which properties you actually need
- # Using * is a memory hog and unnecesary
- try {
- $UserObject = Get-ADUser -Identity $User -Server adVM -Properties EmployeeNumber -ErrorAction Stop
- # Use Out-Host to work around issue with Read-Host swallowing the output
- $UserObject | Select Name, samAccountName, EmployeeNumber, Enabled | Out-Host
- } catch {
- Write-Error "Error encountered locating user: $($_)"
- Exit
- }
- # Verify $User is not empty and reset the password with a secure string prompt
- # if $User exists, it will return true, no need to compare to $null
- If ($UserObject) {
- try {
- $newPassword = (Read-Host -Prompt "Provide New Password" -AsSecureString)
- # We went through all the trouble of pulling the user object above, may as well use it
- $UserObject | Set-ADAccountPassword -NewPassword $newpassword -Reset -ErrorAction Stop
- } catch {
- Write-Error "Error encountered setting password: $($_)"
- Exit
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement