Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScrip to automate computer name change from csv

Status
Not open for further replies.

nik90

Technical User
Nov 22, 2010
2
US
After I image a windows 7 pc i have a vbs scrip run that asks for the computer name and then joins the computer to the domain. I would like for the script to look at a csv file and try to match the computer mac address to the mac address in the csv file, if it finds a match it will name the computer and then add to the domain, if it does not find a match it will then prompt for a computer name to be entered. The part I have no idea how to do is get the script to search the local file compare mac address, name computer or if no match found then prompt for a name. any help would be appricated. the code I use now is below.

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
Dim message
Dim IP
strDomain = "my domain"
strUser = "my user name"
strPassword = "my password"

Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName

Set objWMIService = GetObject ("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")

title = "Join computer to domain"
message = "Please enter computer name. Leave blank or press cancel to quit." & vbCrLf & vbCrLf & "Generated name: " & generatedName
newComputerName = InputBox(message, title)


If newComputerName = "" Then
Wscript.quit
End If

areYousure = MsgBox("Are you sure you want t0 add computer to domain with name:" & vbCrLf & vbCrLf & newComputerName, vbYesNo + vbQuestion,"Add computer to domain")

If areYouSure = "7" Then
MsgBox "Exiting script.",vbInformation
Wscript.quit
End If

Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
strComputer & "'")

ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strDomain & "\" & strUser, "ou=xxx,my domain", _
JOIN_DOMAIN + ACCT_CREATE)

If ReturnValue = 0 Then
MsgBox "Computer added to domain under old name without error. proceeding to change computer name. "
Else
MsgBox "Computer not added to domain successfully. Return value: " & ReturnValue
End If

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputers = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")


For Each objComputer in colComputers
MsgBox "About to rename computer to: " & newComputername
ErrCode = objComputer.Rename(newComputerName, strPassword, strUser)
If ErrCode = 0 Then
MsgBox "Computer renamed correctly."
Else
MsgBox "Eror changing computer name. Error code: " & ErrCode
End If

Next
 
you could read a csv file like?

Set objTS = FSO.OpenTextFile("c:\my.csv", 1, True)
Do While Not objTS.AtEndOfStream
strLine = ""
strLine = objTS.ReadLine
Wscript.Echo strLine
Loop
objTS.Close
Set objTS = Nothing

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
I will work on it this upcomming weekend, how would I get the mac address of the computer so I could campare it to the csv file for a match to automactily enter in the name box?
 
how would I get the mac address of the computer
A starting point:
Code:
...
   Set colItems = objWMIService.ExecQuery("SELECT MACAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True")
   For Each objItem In colItems
      MsgBox "MACAddress: " & objItem.MACAddress
   Next
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top