Thanks for your input. ive used your advice and changed it just to log onto the domain. the code is below, ta.
' joins the target pc to the domain
Function joinDomain( strDomain, strOU, strUser, strPassword, flgReboot)
Dim varExitErrorLevel
On Error Resume Next
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
Const ACCT_DOMAIN_JOIN_IF_JOINED = 32
varExitErrorLevel = 0
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
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
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
joinDomain = varExitErrorLevel
End Function
sub subDisplay(strOutput)
If Instr(1, WScript.FullName, "cscript.exe", vbTextCompare) > 0 Then
WScript.Echo strOutput & vbCrLf
End If
End Sub
'displays errors for joining the domain.
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
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:





by: Alan_WhitePosted on 2009-08-04 at 01:36:15ID: 25011557
I've not tested this so it might not work straight out of the box but will at least point you in the right direction.
Select allOpen in new window