1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
'---------------------------------------------------------------------
'
' The following script is to be used to create a local
' user account and add it to the local Administrators
' group. To use, change the strLocalUserName to the desired
' name and change strLocalPassword to the password for the account.
'
' This script also will reset the password for the account if
' the account already exists and it will mark it as Hidden
' via a Registry key insertion.
'
' Please note, there is a limitation with the NET USER command
' where you are unable to set an account as 'never expires'.
' The bottom part of the script works arounds this.
'
'---------------------------------------------------------------------
Set objShell = CreateObject ("WScript.Shell")
Set Shell = Nothing
on error resume next
'---------------------------------------------------------------------
' Create local account
'---------------------------------------------------------------------
Wscript.Echo "Managing Administrative User Account"
Set oWshNet = CreateObject("WScript.Network")
strComputer = oWshNet.ComputerName
strLocalUserName = "testuser"
strLocalPassword = "toto"
strGroupname = "Administrateurs"
Wscript.Echo "Variables Defined"
WScript.Sleep(900)
On Error Resume Next
Set objUser = GetObject("WinNT://" & strComputer & "/" & strLocalUserName & ",user")
If Err.Number <> 0 Then
' User account does not exist, create it.
objShell.Run "NET USER "&strLocalUserName&" "&strLocalPassword&" /ADD " _
& "/ACTIVE:YES /COMMENT:""iaatech"" /FULLNAME:" _
& strLocalUserName &" /expires:never", 0, True
Wscript.Echo "User Created"
End If
On Error Resume Next ' Try again
Set objUser = GetObject("WinNT://" & strComputer & "/" & strLocalUserName & ",user")
Wscript.Echo "User Exists"
If Err.Number = 0 Then
'---------------------------------------------------------------------
' Set New Password if user exists
'---------------------------------------------------------------------
objUser.SetPassword strLocalPassword
objUser.SetInfo
Wscript.Echo "Password Updated"
'---------------------------------------------------------------------
' Connect to the group
'---------------------------------------------------------------------
Set objGroup = GetObject("WinNT://" & strComputer & "/" & strGroupname)
'---------------------------------------------------------------------
' Add the user account to the group
' Uses error handling in case it is a member already
'---------------------------------------------------------------------
On Error Resume Next
objGroup.Add(objUser.ADsPath)
WScript.sleep 600
objGroup.Add(objUser.ADsPath)
' Error -2147023518 is "The specified account name is already
' a member of the local group."
Wscript.Echo "Account Added to Administrators Group"
'---------------------------------------------------------------------
' Hide User Account
'---------------------------------------------------------------------
HKEY_LOCAL_MACHINE = &H80000002
Set ObjRegistry = _
GetObject("winmgmts:{impersonationLevel = impersonate}!\\" _
& strComputer & "\root\default:StdRegProv")
strPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"
Return = objRegistry.CreateKey(HKEY_LOCAL_MACHINE, strPath)
Set oReg=GetObject( _
"winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"
oReg.SetDWORDValue _
HKEY_LOCAL_MACHINE,strKeyPath,strLocalUserName,0
Wscript.Echo "Account Hidden"
End If
'---------------------------------------------------------------------
' Set Account password to never expire
' This is done externally due to NET USER limitations
'---------------------------------------------------------------------
Const ufDONT_EXPIRE_PASSWD = &H10000
objUserFlags = objUser.Get("UserFlags")
if (objUserFlags And ufDONT_EXPIRE_PASSWD) = 0 then
objUserFlags = objUserFlags Or ufDONT_EXPIRE_PASSWD
objUser.Put "UserFlags", objUserFlags
objUser.SetInfo
Wscript.Echo "Account Password set to never expire"
end if |
Partager