+ Post New Thread
Results 1 to 3 of 3
Coding Thread, Powershell script help needed please for reset of passwords in Coding and Web Development; Hi, I've been looking for a script that will reset a password for a user account and email it to ...
  1. #1
    PEO
    PEO is offline
    PEO's Avatar
    Join Date
    Oct 2007
    Posts
    2,093
    Thank Post
    457
    Thanked 150 Times in 95 Posts
    Rep Power
    71

    Powershell script help needed please for reset of passwords

    Hi, I've been looking for a script that will reset a password for a user account and email it to our admin office. I've found this powershell script but I have to problems with it. I want it to have the useraccounts that are to be changed in the script as this will be ran every 12 hours so eg 12jblog1 12jblog2 without having to manually enter the user name withis is what this script does. next problem is I had a run though of the script and I get the following error?

    Code:
    The term 'Get-RandomString.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At C:\Users\bgadmin2\Downloads\AD_Password_reset_v1.0.ps1:39 char:33
    + $password = Get-RandomString.ps1 <<<<  -Length 13 -LowerCase:$TRUE -Numbers:$TRUE -UpperCase:$TRUE -Symbols:$TRUE -Count 2
        + CategoryInfo          : ObjectNotFound: (Get-RandomString.ps1:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
    Any ideas? The default script unchanged is below

    cheers


    Code:
    ############################################################### 
    # ADPassword_reset_v1.0.ps1 
    # Version 1.0 
    # MALEK Ahmed - 04 / 03 / 2013 
    ################### 
     
    ################## 
    #--------Config 
    ################## 
    $adPath="LDAP://DC=contoso,DC=msft" 
    $domaindnsname = "contoso.msft" 
    $domainnetbiosname = "CONTOSO" 
    $smtpserver = "mail.contoso.msft" 
    $noreplymail = "no-reply@contoso.msft"   
     
    ################## 
    #--------Function  
    ################## 
    Function Set-AdUserPwd  
    {  
    Param(  
    [string]$user,  
    [string]$pwd  
    ) #end param  
    $oUser = [adsi]"LDAP://$user"  
    $ouser.psbase.invoke("SetPassword",$pwd)  
    $ouser.psbase.CommitChanges()  
    } # end function Set-AdUserPwd 
         
    ################## 
    #--------Main   
    ################## 
    cls 
    #Prompting the user to enter the variables 
    #Determine if the user is external or not 
    $usernameAD = ([Environment]::UserDomainName).ToString() + "\" + ([Environment]::UserName).ToString() 
    #Reads the User samaccountname 
    $samaccountname = Read-Host 'What is the user samaccountname?' 
    #Define the User Password 
    $password = .\Get-RandomString.ps1 -Length 8 -LowerCase:$FALSE -Numbers:$TRUE -UpperCase:$TRUE 
    #Doing the password reset 
    import-module activedirectory 
    #Identify the creator e-mail address 
    $User = get-aduser ([Environment]::UserName).ToString() -Properties Mail 
    $accountcreator = $User.mail 
    #LDAP connection 
    $objDomain=New-Object System.DirectoryServices.DirectoryEntry($adPath) 
    #Doing an LDAP search 
    $ObjSearch=New-Object System.DirectoryServices.DirectorySearcher($ObjDomain) 
    $ObjSearch.PageSize = 60000 
    #Filtering user accounts based on their samaccountname 
    $ObjSearch.Filter = "(&(objectCategory=person)(objectClass=user)(samaccountname="+$samaccountname+"))" 
    $allSearchResult = $ObjSearch.FindAll() 
    if ($allSearchResult.Count -eq 0) 
    { 
        $errormessagesamaccountname = "The is no such login in " + $domaindnsname + " domain, please try another one!" 
        $samaccountname = Read-Host $errormessagesamaccountname 
        $ObjSearch.Filter = "(&(objectCategory=person)(objectClass=user)(samaccountname="+$samaccountname+"))" 
        $allSearchResult = $ObjSearch.FindAll() 
        while ($allSearchResult.Count -eq 0) 
        { 
            $samaccountname = Read-Host $errormessagesamaccountname 
            $ObjSearch.Filter = "(&(objectCategory=person)(objectClass=user)(samaccountname="+$samaccountname+"))" 
            $allSearchResult = $ObjSearch.FindAll() 
        } 
        foreach ($objSearchResult in $allSearchResult) 
        { 
            $objUser=New-Object System.DirectoryServices.DirectoryEntry($objSearchResult.Path) 
            $test1 = $Error.Count 
            Set-AdUserPwd -user $objUser.distinguishedname -pwd $password 
            $test2 = $Error.Count 
            if($test1 -eq $test2) 
            { 
                $passwordreset = $true 
            } 
            else 
            { 
                $passwordreset = $false 
            } 
        } 
    } 
    else 
    { 
        foreach ($objSearchResult in $allSearchResult) 
        { 
            $objUser=New-Object System.DirectoryServices.DirectoryEntry($objSearchResult.Path) 
            $test1 = $Error.Count 
            Set-AdUserPwd -user $objUser.distinguishedname -pwd $password 
            $test2 = $Error.Count 
            if($test1 -eq $test2) 
            { 
                $passwordreset = $true 
            } 
            else 
            { 
                $passwordreset = $false 
            } 
        }   
    } 
     
    #Notification 
    $wshShell =  new-object –comobject “wscript.shell” 
    if ($passwordreset) 
    { 
        $wshShell.Popup("The password reset for " + $domainnetbiosname + "\" + $samaccountname + " was done successfully. The new password is " + $password + "", 0, "[SUCCESS] The password reset for " + $domainnetbiosname + "\" + $samaccountname + " was done successfully", 48); 
        $mailbody = "AD Login = " + $domainnetbiosname + "\" + $samaccountname + "`r`n`r`n" 
        $mailbody += "Password = " + $password + "`r`n`r`n" 
        $mailbody += "The password reset was done by " + $usernameAD 
        #Notification about the script execution 
        $msg = new-object Net.Mail.MailMessage 
        $smtp = new-object Net.Mail.SmtpClient($smtpServer) 
        $msg.From = $noreplymail 
        $msg.To.Add($accountcreator) 
        $msg.Subject = "[SUCCESS] The password reset for " + $domainnetbiosname + "\" + $samaccountname + " was done successfully" 
        $msg.Body = $mailbody 
        $smtp.Send($msg)     
    } 
    else 
    { 
        $wshShell.Popup("The password reset for " + $domainnetbiosname + "\" + $samaccountname + " was not done successfully", 0, "[ERROR] The password reset for " + $domainnetbiosname + "\" + $samaccountname + " was not done successfully", 48); 
        $mailbody = "AD Login = " + $domainnetbiosname + "\" + $samaccountname + "`r`n`r`n" 
        $mailbody += "`r`n`r`n" 
        $mailbody += "The error occured when doing the password reset will be checked by the Centralized Infrastructure Team!`r`n`r`n" 
        $mailbody += "Debugging [Centralized Infra]: samaccountname: "+ $samaccountname + "`r`n`r`n" 
        $mailbody += "The password reset was done by " + $usernameAD 
        #Notification about the script execution 
        $msg = new-object Net.Mail.MailMessage 
        $smtp = new-object Net.Mail.SmtpClient($smtpServer) 
        $msg.From = $noreplymail 
        $msg.To.Add($accountcreator) 
        $msg.Subject =  "[ERROR] The password reset for " + $domainnetbiosname + "\" + $samaccountname + " was not done successfully" 
        $msg.Body = $mailbody 
        $smtp.Send($msg) 
    }

  2. #2
    ascott2's Avatar
    Join Date
    Nov 2007
    Posts
    181
    Thank Post
    18
    Thanked 37 Times in 29 Posts
    Rep Power
    20
    It references another script called Get-RandomString.ps1, which it can't find. It seems to expect it to be in the same folder as the main script.

  3. #3
    Mark-Vital's Avatar
    Join Date
    Oct 2010
    Location
    York
    Posts
    7
    Thank Post
    0
    Thanked 3 Times in 3 Posts
    Rep Power
    0
    As ascott2 has said, the script is trying to look for the Get-RandomString.ps1 file and cannot find it. Look at like 39 where you set the $password variable to "./Get-Randomstring.ps1". It may just been the syntax of the way the path is defined, my knowledge is of C# and not so much with powershell but that's what i'd take a guess at. Where is the Get-RandomString.ps1 script located?

SHARE:
+ Post New Thread

Similar Threads

  1. Powershell help needed please!
    By RabbieBurns in forum Scripts
    Replies: 2
    Last Post: 13th May 2010, 09:12 AM
  2. [Ubuntu] Webcam Driver help needed please
    By RabbieBurns in forum *nix
    Replies: 4
    Last Post: 1st February 2010, 10:15 PM
  3. AD Scripting Help needed
    By Grommit in forum Wireless Networks
    Replies: 2
    Last Post: 22nd October 2008, 08:05 PM
  4. batch to bash rewrite help needed please
    By RabbieBurns in forum Scripts
    Replies: 4
    Last Post: 22nd September 2008, 09:05 AM
  5. more excel help needed please
    By RabbieBurns in forum Office Software
    Replies: 10
    Last Post: 6th September 2008, 11:53 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •