powershell - Creating Local User on Remote Windows Server and Add to Administrator Group -
i have created powershell script create user on remote windows server , add administrator group:
$computer = read-host "computer name:" $username = read-host "user name:" $password = read-host "password" -assecurestring $admingroup = [adsi]"winnt://$computer/administrator,group" $user = [adsi]"winnt://$computer/$username,user" $cred = new-object -typename system.management.automation.pscredential -argumentlist $username, (convertto-securestring $password -asplaintext –force) $user.setpassword($cred.getnetworkcredential().password) $admingroup.add($user.path)
and gives me below error:
following exception occurred while retrieving member "setpassword": " user name not found. @ c:\test1.ps1:7 char:18 + $user.setpassword <<<< ($cred.getnetworkcredential().password) + categoryinfo : notspecified: (:) [], extendedtypesystemexception + fullyqualifiederrorid : catchfrombasegetmember following exception occurred while retrieving member "add": "the specified local group not exist. @ c:\test1.ps1:8 char:16 + $admingroup.add <<<< ($user.path) + categoryinfo : notspecified: (:) [], extendedtypesystemexception + fullyqualifiederrorid : catchfrombasegetmember
if want create user need create user. statement you're using returns user account if exists:
$user = [adsi]"winnt://$computer/$username,user"
probably simplest way create local account net
command:
& net user $username ($cred.getnetworkcredential().password) /expires:never /add
using winnt
provider possible, more complicated:
$acct = [adsi]"winnt://$computer" $user = $acct.create('user', $username) $user.setpassword($cred.getnetworkcredential().password) $user.setinfo()
also, others have pointed out, misspelled name of administrators group (that's what's causing second error). since name of group localized, depending on language version you're running, may want resolve anyway:
$admingroupname = get-wmiobject win32_group -filter "localaccount=true , sid='s-1-5-32-544'" | select-object -expand name $admingroup = [adsi]"winnt://$computer/$admingroupname,group"
Comments
Post a Comment