Advertisement
Guest User

Untitled

a guest
Feb 1st, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. 'Script Name : CreateUserAccount.vbs
  2. 'Category : Local User Management
  3. 'Creation date : 1 Feb 2018
  4. 'Author : JWM
  5. 'Version : 1.0
  6. 'Execution : cscript.exe CreateUserAccount.vbs "username=[New Username]" "password=[password]"
  7. 'Example : cscript.exe CreateUserAccount.vbs "username=John Doe" "password=abcd"
  8. 'Description : This script creates single user, puts it in the local user group and assigns password from parameter
  9. 'Supported OSes : Windows Vista, Windows 7, Windows XP, Windows Server 2003 (all), Windows Server 2008 (all)
  10. '---------------------------------------------------
  11.  
  12. Option Explicit
  13. 'On Error Resume Next
  14.  
  15. Dim exitcode : exitcode = 0
  16.  
  17. Class ErrorHandlingClass
  18. Private Sub Class_Initialize()
  19. End Sub
  20.  
  21. ' Class_Terminate is called whenever the progam exit including when a Err.Raise is called.
  22. Private Sub Class_Terminate()
  23. exitcode = Err.Number
  24. ' Exit with the appropriate code.
  25. WScript.Quit exitcode
  26.  
  27. End Sub
  28. End Class
  29. Dim Main : Set Main = New ErrorHandlingClass
  30.  
  31. Dim objShNwk,strComputerName,objComputer,objUsr,objUser, objGrp,strFlgs,keys, options, defaults, values(2)
  32. Dim lstArgmnts
  33.  
  34. set lstArgmnts = WScript.Arguments
  35.  
  36. keys = Array("username", "password")
  37. options = Array(0,0)
  38. defaults = Array("default1", "default2")
  39.  
  40. Call extractParameters(lstArgmnts, 2, keys, options, defaults, values)
  41. 'On Error Resume Next
  42.  
  43. 'RegExp to validate password with minimum length of 6 characters
  44. dim strpassword
  45. strpassword = (values(1))
  46. Function CheckString(strpassword, strRegExp)
  47. Dim re
  48. Set re = new RegExp
  49. re.Pattern = strRegExp
  50. CheckString = re.Test(strpassword)
  51. End Function
  52.  
  53. 'Make sure it's in the format of 7 to 99 characters
  54. If Not CheckString(strpassword, "^(?=.*\w).{6,99}$") then
  55. Call Err.Raise(1015, WScript.Scriptname, "Password does not meet the password policy requirements")
  56. End If
  57.  
  58.  
  59. 'Create User '
  60. Set objShNwk = Wscript.CreateObject("WScript.Network")
  61. strComputerName = objShNwk.ComputerName
  62. Set objComputer = GetObject("WinNT://" & strComputerName)
  63. On Error Resume Next
  64. Set objUser = GetObject("WinNT://" & strComputerName & "/" & (values(0))& ", user")
  65. On Error Goto 0
  66. If IsObject(objUser) Then
  67. Call Err.Raise(1014, WScript.Scriptname, "Username: "& (values(0)) &" already exists")
  68. Else
  69. Set objUsr = objComputer.Create("user",(values(0)) )
  70. objUsr.SetPassword (values(1))
  71. objUsr.Setinfo
  72. strFlgs = objUsr.get("UserFlags")
  73. objUsr.Put "UserFlags", strFlgs OR &H10000
  74. objUsr.Fullname = (values(0))
  75. objUsr.Description = "User Created by Script :CreateLocalAdminUserAccount.vbs"
  76. objUsr.Setinfo
  77. ' Add the user to the group
  78. Set objGrp = GetObject("WinNT://" & strComputerName & "/" & "Users")
  79. objGrp.Add(objUsr.ADsPath)
  80.  
  81. '---------------------------------------------------
  82. 'JWM Added:
  83. Set objLocalAdmGroup = GetObject("WinNT://" & strComputerName & "/Administrators,group")
  84. objLocalAdmGroup.Add(objUsr.AdsPath)
  85. '---------------------------------------------------
  86.  
  87. End If
  88. 'End here
  89.  
  90. 'echo output
  91. If exitcode = 0 Then
  92. WScript.Echo "User account " &(values(0))& " has been created successfully."
  93. End If
  94.  
  95.  
  96. Set objShNwk = Nothing
  97. Set objComputer = Nothing
  98. Set objUsr = Nothing
  99. Set objGrp = Nothing
  100. Set lstArgmnts = Nothing
  101.  
  102. 'Sub to extract the command line parameters
  103. Sub extractParameters(ByVal args, ByVal numKeys, ByVal keys, ByVal options, ByVal defaults, ByRef outVals)
  104. Dim i, j, inKeys(2), inVals(2), hasValue, tokenArray(2)
  105. Dim tokens, keyCount, arg, token, index
  106. 'Only 2 arguments allowed
  107. If args.Count > 2 Then
  108. Call Err.Raise(12003, WScript.Scriptname, "Too many arguments")
  109. End If
  110. 'Split arg token at "="
  111. keyCount = 0
  112. For Each arg In args
  113. tokens = Split(arg, "=")
  114.  
  115. ' Convert the list to an array for access purposes
  116. index = 0
  117. For Each token In tokens
  118. tokenArray(index) = token
  119. index = index + 1
  120. Next
  121.  
  122. ' There must be two tokens (key and value) in each parameter
  123. If index <> 2 Then
  124. Call Err.Raise(12001, WScript.Scriptname, "Parameter format error parsing: " & arg)
  125. End If
  126.  
  127. inKeys(keyCount) = tokens(0)
  128. inVals(keycount) = tokens(1)
  129.  
  130. keyCount = keyCount + 1
  131. Next
  132.  
  133. ' Now that we have the keys and values extracted from the parameters, match them to the
  134. ' the expected keys
  135. For i = 0 To (numKeys - 1)
  136. hasValue = 0
  137. outVals(i) = defaults(i)
  138. For j = 0 To keyCount
  139. If keys(i) = inKeys(j) Then
  140. outVals(i) = inVals(j)
  141. hasValue = 1
  142. End If
  143. Next
  144.  
  145. ' If the parameter is not optional, has not been provided and there is no
  146. ' default value then this is an error (exit loop)
  147. If options(i) = 0 Then
  148. If hasValue = 0 Then
  149. Call Err.Raise(12002, WScript.Scriptname, "Missing required parameter: " & keys(i))
  150. Exit For
  151. End If
  152. End If
  153.  
  154. Next
  155.  
  156. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement