[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.0

script to join a domain and passing arguments within the script

Asked by resolver1 in VB Script, Windows Management Instrumentation (WMI)

Tags: vbscript, wscript, XP

I've attached the script I'm using to join a domain but I want to integrate this with another script I'm developing.   The problem is the script below has arguments that are passed to it when running it from the command prompt (outside of the vbs file).  I want to be able to pass the arguments from within the script.  Any ideas? I will only be using the script to join a domain.  
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:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
'~~[script]~~
'Script name: NetDomOU.vbs
'Author: Geert Van Camp
'Description: joins,unjoins or removes a Windows XP computer to or from the domain.
'		An organisational unit can be specified when joining a domain!
'Requirements:
'	- only works on Windows XP (and higher)
'	- run the script on the computer that needs to be joined/unjoined/removed
'	- specify a domain user account that has permissions to join/unjoin computers to/from the domain
'	- run with CScript.exe to view output
 
On Error Resume Next
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
Const ACCT_DOMAIN_JOIN_IF_JOINED = 32
 
varExitErrorLevel = 0
 
If WScript.Arguments.Length > 0 Then
	Select Case LCase(WScript.Arguments(0))
		Case "join" strCommand = "Join"
		Case "unjoin" strCommand = "Unjoin"
		Case "remove" strCommand = "Remove"
		Case Else subUsage
	End Select
	For varIndex = 1 To (WScript.Arguments.Length - 1)
		arrArgument = Split(WScript.Arguments(varIndex), ":", -1, vbTextCompare)
		strArgument = arrArgument(0)
		If Ubound(arrArgument) = 0 Then
			Select Case LCase(strArgument)
				Case "/reboot" flgReboot = True
				Case Else subUsage
			End Select
		Else
			strArgumentValue = arrArgument(1)
			Select Case LCase(strArgument)
				Case "/domain" strDomain = strArgumentValue
				Case "/ou" strOU = strArgumentValue
				Case "/user" strUser = strArgumentValue
				Case "/password" strPassword = strArgumentValue
				Case Else subUsage
			End Select
		End If
	Next
Else
	subUsage
End If
 
Set objNetwork = CreateObject("WScript.Network")
strHostName = objNetwork.ComputerName
Set objNetwork = Nothing
 
Set objWMIComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & strHostName & "\root\cimv2:Win32_ComputerSystem.Name='" & strHostName & "'")
If Err = 0 Then
	Select Case strCommand
		Case "Join"
			subDisplay "Joining computer to domain." & vbCrLf & "Hostname: " & strHostName & vbCrLf & "Domain: " & strDomain & vbCrLf & "OU: " & strOU & vbCrLf & "Username: " & strUser
			varWMIJoinReturnValue = objWMIComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strUser, strOU, JOIN_DOMAIN + ACCT_CREATE)
			If Err = 0 Then
				If varWMIJoinReturnValue = 2224 Then
					subDisplay "The computer account already exists."
					If Not strOU = "" Then subDisplay "The computer account will stay in it's current OU."
					varWMIJoinReturnValue = objWMIComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strUser, strOU, JOIN_DOMAIN)
					If Not varWMIJoinReturnValue = 0 Then subDisplay fncErrorMessage(varWMIJoinReturnValue, "", True)
				Else
					If Not varWMIJoinReturnValue = 0 Then subDisplay fncErrorMessage(varWMIJoinReturnValue, "", True)
				End If
			Else
				subDisplay fncErrorMessage(Hex(Err.Number), Err.Description, True)
			End If
		Case "Unjoin", "Remove"
			subDisplay "Unjoining the computer from the domain."
			varWMIJoinReturnValue = objWMIComputer.UnJoinDomainOrWorkGroup( , , 0)
			If Err = 0 Then
				If Not varWMIJoinReturnValue = 0 Then subDisplay fncErrorMessage(varWMIJoinReturnValue, "", True)
			Else
				subDisplay fncErrorMessage(Hex(Err.Number), Err.Description, True)
			End If
			Select Case strCommand
				Case "Remove"
					varExitErrorLevel = 0
					subDisplay "Deleting domain computer account."
					Set ADSISysInfo = CreateObject("ADSystemInfo")
					Set objADSIComputer = GetObject("LDAP://" & ADSISysInfo.ComputerName & "")
					If Err = 0 Then
						objADSIComputer.DeleteObject(0)
						If Not Err = 0 Then
							subDisplay fncErrorMessage(Hex(Err.Number), Err.Description, True)
						End If
					Else
						subDisplay fncErrorMessage(Hex(Err.Number), "Unable to get data from domain controller.", True)
					End If
					Set objADSIComputer = Nothing
					Set ADSISysInfo = Nothing
			End Select
		Case "Else"
			subDisplay fncErrorMessage(99990, "Internal error; unrecognized command.", True)
	End Select
	If varExitErrorLevel = 0 Then
		subDisplay "Finished succesfully." & vbCrLf & "Please reboot to apply changes."
		If flgReboot = True Then
			Set objOperatingSystems = GetObject("winmgmts:{(Shutdown)}//./root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")
			subDisplay "Rebooting..."
			For each objOperatingSystem in objOperatingSystems
				objOperatingSystem.Reboot()
			Next
		End If
	End If
