none
Creating a script to change local user passwords RRS feed

  • Question

  • I have started to work on script to change local user account passwords on many workstations in my environment. I pretty much think i have what I want but... Instead of making the password variable the same password for that local user on all computers, I am trying to make them unique on each computer. I use the following... 

    $password = 'mypasswordprefix' + $(hostname)

    I also tried to move that statement to the .SetPassword method as shown in code and it doesn't work on all (just local where I'm running the script)

    My script works and changes the password on the computer I am currently on just fine with that statement, but doesn't work on subsequent computers in my txt file. If I make the password variable $password = 'newpassword'     it changes on all computers in that txt file just fine. I am probably not utilizing the foreach loop correctly or something. Any help would be appreciated. What i have...

    $computers = Get-Content -path ".\Myfile.txt"
    $password = 'mypasswordprefix'
    foreach ($Computer in $Computers) {
    $Computer = $Computer.toupper()
    $Isonline = "OFFLINE"
    $Status = "SUCCESS"
    $StatsError ="Failed"
    if((Test-Connection -ComputerName $Computer -count 1 -ErrorAction 0)) {
    $Isonline = "ONLINE"
    } else { $StatsError= "`t$Computer is OFFLINE" }
    
    try {
    $account = [ADSI]("WinNT://$Computer/Admin,user")
    $account.SetPassword($password + $(hostname))
    $StatsError="Account Password changed successfully"
    }
    catch {
    $status = "FAILED"
    $StatsError="$_"
    }
    
    $obj = New-Object -TypeName PSObject -Property @{
    ComputerName = $Computer
    IsOnline = $Isonline
    PasswordChangeStatus = $Status
    DetailedStatus=$StatsError
    }
    
    $obj | Select ComputerName, IsOnline, PasswordChangeStatus,DetailedStatus
    $obj | Export-Csv -Append -Path ".\ Myfile.csv"
    }

    • Edited by dshonwood Thursday, March 21, 2019 1:25 PM
    Thursday, March 21, 2019 12:41 PM

Answers

  • Part of the issue is the design of your code.  Also the formatting makes it almost impossible to understand what the code is doing.

    Here is an example of how to correctly design this in PowerShell.

    $password = 'mypasswordprefix'
    Get-Content -path .\Myfile.txt |
        ForEach-Object{
            $obj = [pscustomobject]@{
                ComputerName         = $_
                IsOnline             = $false
                PasswordChangeStatus = $false
                DetailedStatus       = $null
            }
            
            if ((Test-Connection -ComputerName $Computer -quiet)) {
                $obj.IsOnline = $true
                try {
                    $account = [ADSI]("WinNT://$_/Admin,user")
                    $account.SetPassword("$password$_")
                    $obj.PasswordChangeStatus = $true
                    $obj.DetailedStatus = 'Account Password changed successfully'
                } catch {
                    $obj.DetailedStatus = "$_"
                }
            }
    $obj } | Export-Csv .\ Myfile.csv -NoTypeInformation


    \_(ツ)_/


    • Edited by jrv Thursday, March 21, 2019 1:51 PM
    • Marked as answer by dshonwood Thursday, March 21, 2019 4:49 PM
    Thursday, March 21, 2019 1:51 PM

All replies

  • help Set-LocalUser -par Password

    You can also download the localaccounts module from PowerShellGet which allows changing of remote account passwords.

    Please never post images of code.  Edit your post and post the code using the code posting tool provided on the edit bar.

    An image of code is not helpful


    \_(ツ)_/



    • Edited by jrv Thursday, March 21, 2019 1:17 PM
    Thursday, March 21, 2019 1:15 PM
  • I noticed the localaccounts module cmdlets but would't I run into the same scenario? Remember I am not making the password for that local user the same on every workstation. In the end, each computer will have a local account called "myaccount" with a password of "mysetprefix" + hostname of the computer. 

    I apologize for the image. I corrected it.

    Thursday, March 21, 2019 1:29 PM
  • What scenario is it that you think you will run into?


    \_(ツ)_/


    • Edited by jrv Thursday, March 21, 2019 1:42 PM
    Thursday, March 21, 2019 1:40 PM
  • For instance... If I change the .SetPassword to just ($password) it "will" change all workstations in my txt file. But if I try to use the ('password' + $hostname) it only changes the password on my local computer I am running the script from. 

    I am definitely not a pro when it comes to powershell, I just feel like maybe I am not using the foreach loop correctly or i need another array for the password as well?


    • Edited by dshonwood Thursday, March 21, 2019 1:46 PM
    Thursday, March 21, 2019 1:45 PM
  • Part of the issue is the design of your code.  Also the formatting makes it almost impossible to understand what the code is doing.

    Here is an example of how to correctly design this in PowerShell.

    $password = 'mypasswordprefix'
    Get-Content -path .\Myfile.txt |
        ForEach-Object{
            $obj = [pscustomobject]@{
                ComputerName         = $_
                IsOnline             = $false
                PasswordChangeStatus = $false
                DetailedStatus       = $null
            }
            
            if ((Test-Connection -ComputerName $Computer -quiet)) {
                $obj.IsOnline = $true
                try {
                    $account = [ADSI]("WinNT://$_/Admin,user")
                    $account.SetPassword("$password$_")
                    $obj.PasswordChangeStatus = $true
                    $obj.DetailedStatus = 'Account Password changed successfully'
                } catch {
                    $obj.DetailedStatus = "$_"
                }
            }
    $obj } | Export-Csv .\ Myfile.csv -NoTypeInformation


    \_(ツ)_/


    • Edited by jrv Thursday, March 21, 2019 1:51 PM
    • Marked as answer by dshonwood Thursday, March 21, 2019 4:49 PM
    Thursday, March 21, 2019 1:51 PM
  • Like I stated... I am definitely not a pro when it comes to powershell! :)  

    Thank you, I will attempt to "clean it up" from your example. I will reply with the results.

    Thursday, March 21, 2019 1:59 PM
  • Yes... I cleaned up my original code to use better formatting as you suggested above and it seems to work as intended now. 

    Thank you... I have marked it as the answer!

    Thursday, March 21, 2019 4:51 PM