Avatar of janhoedt
janhoedt
 asked on

Powershell: create local admin user gives error "specified network password is wrong"

Hi,

Created a powershell script to create a local admin user but keep on getting
"Exception calling "SetInfo" with "0" argument(s): "The specified network password is not correct."


Please advise what is wrong.
J.


#Create local user and make it administrator
$LocalUser = 'NewLocalAdminUser'
$Computers =   @('servera','serverb')
$Date = "get-date"
$Description = 'Newly created user on $date by $env:username'
$GroupName = 'Administrators'
$Password = 'NewPassword!'
#$Password = Read-Host -AsSecureString
foreach ($computer in $computers)
{
  if ((Get-LocalGroupMember -Computername $computer -Name $GroupName) -contains $LocalUser) {
    Write-Host "$LocalUser already exists, setting new password" -ForegroundColor Green
    Set-LocalUser -Name $LocalUser -Password $(ConvertTo-SecureString -AsPlainText -Force -String $Password) -Computername $computer
  }
  else {
    Write-host "Setting $LocalUser on $computer"
    New-LocalUser -Name $LocalUser -Computername $computer -Password $(ConvertTo-SecureString -AsPlainText -Force -String $Password) -Description $Description
    Add-LocalGroupMember -Computername $computer -GroupName $GroupName -name $LocalUser
    if ((Get-LocalGroupMember -Computername $computer -Name $GroupName) -contains $LocalUser) {Write-Host "$LocalUser succesfully added to $computer" -ForegroundColor Green}
  }  
}
PowershellASP

Avatar of undefined
Last Comment
janhoedt

8/22/2022 - Mon
Big Monty

are you using localhost as the name of the server? if so, change it to the actual server name
janhoedt

ASKER
I have no clue what you mean. Why would localhost play any role in my script?
Big Monty

you have

$Computers =   @('servera','serverb')

i assume you do not have servers named servera, serverb, etc. If any of those are localhost, you will receive the error you're getting
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Joshua Grantom

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
janhoedt

ASKER
that's correct