Advertisement
qiwichupa

Static IP to DHCP for all interfaces (XP, Win 2003, Win 7)

Jul 27th, 2011
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '----------------------------------------------------------------------------------------------------------------------------
  2. 'Script Name : EnableDHCP.vbs
  3. 'Author   : Matthew Beattie
  4. 'Created   : 29/12/10
  5. 'Description : This script enables DHCP for systems on specified subnets.
  6. '----------------------------------------------------------------------------------------------------------------------------
  7. 'Initialization Section  
  8. '----------------------------------------------------------------------------------------------------------------------------
  9. Option Explicit  
  10. Dim objFSO, wshShell, wshNetwork, hostName
  11. On Error Resume Next
  12.  Set objFSO   = CreateObject("Scripting.FileSystemObject")  
  13.  Set wshNetwork = Wscript.CreateObject("Wscript.Network")  
  14.  Set wshShell  = CreateObject("Wscript.Shell")  
  15.  hostName    = wshNetwork.ComputerName
  16.  ProcessScript
  17.  If Err.Number <> 0 Then
  18.  Wscript.Quit  
  19.  End If
  20. On Error Goto 0
  21. '----------------------------------------------------------------------------------------------------------------------------
  22. 'Name    : ProcessScript -> Primary Function that controls all other script processing.  
  23. 'Parameters : None     ->  
  24. 'Return   : None     ->  
  25. '----------------------------------------------------------------------------------------------------------------------------
  26. Function ProcessScript
  27.  Dim ipAddress, macAddress, product, systems, system
  28.  Dim subnets, subnet, network, continue
  29.  product = ReadRegistry("HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProductName")
  30.  systems = Array("Windows XP","Windows 7")
  31.  subnets = Array("192.168.10.0", "192.168.20.0","192.168.30.0","192.168.60.0","192.168.70.0","192.168.71.0","192.168.90.0", "192.168.91.0")
  32.  '-------------------------------------------------------------------------------------------------------------------------
  33. 'Ensure this script only applies to client operating systems. (Don't configure servers with DHCP!)
  34. '-------------------------------------------------------------------------------------------------------------------------
  35. Continue = False
  36.  For Each system In systems
  37.  If InStr(1, product, system, vbTextCompare) <> 0 Then
  38.  continue = True
  39.  Exit For
  40.  End If
  41.  Next
  42.  If Not continue Then
  43.  Exit Function
  44.  End If
  45.  '-------------------------------------------------------------------------------------------------------------------------
  46. 'Enumerate the IP and MAC Addresses from the local system.
  47. '-------------------------------------------------------------------------------------------------------------------------
  48. If Not GetIPConfig(hostName, ipAddress, macAddress) Then
  49.  Exit Function
  50.  End If
  51.  '-------------------------------------------------------------------------------------------------------------------------
  52. 'Ensure the script continues processing only if the systems IP address is within the specified subnets.
  53. '-------------------------------------------------------------------------------------------------------------------------
  54. network = Left(ipAddress, InStrRev(ipAddress, ".")) & "0"
  55.  continue = False
  56.  For Each subnet In subnets
  57.  If StrComp(subnet, network, vbTextCompare) = 0 Then
  58.  continue = True
  59.  Exit For
  60.  End If
  61.  Next
  62.  '-------------------------------------------------------------------------------------------------------------------------
  63. 'Ensure the script only continues processing if the systems IP address is within the specified subnets.
  64. '-------------------------------------------------------------------------------------------------------------------------
  65. If Not continue Then
  66.  Exit Function
  67.  End If
  68.  '-------------------------------------------------------------------------------------------------------------------------
  69. 'The system is within one of the specified subnets. Enable DHCP on all enabled interfaces that have static IP Addresses.
  70. '-------------------------------------------------------------------------------------------------------------------------
  71. If Not EnableDHCP Then
  72.  Exit Function
  73.  End If
  74. End Function
  75. '----------------------------------------------------------------------------------------------------------------------------
  76. 'Name    : ReadRegistry -> Read the value of a registry key or value.
  77. 'Parameters : key     -> Name of the key (ending in "\") or value to read.
  78. 'Return   : ReadRegistry -> Value of key or value read from the local registry (blank is not found).
  79. '----------------------------------------------------------------------------------------------------------------------------
  80. Function ReadRegistry(ByVal key)
  81.  Dim result
  82.  If StrComp (Left (key, 4), "HKU\", vbTextCompare) = 0 Then
  83.  Key = "HKEY_USERS" & Mid (key, 4)
  84.  End If
  85.  On Error Resume Next
  86.  ReadRegistry = WshShell.RegRead (key)
  87.  If Err.Number <> 0 Then
  88.  ReadRegistry = ""
  89.  End If
  90.  On Error Goto 0
  91. End Function
  92. '----------------------------------------------------------------------------------------------------------------------------
  93. 'Name    : GetIPConfig -> Enumerates the IP & MAC Address of the system via WMI
  94. 'Parameters : hostName  -> String containing the hostname of the computer to enumerate the IP configuration for.
  95. '      : ipAddress  -> Input/Output : Variable assigned to the IP Address of the system.
  96. 'Parameters : macAddress -> Input/Output : Variable assigned to the MAC Address of the system.
  97. 'Return   : GetIPConfig -> Returns True and the systems IP & MAC Address if successful otherwise returns False.
  98. '----------------------------------------------------------------------------------------------------------------------------
  99. Function GetIPConfig(hostName, ipAddress, macAddress)
  100.  Dim wmi, ipConfig, query
  101.  GetIPConfig = False
  102.  ipAddress  = ""
  103.  macAddress = ""
  104.  query    = "select * from Win32_NetworkAdapterConfiguration where IPEnabled = True"
  105.  On Error Resume Next
  106.  Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & hostName & "\root\cimv2")
  107.  If Err.Number <> 0 Then
  108.  Exit Function
  109.  End If
  110.  For Each ipConfig in wmi.ExecQuery(query)
  111.  If Err.Number <> 0 Then
  112.  Exit Function
  113.  End If
  114.  ipAddress = ipConfig.IPAddress(0)
  115.  macAddress = ipConfig.MACAddress(0)
  116.  If ipAddress <> "" And ipAddress <> "0.0.0.0" And MACAddress <> "" Then
  117.  Exit For
  118.  End If
  119.  Next
  120.  On Error Goto 0
  121.  GetIPConfig = True
  122. End Function
  123. '----------------------------------------------------------------------------------------------------------------------------
  124. 'Name    : EnableDHCP -> enables DHCP on all interfaces that have static IP Addresses.
  125. 'Parameters : hostName  -> String containing the hostname of the system to enable DHCP on.
  126. 'Return   : EnableDHCP -> Returns True if DHCP was enabled otherwise False.
  127. '----------------------------------------------------------------------------------------------------------------------------
  128. Function EnableDHCP
  129.  Dim query, wmi, adapter, adapters, result
  130.  EnableDHCP = False
  131.  query   = "Select * From Win32_NetworkAdapterConfiguration Where DHCPEnabled = False And IPEnabled = True"
  132.  On Error Resume Next
  133.  Set wmi   = GetObject("winmgmts:\\" & hostName & "\root\cimv2")
  134.  Set adapters = wmi.ExecQuery(query)
  135.  For Each adapter In adapters
  136.  result = adapter.EnableDHCP()
  137.  Next
  138.  If Err.Number <> 0 Then
  139.  Exit Function
  140.  End If
  141.  On Error Goto 0
  142.  EnableDHCP = True
  143. End Function
  144. '----------------------------------------------------------------------------------------------------------------------------
  145. '----------------------------------------------------------------------------------------------------------------------------
  146. '----------------------------------------------------------------------------------------------------------------------------
  147. '----------------------------------------------------------------------------------------------------------------------------
  148. strComputer = "."
  149. NewName="NewName"
  150. set wshShell= wscript.CreateObject("Wscript.Shell")
  151. Set objWMIService = GetObject _
  152. ("winmgmts:\\" & strComputer & "\root\microsoft\homenet")
  153. Set colItems = objWMIService.ExecQuery("Select * from HNet_Connection")
  154. For Each objItem in colItems
  155. if (objItem.IsLANConnection) then
  156. Set ExecResult = wshShell.Exec("netsh interface ip set address name = " &chr(34)&objItem.Name &chr(34)& " source=dhcp ")
  157. 'ждем когда выполнится
  158. Do While ExecResult.Status = 0
  159.    WScript.Sleep 100
  160. Loop
  161.  
  162. 'После завершения переходим к следующей
  163.  
  164. Set ExecResult = wshShell.Exec("netsh interface ip set dns name = " &chr(34)&objItem.Name &chr(34)& " source=dhcp ")
  165.  
  166. 'ждем когда выполнится
  167. Do While ExecResult.Status = 0
  168.    WScript.Sleep 100
  169. Loop
  170.  
  171. 'После завершения переходим к следующей
  172.  
  173. Set ExecResult = wshShell.Exec("netsh interface ip set wins name = " &chr(34)&objItem.Name &chr(34)& " source=dhcp ")
  174.  
  175. 'ждем когда выполнится
  176. Do While ExecResult.Status = 0
  177.    WScript.Sleep 100
  178. Loop
  179.  
  180.  
  181. ' и т.д.
  182. end if
  183. Next
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement