Hi SSE@TUE,
Thanks for sharing the updated script and the error message. That AccessDenied, PSSessionStateBroken error actually tells us exactly what is stopping the connection.
When using Invoke-Command, Windows Remote Management (WinRM) strictly requires the connecting user to have administrative privileges on the target machines. To get past this, please double-check these two things:
- Authentication: Make sure you are launching your PowerShell console as an Administrator (right-click -> Run as Administrator). More importantly, the account you are logged in with must be a member of the Local Administrators group on those 100 client machines (typically, a Domain Admin account). If you need to use a specific, different admin account to connect to those clients, you will need to add the
-Credentialparameter to yourInvoke-Command. - Clean up the leftover code: Looking closely at your updated script, I noticed you left the old
[void]$account.psbase.Invoke('setpassword', $newPassword)line right below the newInvoke-Commandblock. Since we are no longer querying the$accountvariable, that line will definitely throw an error and needs to be deleted.
To make sure everything is perfectly aligned, your Try block should look exactly like this (with the old lines completely removed):
Try {
$newPassword = Get-RandomPassword -Length 12
Invoke-Command -ComputerName $computer -ScriptBlock {
param($pwd)
$securePwd = ConvertTo-SecureString $pwd -AsPlainText -Force
Set-LocalUser -Name "Administrator" -Password $securePwd
} -ArgumentList $newPassword
$result.Status = 'OK'
$result.NewCredentials = $newPassword
}
Give that a try from an elevated PowerShell prompt with Domain Admin rights, and let me know how it goes!
Tracy.