Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

ADSI code to add a Local user to Local Group doesn't work

$
0
0

Hi friends

it about 8 hours i am working on following simple code but no result. i feel i am loosing my eyes

i need to use a code within my PS script to add a Local user to the built-in "Users" Local Group in windows 7 , 8, 2012....

the following code which the username is not related with any variable works fine.

 

$computer = [ADSI]"WinNT://."
$user = $computer.Create("User","MyLocaluser")
$user.setinfo()
$user.SetPassword("P@ssw0rd")
$Group = [ADSI]"WinNT://./Users,Group"
$Group.Add("WinNT://MyLocaluser,user")

 

but in the 2 following scenarios (which Variables enter into codes), doesn't work: (for simplicity & be easier to read, i have bolded the only differences in my 3 scenarios for you

Scenario1:

$computer = [ADSI]"WinNT://."
$user = $computer.Create("User","MyLocaluser")
$user.setinfo()
$user.SetPassword("P@ssw0rd")
$Group = [ADSI]"WinNT://./Users,Group"
$Group.Add("WinNT://$user,user")

i checked, user is created but is doesn't become member of local "Users" group

 

Scenario2 (which is my Real Scenario):

$myVMnumber = read-host  "enter your VMnumber"

$computer = [ADSI]"WinNT://."
$user = $computer.Create("User","MyLocalUser$MyVMnumber")  ----># for example on VM2, will be created as "MyLocalUser2"
$user.setinfo()
$user.SetPassword("$MyVMnumber")  # ---> so that the password of MyLocaluser be the digit 2
$Group = [ADSI]"WinNT://./Users,Group"
$Group.Add("WinNT://$user,user")

 

 

what change should make to the code?

Many thanks in advanced

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles