PoshCode Archive  Artifact [c03e1a6389]

Artifact c03e1a638991e5d7c9a1ad68ddfc932afd7ed485d34f353b8a5a6b7289f70596:

  • File Set-LocalPassword.ps1 — part of check-in [fc5cdf8fe3] at 2018-06-10 12:56:29 on branch trunk — Get-OUComputerNames (user: Nathan Hartley size: 1504)

# encoding: ascii
# api: powershell
# title: Set-LocalPassword
# description: Get-OUComputerNames
# version: 0.1
# type: function
# author: Nathan Hartley
# license: CC0
# x-poshcode-id: 1124
# x-derived-from-id: 3002
# x-archived: 2016-03-06T12:34:46
# x-published: 2010-05-22T02:00:00
#
# usage   : [computerName1,computerName2,... | ] ./Set-LocalPassword.ps1 [-user] <userName> [-password] <password> [[-computers] computerName1,computerName2,...]
# returns : Sets local account passwords on one or more computers
#
param(
	[switch]$Help
	, [string] $User
	, [string] $Password
    , [string[]] $ComputerNames = @()
)
$usage = @'
Get-OUComputerNames
usage   : [computerName1,computerName2,... | ] ./Set-LocalPassword.ps1 [-user] <userName> [-password] <password> [[-computers] computerName1,computerName2,...]
returns : Sets local account passwords on one or more computers
author  : Nathan Hartley
'@
if ($help) {Write-Host $usage;break}

$ComputerNames += @($input)

if (! $ComputerNames)
{
    $ComputerNames = $env:computername
}

function ChangePassword ([string] $ChangeComputer, [string] $ChangeUser, [string] $ChangePassword) {
	"*** Setting password for $ChangeComputer/$ChangeUser"
	& {
	$ErrorActionPreference="silentlycontinue"
	([ADSI] "WinNT://$computer/$user").SetPassword($password)
	if ($?) { " Success" }
	else { " Failed: $($error[0])" }
	}

}

ForEach ($computer in $ComputerNames) { 
	ChangePassword $computer $user $password 
}