xiaomingtt 发表于 2018-7-9 13:53

VBS修改MAC地址(Win7)

整理网盘,翻出自己大约10年前写的脚本,整理一些比较有意思的,看大家有没有需要。
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

小小/ng 发表于 2018-7-9 14:01

脚本:e:\Users\Administrator\Desktop\mac.vbs
行:3
字符:1
错误:Automation操作中文件名或类名未找到:
objWMIService.ExecQuery'
代码:800A01B0
源:Microsoft VBScript 运行时错误

xiaomingtt 发表于 2018-7-9 15:25

iteamo 发表于 2018-7-9 15:21
现在还好使么?2楼出现的是什么错误

刚刚测试Win7x64专业版和Win7x86旗舰版没有问题

iteamo 发表于 2018-7-9 15:21

现在还好使么?2楼出现的是什么错误

wushaominkk 发表于 2018-7-9 15:29

楼主大牛啊,10年前就会写脚本了

dad9 发表于 2018-7-11 20:01

很好用,刚好解决了我的一个问题

落墨大大 发表于 2018-7-21 16:49


嗯嗯不错,想法很好,思路很清晰,就是一点没看懂

yamisxu 发表于 2018-7-29 12:02

嗯嗯不错,

RedScarf 发表于 2018-7-29 12:19

可以~很溜

流星的孤单 发表于 2018-7-29 16:06

嗯嗯不错,值得学习
页: [1] 2
查看完整版本: VBS修改MAC地址(Win7)