Tự động cấu hình IP theo thời gian bật máy

thu = WeekDay(Date)
gio = Hour(Time)
‘Mac dinh se cau hinh IP tai co quan (phan lon thoi gian ma)
loc = “OFFICE”
strIP = Array(“192.168.0.114”)
strMask = Array(“255.255.255.0”)
strGatewayIP = Array(“192.168.0.253”)

‘Neu la chu nhat, chieu thu 7, hay toi cac ngay khac thi cau hinh ip cho HOME (nghi o nha)
If thu = 1 OR (thu = 7 AND gio > 12) OR (thu < 7 AND gio > 18) Then
    loc = “HOME”
    strIP = Array(“192.168.1.114”)
    strMask = Array(“255.255.255.0”)
    strGatewayIP = Array(“192.168.1.1”)
End if

SetIP

Sub SetIP
    ‘ This code configures an IP address, subnet mask and default gateway
    ‘ for the specified connection.
    ‘ —————————————————————
    ‘ From the book “Windows Server Cookbook” by Robbie Allen
    ‘ Modified by huyphungvan@gmail.com
    ‘ ISBN: 0-596-00633-0
    ‘ —————————————————————

    ‘ —— SCRIPT CONFIGURATION ——
    strComputer = “.”
    strConnection = “Local Area Connection”
    
    ‘ —— END CONFIGURATION ———
    set objWMI = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
    set colNA = objWMI.ExecQuery(“select * ” & _
                                ” from Win32_NetworkAdapter ” & _
                                ” where NetConnectionID = ‘” & strConnection & “‘” )
    for each objNA in colNA
       set colNAConfig = objWMI.ExecQuery _
          (“ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='” & _
            objNA.DeviceID & “‘} ” & _
          ” WHERE resultClass = win32_NetworkAdapterConfiguration “)
       for each objNAConfig in colNAConfig
          intRC = objNAConfig.EnableStatic(strIP,strMask)
          intRC2 = objNAConfig.SetGateways(strGatewayIP)
          if intRC = 0 and intRC2 = 0 then
             Msgbox “IP address configured for ” & strConnection, 0, “Phung Van Huy”
          elseif intRC = 1 or intRC2 = 1 then
             Msgbox “You must reboot for the changes to take effect for ” & _
                          strConnection, 0, “Phung Van Huy”
          else
             WScript.Echo “There was an error configuring IP for ” & _
                          strconnection & “: ” & intRC & ” and ” & intRC2, 0, “Phung Van Huy”
          end if
       next
    next
End Sub