
Const         DATABASE_SERVER = "10.0.0.101"
Const           DATABASE_NAME = "WSNAME"
Const         DATABASE_USERID = "<youruserid>"
Const         DATABASE_PASSWD = "<yourpassword>"
Const          DATABASE_TABLE = "MACAddresses"
Const      BASE_COMPUTER_NAME = "WKS-"
Const          PATH_TO_WSNAME = "C:\WSNAME.EXE"

Dim WSHShell   : Set WshShell = CreateObject("WScript.Shell")
Dim WshNetwork : Set WshNetwork = CreateObject("WScript.Network")

Dim aIPConfig, iNICIndex, sMACAddress, sCurrentComputerName, sNewComputerName

aIPConfig = GetIPConfiguration
iNICIndex = GetActiveNIC(aIPConfig)
sMACAddress = aIPConfig(2,iNICIndex)
sCurrentComputerName = GetComputerName

sNewComputerName = ReadFromSQL(sMACAddress,"ID") 
If sNewComputerName = "" Then 	' Computer is not in database - adding"
	AddToSQL sMACAddress
	sNewComputerName = ReadFromSQL(sMACAddress,"ID") 
End If

If sNewComputerName <> "" Then
	sNewComputerName = BASE_COMPUTER_NAME & sNewComputerName
	If UCase(sNewComputerName) <> UCase(sCurrentComputerName) Then
		WSHShell.Run PATH_TO_WSNAME & " /N:" & sNewComputerName,1,True
	End if
Else
	WScript.Echo "An Error has occured"
End If

' ----------------------------------------------------------------------------

Function ReadFromSQL(sMACAddress,sFieldName)
	Dim oConn, sConnectionString, rs
	On Error Resume Next
	Set rs = Wscript.CreateObject("ADODB.Recordset")
	Set oConn = Wscript.CreateObject("ADODB.Connection")
	sConnectionString = "Provider=SQLOLEDB;Data Source=" & DATABASE_SERVER & ";Initial Catalog="& DATABASE_NAME & ";User ID=" & DATABASE_USERID & ";Password="& DATABASE_PASSWD & ";"
	oConn.Open sConnectionString
   	set rs = oConn.Execute("SELECT * FROM " & DATABASE_TABLE & " WHERE [MAC] = '" & sMACAddress & "'")
	if (rs.EOF And rs.BOF) Then    
		ReadFromSQL = ""
	Else
		ReadFromSQL = Trim(rs.fields(sFieldName).value)
	End If   	
	oConn.Close
	Set rs = Nothing 
	Set oConn = Nothing
	On Error Goto 0
End Function

' ----------------------------------------------------------------------------

Function AddToSQL(sMACAddress)
	Dim oConn, sConnectionString, rs
	On Error Resume Next
	Set rs = Wscript.CreateObject("ADODB.Recordset")
	Set oConn = Wscript.CreateObject("ADODB.Connection")
	sConnectionString = "Provider=SQLOLEDB;Data Source=" & DATABASE_SERVER & ";Initial Catalog="& DATABASE_NAME & ";User ID=" & DATABASE_USERID & ";Password="& DATABASE_PASSWD & ";"
	oConn.Open sConnectionString
   	set rs = oConn.Execute("SELECT * FROM " & DATABASE_TABLE & " WHERE [MAC] = '" & sMACAddress & "'")
	if (rs.EOF And rs.BOF) Then
		oConn.Execute("INSERT INTO " & DATABASE_TABLE & " ([MAC]) VALUES ('" & sMACAddress & "');")  
 	End If
	oConn.Close
	Set rs = Nothing 
	Set oConn = Nothing
	AddToSQL = 0
	On Error Goto 0
End Function

' ------------------------------------------------------------------------ 

Function GetIPConfiguration
	Dim objWMIService, objSet, obj, i
	On Error Resume Next
	Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
	Set objSet = objWMIService.ExecQuery("select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE",,48)
	ReDim aResult(4,-1)
	for each obj in objSet
		if Not IsNull(obj.IPAddress) then 
			for i=LBound(obj.IPAddress) to UBound(obj.IPAddress)
				if obj.IPAddress(i) <> "0.0.0.0" Then
					ReDim Preserve aResult(UBound(aResult,1),UBound(aResult,2) + 1)
    				aResult(0,UBound(aResult,2)) = obj.IPAddress(i)
    				aResult(1,UBound(aResult,2)) = obj.IPSubnet(i)
    				aResult(2,UBound(aResult,2)) = Replace(obj.MACAddress,":","-")
    				aResult(3,UBound(aResult,2)) = Replace(obj.Description,":","")
    				aResult(4,UBound(aResult,2)) = obj.DNSDomain
				End if
			next
		end If
	Next
	On Error Goto 0
    GetIPConfiguration=aResult
End Function

' ------------------------------------------------------------------------ 

Function GetActiveNIC(aNICData)
	Dim iI, sIPAddress
	For iI = LBound(aNICData,2) To UBound(aNICData,2)
		GetActiveNIC  = iI
		sIPAddress = aNICData(0,iI)
		If (sIPAddress <> "") And (sIPAddress <> "0.0.0.0") And (Left(sIPAddress,3) <> "169") Then : Exit For
	Next
End Function

' ------------------------------------------------------------------------ 

Function GetComputerName
   Dim sTEMP                        ' I suspect this function sometimes  
   sTEMP=wshNetwork.ComputerName    ' retuns a null value if the network is
   if sTEMP="" then                 ' unsettled, the expandenv is to catch this  
       sTEMP=WSHShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
   end if 
   GetComputerName=sTEMP
End Function

' ------------------------------------------------------------------------  

