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}
}
}
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}
}
}
are you using localhost as the name of the server? if so, change it to the actual server name
ASKER
I have no clue what you mean. Why would localhost play any role in my script?
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
$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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that's correct