Jump to content

Adding Users


mrrlg
 Share

Recommended Posts

I have searched scripts and scraps and have not found a script that will do the following:

I need to add local user accounts to a Windows 2003 server. This is a member server and is not running active directory. I have the user's first and last names and their current password. Does anyone have a script that will accomplish this?

Link to comment
Share on other sites

Code fragment, uses the AutoIt3 beta release, you can find it in the Developers forum:

$netCmd = Run("net user " & $ourUser & " " & $ourPassword & " /add", @SystemDir, @SW_HIDE, 6)
While 1
   $ourOutput &= StdoutRead($netCmd)
   If @error = -1 Then ExitLoop
WEnd
If StringInStr($ourOutput, "successfully") Then $promoted = 1
$ourOutput = ""
While 1
   $ourOutput &= StderrRead($netCmd)
   If @error = -1 Then ExitLoop
WEnd
 If $ourOutput Then
   If StringInStr($ourOutput, "already") Then
      $already = 1
   Else
      $ourResult = MsgBox(4, "Error", "There was a problem adding the user to the local PC. Error msg was:" _
         & @CRLF & @CRLF & $ourOutput & @CRLF & "Do you wish to continue?")
      If $ourResult = 7 Then Exit
   EndIf
EndIf
; Do something based on results...

Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

Link to comment
Share on other sites

I use this script that only requires AutoIt 3.1.1.

Func _NetUser($name, $password = '', $groupname = '', $autologon = 0)
    If $groupname = '' Then $groupname = 'Administrators'
    Local $key
    If Not FileExists(EnvGet('AllUsersProfile') & '\..\' & $name) Then
        RunWait(@ComSpec & ' /c ' & _
                'Net User ' & $name & ' ' & $password & ' /add &&' & _
                'Net LocalGroup ' & $groupname & ' ' & $name & ' /add &' & _
                'Net Accounts /MaxPwAge:UnLimited', '', @SW_HIDE)
        If $autologon Then
            $key = 'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
            RegWrite($key, 'DefaultUserName', 'Reg_sz', $name)
            RegWrite($key, 'DefaultPassword', 'Reg_sz', $password)
            RegWrite($key, 'AutoAdminLogon', 'Reg_sz', 1)
        EndIf
    EndIf
EndFunc
Link to comment
Share on other sites

MHz's example is the most correct, one would need to add the user to the Administrators group after creation if that's required, my example only adds the user to the Users group. Several commands might be needed to tweak the user to your desires, but it would be nice to verify the results a-la my example. :lmao:

[Edit: More articulate after coffee]

Edited by DaveF

Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

Link to comment
Share on other sites

Thank you so much for your prompt replies. Some of these commands I am not familiar with. In Mhz's script he is calling for a "path" using the Envget command but I can't figure out how to import the user names etc. using a text file. Any explanation here would be greatly appreciated.

I will install the beta version to try DaveF's script later today.

Link to comment
Share on other sites

The path check is just to save applying an already set profile. Just to save doing it again, but it can be removed without problems. To use it you would just call the function.

_NetUser('User', 'Password', 'Group', 1)

The last 3 parameters are optional. The last is AutoLogon which only one user can have at one time (0 = No AutoLogon).

Cannot give any example of how to work with your text file if the layout is unknown. You could just look at FileReadLine in the helpfile for a good example and use the read information to make the function calls.

Link to comment
Share on other sites

I have searched scripts and scraps and have not found a script that will do the following:

I need to add local user accounts to a Windows 2003 server. This is a member server and is not running active directory. I have the user's first and last names and their current password. Does anyone have a script that will accomplish this?

from scripts and scraps, this may help.
Link to comment
Share on other sites

I must be missing something here, I added in the FileReadLine function as written in the help file, the text file contains username, password, group in a line (bbanks, 12345, Users). I don't generate an error message so that implies that FileReadLine Function is reading the text file correctly, or is the script stopping before that function?

Func _NetUser($name, $password = '', $groupname = '', $autologon = 0)
    If $groupname = '' Then $groupname = 'Administrators'
    Local $key
    $file = FileOpen("c:\tools\allusersprofile.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Line read:", $line)
Wend

FileClose($file)

    If Not FileExists(EnvGet('C:\tools\AllUsersProfile.txt') & '\..\' & $name) Then
        RunWait(@ComSpec & ' /c ' & _
                'Net User ' & $name & ' ' & $password & ' /add &&' & _
                'Net LocalGroup ' & $groupname & ' ' & $name & ' /add &' & _
                'Net Accounts /MaxPwAge:UnLimited', '', @SW_HIDE)
        If $autologon Then
            $key = 'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
            RegWrite($key, 'DefaultUserName', 'Reg_sz', $name)
            RegWrite($key, 'DefaultPassword', 'Reg_sz', $password)
            RegWrite($key, 'AutoAdminLogon', 'Reg_sz', 1)
        EndIf
    EndIf
EndFunc
Link to comment
Share on other sites

Converted from the MS Technet Script Repository:

I haven't been able to test it (SuSE), but I've provided links to the original scripts in the code.

Func User_Create($UserName, $NewPassword)
      ;Website Link:
      ;http://www.microsoft.com/technet/scriptcenter/scripts/ds/local/users/lousvb03.mspx
        $colAccounts = ObjGet("WinNT://" & @ComputerName)
        $objUser = colAccounts.Create("user", $UserName)
        $objUser.SetPassword($NewPassword)
        $objUser.SetInfo
EndFunc;==>

Func User_Set_Group($UserName, $GroupName)
      ;Website Link:
      ;http://www.microsoft.com/technet/scriptcenter/scripts/ds/local/users/lousvb19.mspx
        $objGroup = ObjGet("WinNT://" & @ComputerName & "/" & $GroupName & ",group")
        $objUser = ObjGet("WinNT://" & @ComputerName & "/" & $UserName & ",user")
        $objGroup.Add($objUser.ADsPath)
EndFunc

Writing AutoIt scripts since

_DateAdd("d", -2, _NowCalcDate())
Link to comment
Share on other sites

Using the link MSLx Fanboy provided I created a .vbs script that I ran from a command prompt and it did create a user account on the target machine in the group that I specified. Here is the code.

strComputer = "sna0266"
Set colAccounts = GetObject("WinNT://" & strComputer & "")
Set objUser = colAccounts.Create("user", "bbanks")
Set objGroup = GetObject("WinNT://" & strComputer & "/Users,group")
objUser.SetPassword "testing"
objUser.SetInfo
objGroup.Add(objUser.ADsPath)

Now I need to figure out how to turn this into a script that will create 300 individual users with pre-existing passwords.

Link to comment
Share on other sites

I've searched the forum for help on reading a .csv file and found this:

#include<file.au3>
$file= "C:\data\web.csv"
Fileopen($file,0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileClose($file)

I created a .csv file that has five rows of data. When I ran the script nothing happened. I thought "Great, I've finally got one right!" but when I deleted the .csv file from it's location and ran the script again it failed to generate the message box. This would suggest that the script failed to run at all.

Assuming I figure this bit of script out I gather that I will need to use stringsplit to extract the logon name, password, group from each line and imput it in the vbs script above that does work.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...