• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1601
  • Last Modified:

adding pc to domain using vbscript

hey,

i'm making a vbscript that sets the pc name, add the pc to the domain and add the user that will be using the pc. the problem is when i execute the script no errors occure, under system properties => computer name you will see that the domain is filled in, when starting up you also get the domain list but the pc doesn't show up in AD.

now when i run the scripts each differently not combined, the joining to the domain works

any ideas why not combined??
Dim sPCname
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "\\" & strComputer & "\root\cimv2")
 
 
sPCname = GeneratePCname()
WScript.Echo"pc naam:" & sPCname 
 
Dim bRenamed
 
bRenamed = RenamePc(sPCname)
WScript.Echo"succefully renamed:" & brenamed 
 
 
' rename a PC 
Function RenamePc(pcname)
    RenamePc = False
    
    Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
    
    For Each objComputer in colComputers
    	
    	Err = objComputer.Rename(pcname)
    	If Err <> 0 Then 
    		RenamePc = False
    	Else 
    		RenamePc = True
    	End If
    	WScript.Echo "Error: " & err
	Next
	
End Function
 
 
' Generate a new PC name
'	+Check if name already exists (ping)
Function GeneratePCname()
	Dim sMessage
	Dim sPCnameEnd, sCountryCode, sPCtype, sPC, sPCkoppel
	Dim sOfficeDigit
 
	GeneratePCname = ""
	sPCtype = ""
	sPC = "XP"
	sPCkoppel = "-"
	sPCnameEnd = ""
	sCountryCode = ""
	sOfficeDigit = 0
	strComputer = "."
 
	sMessage = "Please provide the 2 letter Country Code" & _
	vbnewline & "(ISO 3166-1 alpha2)" & _
	vbnewline & "http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Current_codes"
 
	While (GeneratePCname = "")
		While ((Len(sCountryCode) <> 2) Or (IsNumeric(sCountryCode)))
		 	sCountryCode = InputBox(sMessage,"2 digit country code")
		 	If LCase(sCountryCode) = "quit" Then WScript.Quit
		Wend   
		
		sMessage = "Please provide the 1 digit " & _
		vbnewline & "Country Office Number starting from '1'" & _
		vbNewLine & "The main country office = '1', " & _
		vbnewline & "for each extra office in the same" & _
		vbnewline & "country the number increments with 1" & _
		vbnewline & "" & _
		vbnewline & "example:" & _
		vbnewline & "" & _
		vbnewline & "US Boston office = '1'" & _
		vbnewline & "US Lebanon office = '2'" & _
		vbnewline & "US Detroid office = '3'"	
		While ((sOfficeDigit < 1) or (sOfficeDigit > 9))
		 	sOfficeDigit = InputBox(sMessage,"1 digit office nr",1)
		 	If LCase(sOfficeDigit) = "quit" Then WScript.Quit
			If Not IsNumeric(sOfficeDigit) Then sOfficeDigit = 0
		Wend
 
	sMessage = "Please provide beginning letter of type of pc" & _
	vbnewline & "D = Desktop" & _
	vbnewline & "L = Laptop" & _
	vbnewline & "T = Tablet" & _
	vbnewline & "P = ProcessPC" & _
	vbnewline & "V = Virtuals"
	
	sPCtype = InputBox(sMessage,"type of pc")
	
	sMessage = "Please provide the TA number"
	sPCnameEnd = InputBox(sMessage,"TA number")
 
		GeneratePCname = UCase(sCountryCode & sOfficeDigit & sPC & sPCtype & sPCkoppel & sPCnameEnd)
wend
End Function 
 
 
' Join a Computer to a Domain
 
' Windows Server 2003 : Yes
' Windows XP : Yes
' Windows 2000 : No
' Windows NT 4.0 : No
' Windows 98 : No
 
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
 
strDomain = "TA.global"
strPassword = InputBox("password","password")
strUser = InputBox("username", "username")
 
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
 
Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
    strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
        strComputer & "'")
 
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
    strPassword,  "ta\" & strUser, NULL, _
        JOIN_DOMAIN + ACCT_CREATE)
        
Dim StrComputer, input, name
GetTheName()
 
 Function GetTheName()
 name = InputBox("Input your name","name",input)
 If InStr(name,"quit") Then WScript.Quit() Else 
 Set ws = WScript.CreateObject ( "WScript.Shell" )
 Set ObjNetwork = CreateObject ("Wscript.Network")
 StrComputer= objnetwork.ComputerName
 Set adGrp = GetObject ( "WinNT://" & StrComputer & "/Administrators,group" )
 adGrp.Add ( "WinNT://TA/" & name & ",group" )
 End Function

Open in new window

0
ferrar99
Asked:
ferrar99
  • 2
1 Solution
 
