對照mac address 批次更改電腦名稱

URL Link //n.sfs.tw/14686

2020-07-17 19:40:24 By igogo

 

讀取mac address 對照表, 還原電腦後,自動更改密碼

'show logged on user
Dim CurrentUser
Set objNetwork = CreateObject("Wscript.Network")
CurrentUser=objNetwork.UserName

If CurrentUser <> "Administrator" then
  Wscript.Echo "Administrator is needed. Try command in cmd " &  vbCRLF & "net user administrator /active:yes" 
End IF

'read mac_table 
Dim row,csv_file

row=0
csv_file="c:\mac.csv"
'Wscript.Echo csv_file
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (csv_file, ForReading)
'讀取筆數
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    row=row+1   
Loop

objTextFile.Close

ReDim mac_tables(row-1,1)
Dim j
j=0

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (csv_file, ForReading)

Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , ",")
    'Wscript.Echo "Server name: " & arrServiceList(0)
    mac_tables(j,0) = arrServiceList(0)
    For i = 1 to Ubound(arrServiceList)
        'Wscript.Echo "Service: " & arrServiceList(i)
	mac_tables(j,i) = arrServiceList(i)
    Next
    j=j+1   
Loop

 objTextFile.Close
 
'For j = 0 To UBound(mac_tables) 
'  Wscript.Echo mac_tables(j,0)
'Next  

'Wscript.Echo row

Dim CurrentName
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
For Each objComputer in colSettings 
    'Wscript.Echo "System Name: " & objComputer.Name
    CurrentName=objComputer.Name  
Next

Set colAdapters = objWMIService.ExecQuery _ 
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True") 
For Each objAdapter in colAdapters 
  
  
  For j = 0 To UBound(mac_tables)
         
    If UCase(objAdapter.MACAddress) = UCase(mac_tables(j,0)) then
	'Wscript.Echo objAdapter.MACAddress
	  If UCase(CurrentName) <> UCase(mac_tables(j,1)) then
          'Wscript.Echo CurrentName & "vs" & mac_tables(j,1)
          
          Set objWMIService = GetObject("Winmgmts:root\cimv2")

          ' Call always gets only one Win32_ComputerSystem object.
          For Each objComputer in _
            objWMIService.InstancesOf("Win32_ComputerSystem")
       
       
            Return = objComputer.rename(mac_tables(j,1))
            If Return <> 0 Then
              WScript.Echo "Rename failed. Error = " & Err.Number
            Else
              'WScript.Echo "Rename succeeded." & _
              '" Reboot for new name to go into effect"
			 
			  Const TIMEOUT = 5
			  Set objShell = WScript.CreateObject("WScript.Shell") 
			  objShell.Popup "Rename succeeded. Reboot for new name to go into effect", TIMEOUT
	
             '重啟電腦
             strComputer = "."
             Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
             Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
             
             For Each objOperatingSystem in colOperatingSystems
               ObjOperatingSystem.Reboot()
             Next

            End If

          Next 
 
	  Else
        'WScript.Echo "delete this script."
        Set oFso = CreateObject("Scripting.FileSystemObject") : oFso.DeleteFile Wscript.ScriptFullName, True
        Set oFso = CreateObject("Scripting.FileSystemObject") : oFso.DeleteFile csv_file, True	    
	  End If	 
    End If  
                           
  Next   
 
Next