Create local users part of Administrator group on servers part of DMZ

Setup - Mix of Windows 2003, 2008 and 2012

Servers are in workgroup (DMZ)

Need to create user A, user B and User C whereas i will input a text file with the servernames

-----------------------

Found this on other forum

$username = "admin"
$password = "password001"
$computers = Get-Content "C:\Scripts\Create local admin accounts\serverlist.txt"
# OR $computers = Import-CSV c:\serverlist.csv | select Server
Foreach ($computer in $computers) {
   $users = $null
   $computer = [ADSI]WinNT://$computer
   Try {
      $users = $computer.psbase.children | select -expand name  
      if ($users -like $username) {
         Write-Host "$username already exists"
      } Else {
         $user_obj = $computer.Create(user, $username)
         $user_obj.SetPassword($password)
         $user_obj.SetInfo()
         $user_obj.Put(description, $username)
         $user_obj.SetInfo()
         $user_obj.psbase.invokeset(AccountDisabled, False)
         $user_obj.SetInfo()
         $users = $computer.psbase.children | select -expand name
         if ($users -like $username) {
            Write-Host "$username has been created on $($computer.name)"
         } Else {
            Write-Host "$username has not been created on $($computer.name)"
         }
      }
   } Catch {
      Write-Host "Error creating $username on $($computer.path):  $($Error[0].Exception.Message)"
   }
}

Error is  - 

-------------------

PS C:\Scripts\Create local admin accounts> .\createuser.ps1
Error creating admin on WinNT://10.157.33.51:  The following exception occurred while retrieving member "name": "Not imp
lemented
"

How i am running the script

-----------------------------------

Got a client running with Windows 7, i have set the local administrator password same as running on servers in the input file, have logged on locally to windows 7 with administrator account and running powershell with administrator.

June 23rd, 2015 1:18pm

Hi All,

With the above requirement, i got a powershell script working, doing its job but would need suggestions to make it better

UserA, UserB and User C is getting created and added to Administrators group (local) on the server part of workgroup.

Need is to insert computername variable - text file will have approx 20 servers, mix of 2003, 2008 and 2012, could you help?

-----------------------------------------------------

$cn = [ADSI]"WinNT://computername"
$user = $cn.Create("User","userA")
$user.SetPassword("P@ssword1")
$user.setinfo()
$user.description = "Test user"
$user.SetInfo()
$group = [ADSI]"WinNT://computername/administrators,group"
$group.Add("WinNT://computername/userA,user")

$user = $cn.Create("User","userB")
$user.SetPassword("P@ssword1")
$user.setinfo()
$user.description = "Test user"
$user.SetInfo()
$group = [ADSI]"WinNT://computername/administrators,group"
$group.Add("WinNT://computername/userB,user")

$user = $cn.Create("User","userC")
$user.SetPassword("P@ssword1")
$user.setinfo()
$user.description = "Test user"
$user.SetInfo()
$group = [ADSI]"WinNT://computername/administrators,group"
$group.Add("WinNT://computername/userC,user")

Free Windows Admin Tool Kit Click here and download it now
June 23rd, 2015 1:41pm

Look in the repository for many scripts that do what you are trying to do.  Look at how each one is crafted.  You will get many good ideas.

https://gallery.technet.microsoft.com/scriptcenter/site/search?f%5B0%5D.Type=RootCategory&f%5B0%5D.Value=localaccount&f%5B0%5D.Text=Local%20Account%20Management

June 23rd, 2015 1:46pm

Thanks Jrv, my hands are tight with powershell but learning and growing, sometime you need a coach to show you how this will work, may be its you this time, could you help?
Free Windows Admin Tool Kit Click here and download it now
June 23rd, 2015 2:05pm

You need to ask a specific question.
June 23rd, 2015 2:30pm

Hi Inderjit,

If you want to create multiple users on bulk compiters, please refer the script below to start:

$username = "UserA","UserB"
$computers = "vm2","vm3"
Foreach ($computer in $computers) {
$cn = [ADSI]"WinNT://$computer"
foreach($u in $username){
 $user = $cn.Create("User","$u")
 $user.SetPassword("P@ssword!")
 $user.setinfo()
 $user.description = "Test user"
 $user.SetInfo()
 $group = [ADSI]"WinNT://$computer/administrators,group"
 $group.Add("WinNT://$computer/$u,user")
}
}

If there is anything else regarding this issue, please feel free to post back.

Best Regards,

Anna Wang

Free Windows Admin Tool Kit Click here and download it now
June 24th, 2015 2:23am

Thanks Anna, this is what i was looking for, i was not sure how can i add multiple users in one line rather than running the same function for 3 times, appreciate it.
June 24th, 2015 4:25am

I am using Anna's script above, can see the user created but not getting added to the group, error says, any clue...

Exception calling "Add" with "1" argument(s): "A member could not be added to or removed from the local group because the member does not exist.
"
At C:\scripts\Create accounts DMZ\createtemp.ps1:12 char:12
+  $group.Add <<<< ("WinNT://$computer/$u,user")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

Exception calling "Add" with "1" argument(s): "A member could not be added to or removed from the local group because the member does not exist.
"
At C:\scripts\Create accounts DMZ\createtemp.ps1:12 char:12
+  $group.Add <<<< ("WinNT://$computer/$u,user")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

Free Windows Admin Tool Kit Click here and download it now
August 10th, 2015 12:05pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics