Link to home
Start Free TrialLog in
Avatar of missymadi
missymadi

asked on

Powershell script not running as expected

Experts,

       I created a password reset script but when [1] or [2] is entered the script asks for the user to enter the password. How do I fix this error? It should be if [1] is entered then reset password. [2] then exit out of script.

Thanks, missymadi
Avatar of missymadi
missymadi

ASKER

Here is the code......
# 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 the user's password?`n[1] Yes, [2] No"

# Ask for password 

$Password1 = Read-Host "Please enter a password to use"
$Password2 = Read-Host "Please re-enter the password to confirm"

If ($Password1 -ne $Password2) {

  Write-Host "Passwords do not match. Aborting script." -ForegroundColor Red

} Else {

  # Perform 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 $Password1
      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

Avatar of Chris Dent

Oops, sorry :)
# 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

If ($Users) {

  # Ask if they wish to proceed

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

  If ($Response -eq "1") {

    # Ask for password 

    $Password1 = Read-Host "Please enter a password to use"
    $Password2 = Read-Host "Please re-enter the password to confirm"

    If ($Password1 -ne $Password2) {

      Write-Host "Passwords do not match. Aborting script." -ForegroundColor Red

    } Else {

      # Perform work

      # 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 $Password1
        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

Chris
the paren at the end! I should have caught that!
Any suggestions for a free editor. I'm just using text files to look at the programs. A good editor would have helped me catch this! Also, any tips on good tutorials for Powershell?

Thanks, Missymadi





bracket at end!
OK, I see where you moved the If statement!
I'm getting an error on line 7. Get-QADUser is not recognized as the name of a cmdlet. However, I have already added the snap-in Add-PSSnapin Quest.ActiveRoles.ADManagement. What else could be the problem?
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
Thanks for the information!

Can you help me on another open question?  ID: 26831748 I am trying to encrypt clear passwords.
I'll close this question out. Thanks for your help!

Missymadi