1+ # requires -version 2
2+ <#
3+ . SYNOPSIS
4+ Replace password of local admin account
5+
6+ . DESCRIPTION
7+ Get local account (user object) with $childObjectSID ends with "-500" (Admin account) then replace password
8+
9+ . INPUTS
10+ <None>
11+
12+ . OUTPUTS
13+ <None>
14+
15+
16+ . NOTES
17+ Version: 0.1
18+ Author: ALBERT Jean-Marc
19+ Creation Date: 29/04/2016 (DD/MM/YYYY)
20+ Purpose/Change: 1.0 - 2016.04.29 - ALBERT Jean-Marc - Initial script development
21+
22+
23+ .SOURCES
24+ <None>
25+
26+
27+ . EXAMPLE
28+ <None>
29+
30+ #>
31+
32+ # ---------------------------------------------------------[Initialisations]--------------------------------------------------------
33+ Set-StrictMode - version Latest
34+
35+ # Set Error Action to Silently Continue
36+ $ErrorActionPreference = " SilentlyContinue"
37+
38+ # ----------------------------------------------------------[Declarations]----------------------------------------------------------
39+
40+ $scriptName = [System.IO.Path ]::GetFileName($scriptFile )
41+ $scriptVersion = " 0.1"
42+
43+ $ComputerName = $env: COMPUTERNAME
44+ $Computer = [ADSI ] " WinNT://$ComputerName ,Computer"
45+ $DecodedPassword = " Str0n6P@ssw0rd!!"
46+
47+ # -----------------------------------------------------------[Execution]------------------------------------------------------------
48+ Write-Host " ======================================================="
49+ Write-Host {}{}{}{}{}{}{}{}{}{}{}{}" $scriptName "
50+ Write-Host " ======================================================="
51+
52+
53+ # Get local admin account name
54+ Write-Progress - Activity " Get local admin account name" - status " Running..." - id 1
55+ foreach ( $childObject in $Computer.Children ) {
56+ # Skip objects that are not users.
57+ if ( $childObject.Class -ne " User" ) {
58+ continue
59+ }
60+ $type = " System.Security.Principal.SecurityIdentifier"
61+ # BEGIN CALLOUT A
62+ $childObjectSID = new-object $type ($childObject.objectSid [0 ], 0 )
63+ # END CALLOUT A
64+ if ( $childObjectSID.Value.EndsWith (" -500" ) ) {
65+ $LocalAdminAccount = $ ($childObject.Name [0 ])
66+ break
67+ }
68+ }
69+
70+ # Show local Admin account
71+ Write-Progress - Activity " Show local Admin account" - status " Running..." - id 1
72+ Write-Host - ForegroundColor Green " Local user account: $LocalAdminAccount "
73+
74+ # Define new password to local Admin account
75+ Write-Progress - Activity " Define new password to local Admin account" - status " Running..." - id 1
76+ $Computer
77+ $User = [adsi ]" WinNT://$ComputerName /$LocalAdminAccount ,user"
78+ $User.SetPassword ($DecodedPassword )
79+ $User.SetInfo ()
80+ Write-Host - ForegroundColor Green " Password changed successfully"
0 commit comments