Jump to content

Powershell script help needed please for reset of passwords


Recommended Posts

Posted

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?

 

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

 

 

############################################################### 
# 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 = "[email protected]"   

################## 
#--------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) 
}

Posted
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.
  • 2 weeks later...
Posted
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?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...