Else
	subDisplay fncErrorMessage(Hex(Err.Number), Err.Description, True)
End If
Set objWMIComputer = Nothing
WScript.Sleep 1000
WSCript.Quit varExitErrorLevel
 
 
Sub subUsage()
	WScript.Echo "Usage: cscript.exe NetDomOU.vbs Join|Unjoin|Remove /Domain:domain [/OU:ou] [/User:user] [/Password:password]" & vbCrLf & _
	vbCrLf & "Join:    Joins the computer to a domain." & vbCrLf & _
	vbCrLf & "Unjoin:  Unjoin the computer from a domain. No other arguments required. The domain computer account will not be deleted!" & vbCrLf & _
	vbCrLf & "Remove:  Unjoin the computer from a domain and delete the domain computer account. No other arguments required. Administrative permissions on the domain are required! " &_
			"(The /user-argument is ignored). Wait for replication to finish before rejoining the computer!" & vbCrLf & _
	vbCrLf & "/Domain: Name of the domain." & vbCrLf & _
	vbCrLf & "/User:   The usersaccount used to execute the command, using the domain\username or username@domain notation! Leave username and password empty to use callers credentials." & vbCrLf & _
	vbCrLf & "/OU:     The full 'distinguished name' of the organisational unit where the new domain computer account will be created when joining a domain. " & _
			"Example /OU:""OU=myOU, DC=domain, DC=com"". The name must be between quotes! Leave empty to add the computer to the default 'Computers'-container. " & vbCrLf & _ 
	vbCrLf & "/Reboot: Reboot the computer if Join/Unjoin/Remove whas succesfull." & vbCrLf
	WScript.Sleep 1000
	WScript.Quit 1
End Sub
 
 
sub subDisplay(strOutput)
	If Instr(1, WScript.FullName, "cscript.exe", vbTextCompare) > 0 Then
		WScript.Echo strOutput & vbCrLf
	End If
End Sub
 
 
Function fncErrorMessage(varErrorNumber, strErrorDescription, flgSetExitErrorLevel)
	If strErrorDescription = "" Then
		'List of 'system error codes' and 'network management error codes'
		Select Case varErrorNumber
			Case 5    strErrorDescription = "Access is denied"
			Case 87   strErrorDescription = "The parameter is incorrect"
			Case 110  strErrorDescription = "The system cannot open the specified object"
			Case 1323 strErrorDescription = "Unable to update the password"
			Case 1326 strErrorDescription = "Logon failure: unknown username or bad password"
			Case 1355 strErrorDescription = "The specified domain either does not exist or could not be contacted"
			Case 2224 strErrorDescription = "The account already exists"
			Case 2691 strErrorDescription = "The machine is already joined to the domain"
			Case 2692 strErrorDescription = "The machine is not currently joined to a domain"
		End Select
	End If
	fncErrorMessage = "Error: " & varErrorNumber & ". " & strErrorDescription & "."
	If flgSetExitErrorLevel Then varExitErrorLevel = 1
End Function
 
'~~[/script]~~
[+][-]08/04/09 01:36 AM, ID: 25011557Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]08/06/09 06:20 PM, ID: 25039358Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: VB Script, Windows Management Instrumentation (WMI)
Tags: vbscript, wscript, XP
Sign Up Now!
Solution Provided By: resolver1
Participating Experts: 1
Solution Grade: A
 
 
Loading Advertisement...
20091021-EE-VQP-81 - Hierarchy / EE_QW_3_20080625