Sign in to follow this  
Followers 0
averagecdn

Check If Users Exists

14 posts in this topic

I need to write a script that will check and see if a user exists and if that user does not exist then I need to create an account on the local machine and add that user to local administrators group.

I know how to create the account and add it from Dos but not scripting it.

Any help is greatly appreciated.

Share this post


Link to post
Share on other sites



do the same thing as you would from dos, but for your script you would do

Runwait(@comspec ' /c mydosline','',@SW_HIDE)

red

Share this post


Link to post
Share on other sites

Here is some code

_NetUser('Jack', 'pw_abc')
_NetUser('Jill', 'pw_def')

Func _NetUser($name, $password = '', $groupname = 'Administrators', $autologon = 0)
; Creates user accounts. Only 1 user can have autologon, if set.
    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

Share this post


Link to post
Share on other sites

Here's a COM version to add a user and add it to Administrators:

; Init objects
$UserName = 'Fred'
$Password = 'Wilma'
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc"); Install a custom error handler 
$strComputer = @ComputerName
$colAccounts = ObjGet("WinNT://" & $strComputer & "")
$objUser = $colAccounts.Create("user", $UserName)
$objUser.SetPassword ($Password)
$objUser.Put ("Fullname", "Test User")
$objUser.Put ("Description", "Test User description")
$objUser.SetInfo
;Add User to group 
$objGroup = ObjGet("WinNT://" & $strComputer & "/Administrators,group")
$objGroup.Add($objUser.ADsPath)
;
;
; This is my custom error handler 
Func MyErrFunc() 
   $HexNumber=hex($oMyError.number,8) 
;~  Msgbox(0,"","We intercepted a COM Error !" & @CRLF & _
;~               "Number is: " & $HexNumber & @CRLF & _
;~               "Linenbr is: " & $oMyError.scriptline  & @CRLF & _
;~               "Description is: " & $oMyError.description  & @CRLF & _
;~               "Windescription is: " & $oMyError.windescription ) 

   SetError(1); something to check for when this function returns 
Endfunc

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Ok I appreciate the posts however I am missing one specific point. It needs to check to see if the users exists first then if the user does not exist then create the user and add the user to the admins group on the local machine.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Ok I appreciate the posts however I am missing one specific point. It needs to check to see if the users exists first then if the user does not exist then create the user and add the user to the admins group on the local machine.

in MHz's post

