VB script บนระบบปฏิบัติการ Windows XP เพื่อเปลี่ยน computer name ให้เป็นไปตามเลข MAC address ของ LAN card

สามารถประยุกต์ติดตั้งให้ทำงานอัตโนมัติทุกครั้งที่บูทเครื่อง โดยนำไปเก็บไว้ในพื้นที่ที่ startup สั่งทำงานได้

ปัญหาโคลนนิง PC ที่ใช้ XP จะเจอว่า computer name ซ้ำกัน  admin ต้องมา rename computer name ทุกเครื่อง หากใช้ script นี้ ก็ไม่ต้องยุ่งยากไป rename อีก เพราะจะได้ตามเลข MAC address ของ LAN card ที่ใช้เลย สามารถใช้ได้ทุก PC ทั่วโลก เพราะ MAC address ของ LAN card จะไม่มีซ้ำกัน ยกเว้นเจอ LAN card ที่เสียเท่านั้น

แฟ้มเก็บไว้ที่ http://gotoknow.org/file/wipatwipat/RenameComputerMAC.vbs

หรือ  ftp://ftp.psu.ac.th/pub/tutor/RenameComputerMAC.vbs

ตัวอย่างการใช้งานจริง คือให้ copy แฟ้ม RenameComputerMAC.vbs ไปเก็บไว้ใน drive C:\

แล้วตั้งเวลาทำงานโดยคลิก start->All Programs->Accessories->System Tools->Scheduled Tasks

สั่ง Add Scheduled Task  เลือกโปรแกรม C:\RenameComputerMAC.vbs ให้ทำงานที่บูทเครื่อง When my computer starts

ใส่ username และ password ที่ทำงานระดับ administrator ให้เรียบร้อย

ผลคือทุกครั้งที่บูทเครื่องระบบจะเปลี่ยนชื่อโดยอัตโนมัติ

--วิภัทร

รายละเอียด source code ต้นฉบับ

'GetIPaddr.vbs - Check the IP address you are currently
'connected to the Internet with (or LAN IP).
'LabAdmin - Computer Center
' rev-2 15 May 2007 by KX
' rev 25 April 2007
'   Now works with Windows NT, 2K, XP

Option Explicit
Dim newName
newName = "_" + GetLastIPNo+"_"
RenameComputer(newName)

Function RenameComputer(Name)
    Dim strComputer
        Dim objWMIService, colComputers, objComputer
        strComputer = "."
        Set objWMIService = GetObject("winmgmts:" _
                & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

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

        For Each objComputer in colComputers
                err = objComputer.Rename(Name)
        Next
End Function

Function GetLastIPNo()
  Dim ws : Set ws = CreateObject("WScript.Shell")
  Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
  'Dim TmpFile : TmpFile = fso.GetSpecialFolder(2) & "\ip.txt"
  Dim TmpFile : TmpFile = "ip.txt"
  Dim ThisLine, IP, IPArr
  If ws.Environment("SYSTEM")("OS") = "" Then
    ws.run "winipcfg /batch " & TmpFile, 0, True
  Else
    ws.run "%comspec% /c ipconfig /all > " & TmpFile, 0, True
  End If
  With fso.GetFile(TmpFile).OpenAsTextStream
    Do While NOT .AtEndOfStream
      ThisLine = .ReadLine
      If InStr(ThisLine, "Physical Address") <> 0 Then IP = Mid(ThisLine, InStr(ThisLine, ":") + 2)
    Loop
    .Close
  End With
  'WinXP (NT? 2K?) leaves a carriage return at the end of line
  If IP <> "" Then
    If Asc(Right(IP, 1)) = 13 Then IP = Left(IP, Len(IP) - 1)
  End If

  IPArr = Split(IP,"-")
  GetLastIPNo = IPArr(0)+IPArr(1)+IPArr(2)+IPArr(3)+IPArr(4)+IPArr(5)

  fso.GetFile(TmpFile).Delete
  Set fso = Nothing
  Set ws = Nothing
End Function