rejoinderCommented:
Try changing the order of execution so that the computer is added to the domain first, then it is renamed.  I know this is backwards to the login you are thinking right now but this will work.
The script will take the machine name as is and add it to the domain, then the computer will get renamed and rebooted.  Once the machine is back online, you should see it appear in AD under the Computers container with the new name.

I have attached a sample of what I mean below.

My response is based on the following post;
http://www.tek-tips.com/viewthread.cfm?qid=1240726&page=1

'Join the computer to the domain
JoinComputer
 
'Get a new computer name
sPCname = GeneratePCname()
WScript.Echo"pc name:" & sPCname 
 
'Rename the computer
bRenamed = RenamePC(sPCname)
WScript.Echo"succefully renamed:" & bRenamed
 
'Add user to workstation admin
GetTheName()
 
Sub JoinComputer
	' Join a Computer to a Domain
 	
	' Windows Server 2003 : Yes
	' Windows XP : Yes
	' Windows 2000 : No
	' Windows NT 4.0 : No
	' Windows 98 : No
	 
	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
	 
	strDomain   = "TA.global"
	strPassword = InputBox("password","password")
	strUser     = InputBox("username", "username")
	 
	Set objNetwork = CreateObject("WScript.Network")
	strComputer    = objNetwork.ComputerName
	 
	Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
	    strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
	        strComputer & "'")
	 
	ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, "ta\" & strUser, NULL, JOIN_DOMAIN + ACCT_CREATE)
End Sub
 
Function RenamePC(pcname)
    RenamePC = False
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" & "\\" & strComputer & "\root\cimv2")
    Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
    For Each objComputer in colComputers
    	Err = objComputer.Rename(pcname)
    	If Err <> 0 Then 
    		RenamePC = False
    	Else 
    		RenamePC = True
    	End If
    	WScript.Echo "Error: " & err
    Next
End Function
 
' Generate a new PC name
'	+Check if name already exists (ping)
Function GeneratePCname()
	Dim sMessage
	Dim sPCnameEnd, sCountryCode, sPCtype, sPC, sPCkoppel
	Dim sOfficeDigit
 
	GeneratePCname = ""
	sPCtype = ""
	sPC = "XP"
	sPCkoppel = "-"
	sPCnameEnd = ""
	sCountryCode = ""
	sOfficeDigit = 0
	strComputer = "."
 
	sMessage = "Please provide the 2 letter Country Code" & _
	vbnewline & "(ISO 3166-1 alpha2)" & _
	vbnewline & "http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Current_codes"
 
	While (GeneratePCname = "")
		While ((Len(sCountryCode) <> 2) Or (IsNumeric(sCountryCode)))
		 	sCountryCode = InputBox(sMessage,"2 digit country code")
		 	If LCase(sCountryCode) = "quit" Then WScript.Quit
		Wend   
		
		sMessage = "Please provide the 1 digit " & _
		vbnewline & "Country Office Number starting from '1'" & _
		vbNewLine & "The main country office = '1', " & _
		vbnewline & "for each extra office in the same" & _
		vbnewline & "country the number increments with 1" & _
		vbnewline & "" & _
		vbnewline & "example:" & _
		vbnewline & "" & _
		vbnewline & "US Boston office = '1'" & _
		vbnewline & "US Lebanon office = '2'" & _
		vbnewline & "US Detroit office = '3'"	
		While ((sOfficeDigit < 1) or (sOfficeDigit > 9))
		 	sOfficeDigit = InputBox(sMessage,"1 digit office nr",1)
		 	If LCase(sOfficeDigit) = "quit" Then WScript.Quit
			If Not IsNumeric(sOfficeDigit) Then sOfficeDigit = 0
		Wend
 
	sMessage = "Please provide beginning letter of type of pc" & _
	vbnewline & "D = Desktop" & _
	vbnewline & "L = Laptop" & _
	vbnewline & "T = Tablet" & _
	vbnewline & "P = ProcessPC" & _
	vbnewline & "V = Virtuals"
	
	sPCtype = InputBox(sMessage,"type of pc")
	
	sMessage = "Please provide the TA number"
	sPCnameEnd = InputBox(sMessage,"TA number")
 
		GeneratePCname = UCase(sCountryCode & sOfficeDigit & sPC & sPCtype & sPCkoppel & sPCnameEnd)
	wend
End Function
 
Function GetTheName()
	name = InputBox("Input your name","name",input)
	If InStr(name,"quit") Then
		WScript.Quit()
	Else 
		Set ObjNetwork = CreateObject ("Wscript.Network")
		strComputer = objnetwork.ComputerName
		Set adGrp = GetObject ( "WinNT://" & strComputer & "/Administrators,group" )
		adGrp.Add ( "WinNT://TA/" & name & ",group" )
	end if