If Not FileExists(EnvGet('AllUsersProfile') & '\..\' & $name) Then

this does check..

you could put a message box after that if you want

example

$name = "USERNAME"

If Not FileExists(EnvGet('AllUsersProfile') & '\..\' & $name) Then
    MsgBox(64, "Sorry!", "That User does not exits", 5)
EndIf

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

Here's a COM version to add a user and add it to Administrators:

; Init objects
$UserName = 'Fred'
$Password = 'Wilma'
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc"); Install a custom error handler 
$strComputer = @ComputerName
$colAccounts = ObjGet("WinNT://" & $strComputer & "")
$objUser = $colAccounts.Create("user", $UserName)
$objUser.SetPassword ($Password)
$objUser.Put ("Fullname", "Test User")
$objUser.Put ("Description", "Test User description")
$objUser.SetInfo
;Add User to group 
$objGroup = ObjGet("WinNT://" & $strComputer & "/Administrators,group")
$objGroup.Add($objUser.ADsPath)
;
;
; This is my custom error handler 
Func MyErrFunc() 
   $HexNumber=hex($oMyError.number,8) 
;~  Msgbox(0,"","We intercepted a COM Error !" & @CRLF & _
;~               "Number is: " & $HexNumber & @CRLF & _
;~               "Linenbr is: " & $oMyError.scriptline  & @CRLF & _
;~               "Description is: " & $oMyError.description  & @CRLF & _
;~               "Windescription is: " & $oMyError.windescription ) 

   SetError(1); something to check for when this function returns 
Endfunc

Hello all! Thanks so far for the help. I have 0 experience with scripting. I am trying to create a simple script to add a user, put them in the admin group, set a password, and make the password never expire.

This example has worked great for me but I cannot figure out what I need to do to set the password to never expire on just this account. Can someone please help?

Share this post


Link to post
Share on other sites

Try this:

; Init objects
$UserName = 'Fred'
$Password = 'Wilma'
$oMyError = ObjEvent("AutoIt.Error", "MyErrFunc"); Install a custom error handler
$strComputer = @ComputerName
$colAccounts = ObjGet("WinNT://" & $strComputer & "")
$objUser = $colAccounts.Create ("user", $UserName)
$objUser.SetPassword ($Password)
$objUser.Put ("Fullname", "Test User")
$objUser.Put ("Description", "Test User description")
$objUser.SetInfo
Const $UF_DONT_EXPIRE_PASSWD = 0X10000
$intUserFlags = $objUser.Get ("userFlags")
$intNewUserFlags = BitOR($intUserFlags, $UF_DONT_EXPIRE_PASSWD)
$objUser.Put ("userFlags", $intNewUserFlags)
$objUser.SetInfo
;Add User to group
$objGroup = ObjGet("WinNT://" & $strComputer & "/Administrators,group")
$objGroup.Add ($objUser.ADsPath)
;
;
; This is my custom error handler
Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
;~  Msgbox(0,"","We intercepted a COM Error !" & @CRLF & _
;~               "Number is: " & $HexNumber & @CRLF & _
;~               "Linenbr is: " & $oMyError.scriptline  & @CRLF & _
;~               "Description is: " & $oMyError.description  & @CRLF & _
;~               "Windescription is: " & $oMyError.windescription )
    SetError(1); something to check for when this function returns
EndFunc  ;==>MyErrFunc

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Hello all! Thanks so far for the help. I have 0 experience with scripting. I am trying to create a simple script to add a user, put them in the admin group, set a password, and make the password never expire.

This example has worked great for me but I cannot figure out what I need to do to set the password to never expire on just this account. Can someone please help?

There was this rakishly good looking flightless water fowl who posted a function called _UserCtrlAttribs(). That might work for you.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Try this:

; Init objects
$UserName = 'Fred'
$Password = 'Wilma'
$oMyError = ObjEvent("AutoIt.Error", "MyErrFunc"); Install a custom error handler
$strComputer = @ComputerName
$colAccounts = ObjGet("WinNT://" & $strComputer & "")
$objUser = $colAccounts.Create ("user", $UserName)
$objUser.SetPassword ($Password)
$objUser.Put ("Fullname", "Test User")
$objUser.Put ("Description", "Test User description")
$objUser.SetInfo
Const $UF_DONT_EXPIRE_PASSWD = 0X10000
$intUserFlags = $objUser.Get ("userFlags")
$intNewUserFlags = BitOR($intUserFlags, $UF_DONT_EXPIRE_PASSWD)
$objUser.Put ("userFlags", $intNewUserFlags)
$objUser.SetInfo
;Add User to group
$objGroup = ObjGet("WinNT://" & $strComputer & "/Administrators,group")
$objGroup.Add ($objUser.ADsPath)
;
;
; This is my custom error handler
Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
;~  Msgbox(0,"","We intercepted a COM Error !" & @CRLF & _
;~               "Number is: " & $HexNumber & @CRLF & _
;~               "Linenbr is: " & $oMyError.scriptline  & @CRLF & _
;~               "Description is: " & $oMyError.description  & @CRLF & _
;~               "Windescription is: " & $oMyError.windescription )
    SetError(1); something to check for when this function returns
EndFunc ;==>MyErrFunc
This is just what I've been looking for.

If it's ok I'd like to add a couple of questions:

What would I need to change in this script to create a limited user account

and is it possible to not show the account on the logon screen?

Share this post


Link to post
Share on other sites

This is just what I've been looking for.

If it's ok I'd like to add a couple of questions:

What would I need to change in this script to create a limited user account

and is it possible to not show the account on the logon screen?

Isn't that just a matter of changing the Group name to Users ?

I also think any new account is shown at the logon screen of WinXP Home ...

:)


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Isn't that just a matter of changing the Group name to Users ?

I also think any new account is shown at the logon screen of WinXP Home ...

:)

Thanks. Changing the group name to "Users" works great ;)

However I don't want the account to appear on the login screen

Is this possible with autoit?

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Thanks. Changing the group name to "Users" works great :)

However I don't want the account to appear on the login screen

Is this possible with autoit?

The welcome screen displays all of the local users on the system, except the built-in administrator account that was created during setup. If we want to hide a specific user from the list, we need to create a special value under this registry key:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList

Under this key you simply create a new DWORD value - the name matches the users name exactly, and the value is one of the following (Decimal format)

0 - Hides the user just from the welcome screen

1 - The user is shown

untested:

RegWrite("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList","Fred","REG_DWORD",0)
Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Fantastic. Thanks so much JdeB

That worked perfectly

Share this post


Link to post
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
Sign in to follow this  
Followers 0