脚本保存为 a.vbs- Option Explicit
- On Error Resume Next
-
- Const conINI = "D:\mis.ini" 'ini路径
- Const conNIC = "vEthernet (Default Switch)" '网卡名
- Const ForReading = 1, ForWriting = 2, ForAppending = 8
-
- Dim objWbemServices,fso,tsr,tsw,Element,dicNIC_ININO,mac,str
- Set dicNIC_ININO = CreateObject("Scripting.Dictionary")
- ' MAC地址对应COUNTER_NO
- dicNIC_ININO.Add "00:15:5D:62:F7:80","12863768"
- dicNIC_ININO.Add "00:15:5D:62:F7:90","12863767"
- dicNIC_ININO.Add "00:15:5D:62:F7:72","12863765"
- dicNIC_ININO.Add "00:15:5D:62:F7:42","12863764"
- dicNIC_ININO.Add "00:15:5D:62:F7:32","12863763"
- dicNIC_ININO.Add "00:15:5D:62:F7:02","12863762"
- dicNIC_ININO.Add "00:15:5D:62:F7:12","12863761"
- dicNIC_ININO.Add "00:15:5D:62:F7:96","12863759"
- dicNIC_ININO.Add "00:15:5D:62:F7:98","12863758"
- dicNIC_ININO.Add "00:15:5D:62:F7:66","12863757"
-
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set objWbemServices = GetObject("winmgmts:{ImpersonationLevel=Impersonate}!//./root/cimv2")
- For Each Element In objWbemServices.ExecQuery("Select NetConnectionID,MACAddress From Win32_NetworkAdapter Where NetConnectionID = '" & conNIC & "'")
- mac = Element.MACAddress
- Next
- ' WScript.Echo "mac='" & mac & "'"
- If dicNIC_ININO.Exists(mac) Then
- Set tsr = fso.OpenTextFile(conINI,ForReading,False,vbUseDefault) ' ANSI encoding
- str = tsr.ReadAll
- If Err.Number <> 0 Then MyQuit
- Set tsw = fso.OpenTextFile(conINI,ForWriting,False,vbUseDefault) ' ANSI encoding
- If Err.Number <> 0 Then MyQuit
- With New RegExp
- .Global = False
- .Ignorecase = True
- .Multiline = False
- .Pattern = "(COUNTER_NO\s*=\s*).*"
- tsw.Write(.Replace(str,"$1" & dicNIC_ININO.Item(mac)))
- End With
- End If
-
- MyQuit
-
- Sub MyQuit()
- On Error Resume Next
- If IsObject(tsw) Then
- tsw.Close
- Set tsw = Nothing
- End If
- If IsObject(tsr) Then
- tsr.Close
- Set tsr = Nothing
- End If
- Set fso = Nothing
- set dicNIC_ININO = Nothing
- Set objWbemServices = Nothing
- WScript.Quit
- End Sub
复制代码
|