Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
Const ACCT_DELETE = 4
Const WIN9X_UPGRADE = 16
Const DOMAIN_JOIN_IF_JOINED = 32
Const JOIN_UNSECURE = 64
Const MACHINE_PASSWORD_PASSED = 128
Const DEFERRED_SPN_SET = 256
Const INSTALL_INVOCATION = 262144
Sub vbSelfDestruct()
' Function: Make a script delete itself
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
objFSO.DeleteFile WScript.ScriptFullName
wscript.quit
End Sub
sysReboot=True
strDomain = "**"
strUser="**"
strPassword="**"
compDestOU="ou=**,ou=**,DC=**,dc=co,dc=uk"
' get current machine name
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
' get Dell Service Tag from machine
on error resume next
Set objWMIservice = GetObject ("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
set colitems = objWMIservice.ExecQuery("Select * from Win32_BIOS",,48)
For each objitem in colitems
' Wscript.echo "Dell Service Tag: " & objitem.serialnumber
DellServiceTag = objitem.serialnumber
Next
' if theres an error getting the service tag inform the user and delete the script
If DellServiceTag="" Then
MsgBox "Error Retrieving Dell Service Tag."
vbSelfDestruct()
End If
' check if domain, username and password are already set and decide how to ask the question
If strDomain<>"" and strUser<>"" and strPassword<>"" then
MsgBoxQuestion="Do you want to add this machine as " & DellServiceTag & " to domain " & strDomain & " now?"
else
MsgBoxQuestion="Do you want to add this to a domain named using its Dell Service Tag - " & DellServiceTag & "?"
end if
MsgBoxQuestion=MsgBoxQuestion & vbCrLf & vbCrLf & "Computer account for this machine will be created under:"
MsgBoxQuestion=MsgBoxQuestion & vbCrLf & compDestOU
' ask user if they want to add the machine to a/the domain
AddToDomainQuestion=Msgbox(MsgBoxQuestion,vbYesNo, "Add Computer to Domain")
' if user selects no, delete vbscript and exit vbscript
If AddToDomainQuestion=vbNo Then
vbSelfDestruct()
end if
' inform user that the machine name is already the service tag
If strComputer = DellServiceTag Then
MsgBox "This machines name is already renamed to the Dell Service Tag of " & DellServiceTag & "."
End If
' find if machine is already joined to a domain
Set col2Computer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in col2Computer
joinedtodomain=objComputer.PartOfDomain
domainorworkgroup=objComputer.Domain
Next
if joinedtodomain=True then
MsgBox "Machine is already joined to domain " & domainorworkgroup & "."
vbSelfDestruct()
end if
newComputerName = DellServiceTag
' domain login credentials
if len(strUser)<1 then
strUser = InputBox("Enter Username to join domain " & strDomain,"Username for join domain " & strDomain)
if strUser="" then
wscript.quit
end if
end if
if len(strPassword)<1 then
strPassword =InputBox("Enter Password for username " & strUser & " to join domain " & strDomain,"Password for user " & strUser & " to join domain " & strDomain)
if strPassword="" then
wscript.quit
end if
end if
' setup objComputer variable and add to domain under OU - Workstations
Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strDomain & "\" & strUser, compDestOU, JOIN_DOMAIN + ACCT_CREATE)
If ReturnValue = 0 Then
'MsgBox "Computer added to domain " & strDomain & " successfully."
Else
MsgBox "Computer not added to domain successfully. Return value: " & ReturnValue
End If
wscript.sleep(5000)
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colComputers = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each objComputer in colComputers
ErrCode = objComputer.Rename(newComputerName,strPassword,strUser)
If ErrCode = 0 Then
'MsgBox "Computer renamed successfully to " & newComputerName & "."
If sysReboot = True Then
Set objOperatingSystems = GetObject("winmgmts:{(Shutdown)}//./root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")
For each objOperatingSystem in objOperatingSystems
objOperatingSystem.Reboot()
Next
End If
vbSelfDestruct()
Else
If ErrCode = "2691" Then
MsgBox "Machine is already joined to domain " & strDomain & "."
ElseIf ErrCode = "2224" Then
MsgBox "The computer account already exists."
Else
MsgBox "Error changing computer name. Error code: " & ErrCode
End if
End If
Next