Last active
July 12, 2017 23:41
-
-
Save rodmhgl/243c0915a5672cf90f34b7a42cd8e66c to your computer and use it in GitHub Desktop.
For James Espinoza - Help Desk script to reset user's password
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Huh...I've never had that work before. It's working for me now, though.
Then again, I've never tested it directly from the DC before, so it may just be an issue when going through remoting. I'll have to set up another machine to test that.