Link to home
Start Free TrialLog in
Avatar of missymadi
missymadi

asked on

How to get Powershell to return

Experts,
 
         Attached below I have a script that we run in our test LAB. The script should do the following:
1. Search for a user and return password information
2. Ask user if they want to reset the users password based on the info returned
3. Take the user's response and either change the users password or exit script
4. Password activity should be written out to text file.

Thanks, Missymadi
$user = read-host "Enter UserName"
Get-qaduser $User | Select PasswordLastSet, PasswordAge,PasswordExpires, PasswordNeverExpires, UserMustChangePassword, PasswordIsExpired, PasswordStatus 
$User = read-host "Do you want to reset a user's password?"
$strResponse = Read-Host "[1] Yes, [2] No"

If ($strResponse -eq "1" )

{$users = get-qaduser -samaccountname USERNAME 
$users | %{
set-qaduser -Userpassword 'Test'
get-qaduser -samaccountname $_.samaccountname | Select samaccountname, passwordlastset | out-file c:\PwdChanged.txt -noclobber
}

   else{Write-Host "Exiting Search Script"}
}

Open in new window

Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

You're not far off it really.

Chris
# Ask for a username

$Username = Read-Host "Enter UserName"

# Search for the user(s)

$Users = Get-QADUser -SamAccountName $Username | Select-Object Name, DN, 
  PasswordLastSet, PasswordAge,PasswordExpires, PasswordNeverExpires, 
  UserMustChangePassword, PasswordIsExpired, PasswordStatus

# Display the user(s)

$Users

# Ask if they wish to proceed

$Response = Read-Host "Do you want to reset a user's password?`n[1] Yes, [2] No"

# Do the work

If ($Response -eq "1" ) {
  # For each user we found earlier, set the password. Log a few things and the name of the 
  # user running this script 

  $Users | ForEach-Object { Set-QADUser $_.DN -UserPassword 'Test' } |
    Select-Object SamAccountName, PasswordLastSet, @{n='SetBy';e={ $Env:Username }} |
    Out-File c:\PwdChanged.txt -Append
}

Open in new window

Avatar of missymadi
missymadi

ASKER

Thanks! Worked for me.
I need to tell the user at the end of the script that the password has been changed. What is the syntax to do that?

Thanks, Missymadi

We could add in something to tell us about that. Is this what you had in mind?

Chris
# Ask for a username

$Username = Read-Host "Enter UserName"

# Search for the user(s)

$Users = Get-QADUser -SamAccountName $Username | Select-Object Name, DN, 
  PasswordLastSet, PasswordAge,PasswordExpires, PasswordNeverExpires, 
  UserMustChangePassword, PasswordIsExpired, PasswordStatus

# Display the user(s)

$Users

# Ask if they wish to proceed

$Response = Read-Host "Do you want to reset a user's password?`n[1] Yes, [2] No"

# Do the work

If ($Response -eq "1" ) {
  # For each user we found earlier, set the password. Log a few things and the name of the 
  # user running this script 

  $Users | ForEach-Object { 
    Set-QADUser $_.DN -UserPassword 'Test'
    Write-Host "Password reset for $($_.Name)"
  } | Select-Object SamAccountName, PasswordLastSet, @{n='SetBy';e={ $Env:Username }} |
    Out-File c:\PwdChanged.txt -Append
}

Open in new window

It works great.
How would I  prompt the user to enter their own made up AD passwordm instead of letting the script hardcode the password. What is the syntax to accept a new password and save to AD?

Thanks, Missymadi

The password is just clear text here, so you could always have another Read-Host prompt if that's what you're after :)

Unfortunately we can't easily use the -AsSecureString option which gives you *** as you're typing instead of displaying the typed password. If we did, we'd have to use a rather complex command to get the plain text password back out of it.

Chris
# Ask for a username

$Username = Read-Host "Enter UserName"

# Search for the user(s)

$Users = Get-QADUser -SamAccountName $Username | Select-Object Name, DN, 
  PasswordLastSet, PasswordAge,PasswordExpires, PasswordNeverExpires, 
  UserMustChangePassword, PasswordIsExpired, PasswordStatus

# Display the user(s)

$Users

# Ask if they wish to proceed

$Response = Read-Host "Do you want to reset a user's password?`n[1] Yes, [2] No"

# Ask fora  password to use

$Password = Read-Host "Please enter a password to use"

# Do the work

If ($Response -eq "1" ) {
  # For each user we found earlier, set the password. Log a few things and the name of the 
  # user running this script 

  $Users | ForEach-Object { 
    Set-QADUser $_.DN -UserPassword $Password
    Write-Host "Password reset for $($_.Name)"
  } | Select-Object SamAccountName, PasswordLastSet, @{n='SetBy';e={ $Env:Username }} |
    Out-File c:\PwdChanged.txt -Append
}

Open in new window

Great. One more thing I thought of ...what if the password is fat-fingered. How can I force user to type in twice then make sure the two match?

Thanks, Missymadi
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial