Net user username password%computername%
#Do-ChangePassword -Filepath ".\computersfile.txt"
[cmdletbinding()]
param(
#parameter to be received as mandatory with the url of the txt file with the name of computers.
[Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true)]$FilePath
)
#this function will check if the computer is online and will check the password after that check.
function Reset-LocalAdminPassword{
[Cmdletbinding()]
param(
[Parameter(position=0,mandatory=$true)]$Computer,
[Parameter(position=0,mandatory=$true)]$pwd
)
BEGIN{
#check if computer is online
#$secureString = $pwd | ConvertTo-SecureString -AsPlainText -Force
$Computer = $Computer.toupper()
$Isonline = "OFFLINE"
$Status = "SUCCESS"
Write-Verbose "Working on $Computer"
if((Test-Connection -ComputerName $Computer -count 1 -ErrorAction 0)) {
$Isonline = "ONLINE"
Write-Verbose "`t$Computer is Online"
}
else { Write-Verbose "`t$Computer is OFFLINE" }
}
PROCESS{
try {
$account = [ADSI]("WinNT://$Computer/Administrator,user")
$account.psbase.invoke("setpassword",$pwd)
Write-Verbose "`tPassword Change completed successfully"
}
catch {
$status = "FAILED"
$error=$_.Exception.Message
write-host "Failed to set the password for the local admin with message: $error".
}
$obj = New-Object -TypeName PSObject -Property @{"ComputerName"= $Computer;"IsOnline"=$Isonline;"PasswordChangeStatus"=$Status}
$obj | Select ComputerName, IsOnline, PasswordChangeStatus
}
END{
if($Status -eq "FAILED" -or $Isonline -eq "OFFLINE") {
$error=$_.Exception.Message
write-host "Failed to set the password for the local admin with message: $error".
}
}
}
#this is the script itself it will read the file from computers and it will be setting up the password variable and set that up into the computer administrator account.
Get-Context $FilePath | %{ $password="password$_"; Reset-LocalAdminPassword -Computer $_ -pwd $password }
Save the file as Do-ChangePassword.ps1Do-ChangePassword.ps1 -Filepath ".\computersfile.txt"
wmic /NODE "PCName" bios get serialnumber
Open in new window