End Function

Open in new window

0
 
ferrar99Author Commented:
i tried the script above, but when executing the vbscript i get an error 1326 for changing the pc name
any ideas why??
0
 
rejoinderCommented:
1326 might be a permissions issue with renaming the computer.

This script passed the username and password from the previous step to help change the computer name.
Dim strPassword
Dim strUser
 
'Join the computer to the domain
JoinComputer
 
'Get a new computer name
sPCname = GeneratePCname()
WScript.Echo"pc name:" & sPCname 
 
'Rename the computer
bRenamed = RenamePC(sPCname)
WScript.Echo"succefully renamed:" & bRenamed
 
'Add user to workstation admin
GetTheName()
 
Sub JoinComputer
	' Join a Computer to a Domain
 	
	' Windows Server 2003 : Yes
	' Windows XP : Yes
	' Windows 2000 : No
	' Windows NT 4.0 : No
	' Windows 98 : No
	 
	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
	 
	strDomain   = "TA.global"
	strPassword = InputBox("password","password")
	strUser     = InputBox("username", "username")
	 
	Set objNetwork = CreateObject("WScript.Network")
	strComputer    = objNetwork.ComputerName
	 
	Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
	    strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
	        strComputer & "'")
	 
	ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, "ta\" & strUser, NULL, JOIN_DOMAIN + ACCT_CREATE)
End Sub
 
Function RenamePC(pcname)
    RenamePC = False
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" & "\\" & strComputer & "\root\cimv2")
    Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
    For Each objComputer in colComputers
    	Err = objComputer.Rename(pcname,strPassword,"ta\" & strUser)
    	If Err <> 0 Then 
    		RenamePC = False
    	Else 
    		RenamePC = True
    	End If
    	WScript.Echo "Error: " & err
    Next
End Function
 
' Generate a new PC name
'	+Check if name already exists (ping)
Function GeneratePCname()
	Dim sMessage
	Dim sPCnameEnd, sCountryCode, sPCtype, sPC, sPCkoppel
	Dim sOfficeDigit
 
	GeneratePCname = ""
	sPCtype = ""
	sPC = "XP"
	sPCkoppel = "-"
	sPCnameEnd = ""
	sCountryCode = ""
	sOfficeDigit = 0
	strComputer = "."
 
	sMessage = "Please provide the 2 letter Country Code" & _
	vbnewline & "(ISO 3166-1 alpha2)" & _
	vbnewline & "http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Current_codes"
 
	While (GeneratePCname = "")
		While ((Len(sCountryCode) <> 2) Or (IsNumeric(sCountryCode)))
		 	sCountryCode = InputBox(sMessage,"2 digit country code")
		 	If LCase(sCountryCode) = "quit" Then WScript.Quit
		Wend   
		
		sMessage = "Please provide the 1 digit " & _
		vbnewline & "Country Office Number starting from '1'" & _
		vbNewLine & "The main country office = '1', " & _
		vbnewline & "for each extra office in the same" & _
		vbnewline & "country the number increments with 1" & _
		vbnewline & "" & _
		vbnewline & "example:" & _
		vbnewline & "" & _
		vbnewline & "US Boston office = '1'" & _
		vbnewline & "US Lebanon office = '2'" & _
		vbnewline & "US Detroit office = '3'"	
		While ((sOfficeDigit < 1) or (sOfficeDigit > 9))
		 	sOfficeDigit = InputBox(sMessage,"1 digit office nr",1)
		 	If LCase(sOfficeDigit) = "quit" Then WScript.Quit
			If Not IsNumeric(sOfficeDigit) Then sOfficeDigit = 0
		Wend
 
	sMessage = "Please provide beginning letter of type of pc" & _
	vbnewline & "D = Desktop" & _
	vbnewline & "L = Laptop" & _
	vbnewline & "T = Tablet" & _
	vbnewline & "P = ProcessPC" & _
	vbnewline & "V = Virtuals"
	
	sPCtype = InputBox(sMessage,"type of pc")
	
	sMessage = "Please provide the TA number"
	sPCnameEnd = InputBox(sMessage,"TA number")
 
		GeneratePCname = UCase(sCountryCode & sOfficeDigit & sPC & sPCtype & sPCkoppel & sPCnameEnd)
	wend
End Function
 
Function GetTheName()
	name = InputBox("Input your name","name",input)
	If InStr(name,"quit") Then
		WScript.Quit()
	Else 
		Set ObjNetwork = CreateObject ("Wscript.Network")
		strComputer = objnetwork.ComputerName
		Set adGrp = GetObject ( "WinNT://" & strComputer & "/Administrators,group" )
		adGrp.Add ( "WinNT://TA/" & name & ",group" )
	end if
End Function

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now