Advertisement
Guest User

Untitled

a guest
Apr 5th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. ' JoinScript.vbs
  2. '
  3. ' Script to join a computer to a domain.
  4. '
  5. '
  6. '
  7.  
  8. sub Usage
  9. wscript.echo " |------------------------------------------------|"
  10. wscript.echo " | Joins a computer to a domain or workgroup |"
  11. wscript.echo " |------------------------------------------------|"
  12. wscript.echo ""
  13. wscript.echo "Usage: "
  14. wscript.echo " cscript JoinScript.vbs [/domain <domainname> | /workgroup <workgroupname>]"
  15. wscript.echo " [/unjoin] [user <username>] [/password <password>]"
  16. wscript.echo " [/machinepassword <password>] [/readonly] [/createaccount]"
  17. wscript.echo " [/unsecure]"
  18. wscript.echo ""
  19. wscript.echo "domain Specifies the name of a domain to join"
  20. wscript.echo " This option requires user, password"
  21. wscript.echo ""
  22. wscript.echo "workgroup Specifies the name of a workgroup to join"
  23. wscript.echo ""
  24. wscript.echo "unjoin Unjoin from a domain if currently joined."
  25. wscript.echo ""
  26. wscript.echo "disable Disable the account when unjoining the domain."
  27. wscript.echo " This option requires unjoin, user, and password."
  28. wscript.echo ""
  29. wscript.echo "createaccount Specifies to create the computer account in AD"
  30. wscript.echo ""
  31. wscript.echo "machinepassword Specifies a password which is used to"
  32. wscript.echo " authenticate as the machine account to the DC"
  33. wscript.echo ""
  34. wscript.echo "readonly Specifies the domain join will be read only"
  35. wscript.echo " and will not require a writable DC. This option"
  36. wscript.echo " requires machinepassword and that an Administrator"
  37. wscript.echo " has pre-created the computer account and set a"
  38. wscript.echo " password matching the machinepassword parameter."
  39. wscript.echo ""
  40. wscript.echo "DC Specifies a DC to use during domain join."
  41. wscript.echo " If readonly is specified this is mandatory, otherwise optional."
  42. wscript.echo ""
  43. wscript.echo "OU Specifies an OU where the machine account is created, this is optional."
  44. wscript.echo ""
  45. wscript.echo ""
  46. wscript.echo "Unsecure Specifies a an unsecure domain join."
  47. wscript.echo ""
  48. wscript.echo " |------------------------------------------------|"
  49. wscript.echo " |Examples: Run 'cscript JoinScript.vbs <args>' |"
  50. wscript.echo " | <args>: Choose a scenario below |"
  51. wscript.echo " | * Note lines have been wrapped for readability |"
  52. wscript.echo " |------------------------------------------------|"
  53. wscript.echo ""
  54. wscript.echo " Join domain: /domain <domainname> /user <username>"
  55. wscript.echo " /password <password> /createaccount"
  56. wscript.echo ""
  57. wscript.echo " Join domain with existing account: /domain <domainname>"
  58. wscript.echo " /user <username>"
  59. wscript.echo " /password <password>"
  60. wscript.echo ""
  61. wscript.echo " Unjoin from a domain: /unjoin /user <username> /password <password>"
  62. wscript.echo " "
  63. wscript.echo ""
  64. wscript.echo " Read Only join domain: /domain <domainname> /machinepassword <password>"
  65. wscript.echo " /dc <rodcname> /readonly"
  66. wscript.echo ""
  67. wscript.echo " Join workgroup: /workgroup <workgroupname>"
  68. wscript.echo ""
  69. wscript.echo ""
  70. wscript.quit -1
  71. end sub
  72.  
  73.  
  74. '
  75. ' Get the command line arguments
  76. '
  77. Set Args = Wscript.Arguments
  78. 'Set ArgCount = Args.Count
  79.  
  80. ' Validation and Usage
  81. if Args.Count = 0 then
  82. wscript.echo "Help Requested"
  83. wscript.echo ""
  84. Usage
  85. end if
  86.  
  87. if Args.Count > 0 then
  88. if Args(0) = "/?" or Args(0) = "-?" or Args(0) = "help" then
  89. wscript.echo "Help Requested"
  90. wscript.echo ""
  91. Usage
  92. end if
  93. if Args.Count < 1 then
  94. wscript.echo "Help Requested"
  95. wscript.echo ""
  96. Usage
  97. end if
  98. end if
  99.  
  100.  
  101. ' NetJoinDomain flags
  102. Const NETSETUP_JOIN_DOMAIN = 1
  103. Const NETSETUP_ACCT_CREATE = 2
  104. Const NETSETUP_ACCT_DELETE = 4
  105. Const NETSETUP_WIN9X_UPGRADE = 16
  106. Const NETSETUP_DOMAIN_JOIN_IF_JOINED = 32
  107. Const NETSETUP_JOIN_UNSECURE = 64
  108. Const NETSETUP_MACHINE_PWD_PASSED = 128
  109. Const NETSETUP_DEFER_SPN_SET = 256
  110. Const NETSETUP_JOIN_READONLY = 2048
  111. Const NETSETUP_INSTALL_INVOCATION = 262144
  112.  
  113. ' Local state to track limited parameter validation
  114. Options = 0
  115. ReadOnly = 0
  116. Unsecure = 0
  117. JoinWorkgroup = 0
  118. UnjoinDomain = 0
  119. MachinePassword = 0
  120.  
  121. ' Inputs for the join call
  122. strDC = ""
  123. strOU = ""
  124. strDomainName = ""
  125. strDomainNameAndDC = ""
  126. strPassword = ""
  127. strUserName = ""
  128.  
  129. ' Collect parameters
  130. ArgNum = 0
  131.  
  132. do while ArgNum < Args.Count
  133.  
  134. if Args(ArgNum) = "/domain" or Args(ArgNum) = "/Domain" then
  135. strDomainName = Args(ArgNum+1)
  136. Options = Options + NETSETUP_JOIN_DOMAIN
  137. ArgNum = ArgNum + 1
  138. end if
  139.  
  140. if Args(ArgNum) = "/user" or Args(ArgNum) = "/User" then
  141. strUserName = Args(ArgNum+1)
  142. ArgNum = ArgNum + 1
  143. end if
  144.  
  145. if Args(ArgNum) = "/password" or Args(ArgNum) = "/Password" then
  146. strPassword = Args(ArgNum+1)
  147. ArgNum = ArgNum + 1
  148. end if
  149.  
  150. if Args(ArgNum) = "/machinepassword" or Args(ArgNum) = "/MachinePassword" then
  151. strPassword = Args(ArgNum+1)
  152. MachinePassword = 1
  153. Options = Options + NETSETUP_MACHINE_PWD_PASSED
  154. ArgNum = ArgNum + 1
  155. end if
  156.  
  157. if Args(ArgNum) = "/readonly" or Args(ArgNum) = "/ReadOnly" then
  158. Options = Options + NETSETUP_JOIN_READONLY
  159. ReadOnly = 1
  160. end if
  161.  
  162. if Args(ArgNum) = "/unsecure" or Args(ArgNum) = "/Unsecure" then
  163. Options = Options + NETSETUP_JOIN_UNSECURE
  164. Unsecure = 1
  165. end if
  166.  
  167. if Args(ArgNum) = "/workgroup" or Args(ArgNum) = "/WorkGroup" then
  168. JoinWorkgroup = 1
  169. strDomainName = Args(ArgNum+1)
  170. ArgNum = ArgNum + 1
  171. end if
  172.  
  173. if Args(ArgNum) = "/dc" or Args(ArgNum) = "/DC" then
  174. strDC = Args(ArgNum+1)
  175. ArgNum = ArgNum + 1
  176. end if
  177.  
  178. if Args(ArgNum) = "/ou" or Args(ArgNum) = "/OU" then
  179. strOU = Args(ArgNum+1)
  180. ArgNum = ArgNum + 1
  181. end if
  182.  
  183. if Args(ArgNum) = "/unjoin" or Args(ArgNum) = "/Unjoin" then
  184. UnjoinDomain = 1
  185. ArgNum = ArgNum + 1
  186. end if
  187.  
  188. if Args(ArgNum) = "/disable" or Args(ArgNum) = "/disable" then
  189. Disable = 1
  190. Options = Options + NETSETUP_ACCT_DELETE
  191. end if
  192.  
  193. if Args(ArgNum) = "/createaccount" or Args(ArgNum) = "/CreateAccount" then
  194. Options = Options + NETSETUP_ACCT_CREATE
  195. end if
  196.  
  197. ArgNum = ArgNum + 1
  198.  
  199.  
  200. loop
  201.  
  202. ' Error reporting
  203. if ReadOnly = 1 then
  204. if MachinePassword = 0 then
  205. wscript.echo "ReadOnly requires MachinePassword"
  206. wscript.quit(-1)
  207. end if
  208. end if
  209.  
  210. if Disable = 1 and UnjoinDomain = 0 then
  211. wscript.echo "Disable is only valid with the unjoin option"
  212. wscript.quit(-1)
  213. end if
  214.  
  215.  
  216. ' The username is optional and may need to be NULL when passed to the join API below
  217. if strUserName = "" then optionAux = NULL else optionAux = strUserName
  218.  
  219. ' The OU is optional and may need to be NULL when passed to the join API below
  220. if strOU = "" then optionOU = NULL else optionOU = strOU
  221.  
  222. ' Handle the case where this is a domain join and a DC was specified
  223. if strDC = "" then strDomainNameAndDC = strDomainName else strDomainNameAndDC = strDomainName & "\" & strDC
  224.  
  225. wscript.echo strDomainNameAndDC
  226.  
  227. Set objNetwork = CreateObject("WScript.Network")
  228. strComputer = objNetwork.ComputerName
  229.  
  230. Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
  231. 'ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomainName, strPassword, strDomainName & "\" & strUserName, NULL, NETSETUP_JOIN_DOMAIN + NETSETUP_JOIN_READONLY + NETSETUP_MACHINE_PWD_PASSED)
  232.  
  233. ' Perform the join/unjoin operation
  234. if UnjoinDomain = 1 then
  235. ReturnValue = objComputer.UnjoinDomainOrWorkGroup(strPassword, optionAux, Options)
  236. else
  237. ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomainNameAndDC, strPassword, optionAux, optionOU, Options)
  238. end if
  239.  
  240.  
  241. ' Report success messages
  242. if ReturnValue = 0 then
  243. if JoinWorkgroup = 1 then
  244. wscript.echo "Welcome to the workgroup: " & strDomainName
  245. wscript.quit(0)
  246. end if
  247.  
  248. if UnjoinDomain = 1 then
  249. wscript.echo "The machine was unjoined from the domain."
  250. wscript.quit(0)
  251. end if
  252.  
  253. if JoinWorkgroup = 0 then
  254. wscript.echo "Welcome to the domain: " & strDomainName
  255. wscript.quit(0)
  256. end if
  257. else
  258. wscript.echo "Error: " & ReturnValue
  259. end if
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement