[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Powershell script not running as expected

Posted on 2011-02-18
8
Medium Priority
?
486 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:missymadi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 

Author Comment

by:missymadi
ID: 34927455
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

0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 34927522

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
0
 

Author Comment

by:missymadi
ID: 34927627
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





0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:missymadi
ID: 34927644
bracket at end!
0
 

Author Comment

by:missymadi
ID: 34927752
OK, I see where you moved the If statement!
0
 

Author Comment

by:missymadi
ID: 34928021
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?
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 2000 total points
ID: 34928817

Hmm nothing more than that really. How are you executing this?

I'm not so good at editors, I tend to use Notepad++ and paste into the console or execute scripts. Some people have luck with PowerGUI, others are happy with PowerShell ISE (the editor that comes with PS 2). There are a number of other editors kicking around, including PrimalScript (Sapien).

Links :)

PowerGUI: http://www.powergui.org/downloads.jspa
PrimalScript: http://www.primalscript.com/

Chris
0
 

Author Comment

by:missymadi
ID: 34928912
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question