[Visual Basic] 纯文本查看 复制代码
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=true", , 48)
For Each objItem in colItems
msg = msg & "编号:" & objItem.Index & vbCrLf & "MAC:" & objItem.MACAddress & vbCrLf & "网卡:" & objItem.Description & vbCrLf & vbCrLf
ix = ix & objItem.Index & ","
MAC = MAC & objItem.Index & "``" & objItem.MACAddress & "$$$"
Next
MAC = left(MAC,len(MAC)-3)
If msg = "" Then
MsgBox "未找到活动网卡,单击确定退出程序",64,"MAC修改程序"
wscript.Quit
End If
createobject("scripting.filesystemobject").createtextfile("MAC-backup.txt").write Replace(MAC,"$$$",vbcrlf)
Do
idx = InputBox( msg , "请输入您要修改MAC的网卡编号", Left(ix,InStr(ix,",") - 1))
if idx = False Then Wscript.Quit
Loop Until IsNumeric(idx) And InStr(ix,idx)
s = Split(MAC,"$$$")
for i = 0 to ubound(s)
z = split(s(i),"``")
if z(0) = idx then MAC=z(1)
next
Do
MAC = InputBox( "输入您指定的MAC地址值(注意应该是12位的连续数字或字母(A~F),其间没有-、:等分隔符)" , "输入新的MAC地址", MAC)
if MAC = False Then Wscript.Quit
MAC = Replace(Replace(Replace(MAC, ":", ""), "-", ""), " ", "")
loop until rt("^[\da-fA-F]{12}$",MAC)
idx = Right("00000" & idx, 4)
reg = "HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\" & idx
Set WSHShell = CreateObject("WScript.Shell")
WshShell.RegWrite reg & "\NetworkAddress", MAC , "REG_SZ"
WshShell.RegWrite reg & "\Ndi\params\NetworkAddress\default" , MAC , "REG_SZ"
WshShell.RegWrite reg & "\Ndi\params\NetworkAddress\ParamDesc" , "NetworkAddress" , "REG_SZ"
WshShell.RegWrite reg & "\Ndi\params\NetworkAddress\optional" , "1" , "REG_SZ"
NetWorkName = WshShell.RegRead("HKLM\SYSTEM\ControlSet001\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\" & WshShell.RegRead(reg & "\NetCfgInstanceId") & "\Connection\Name")
WSHShell.Popup "程序将重启您的网卡,请稍后...",2,"MAC修改程序",64
WSHShell.Run("cmd /c netsh interface set interface " & NetWorkName & " disabled"),0
wscript.sleep 1000 * 5
WSHShell.Run("cmd /c netsh interface set interface " & NetWorkName & " enabled"),0
wscript.sleep 1000 * 2
If Not Err Then WSHShell.Popup "修改成功!" & vbcrlf & vbcrlf & "如果出现异常,请打开设备管理器,展开网络适配器," & vbcrlf & "在相应的网卡上右击,选择属性" & _
"切换到高级选项卡," & vbcrlf & "将属性NetworkAddress值修改为不存在." & vbcrlf & "(不同网卡略有不同,请据实际情况修改)",8,"MAC地址修改程序",64 _
Else WSHShell.Popup "修改失败",3,"MAC地址修改程序",64
Function rt(patrn,str)
Set re = New Regexp
re.Pattern = patrn
re.IgnoreCase = True
re.Global = True
rt = re.Test(str)
End Function