Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for apipa.vbs

(view source code of apipa.vbs as plain text)

  1. Option Explicit
  2.  
  3. ' Registry related constants
  4. Const HKEY_CLASSES_ROOT   = &H80000000
  5. Const HKEY_CURRENT_USER   = &H80000001
  6. Const HKEY_LOCAL_MACHINE  = &H80000002
  7. Const HKEY_USERS          = &H80000003
  8. Const HKEY_CURRENT_CONFIG = &H80000005
  9.  
  10. Dim arrSubKeys
  11. Dim blnAPIPA, blnDHCP, blnErr, blnQuiet, blnUndo
  12. Dim i, intErr, intArgs, intWinStyle
  13. Dim objFSO, objReg, objUndo
  14. Dim strHive, strIP, strKey, strKeyPath
  15. Dim strMsg, strRegPath, strTitle, strUndo
  16.  
  17. ' Check command line arguments
  18. With WScript.Arguments
  19. 	blnQuiet = False
  20. 	intArgs  = 0
  21. 	If .Named.Exists( "C" ) Then
  22. 		blnQuiet = True
  23. 		intArgs  = intArgs + 1
  24. 	End If
  25. 	If .Named.Exists( "U" ) Then
  26. 		intArgs = intArgs + 1
  27. 	End If
  28. 	If intArgs <> .Count Then Syntax
  29. End With
  30.  
  31. ' Ask for confirmation
  32. strMsg      = "Are you sure you want to change the APIPA "   _
  33.             & "registry settings for your network adapters?" _
  34.             & vbCrLf & vbCrLf _
  35.             & "If in doubt, please click ""No"" or ""Cancel""."
  36. strTitle    = "Registry Change"
  37. intWinStyle = vbYesNoCancel + vbExclamation + vbDefaultButton2 + vbApplicationModal
  38.  
  39. If Not blnQuiet Then
  40. 	If MsgBox( strMsg, intWinStyle, strTitle ) <> vbYes Then
  41. 		Syntax
  42. 	End If
  43. End If
  44.  
  45. ' Initialize variables
  46. blnAPIPA   = False
  47. blnDHCP    = False
  48. blnErr     = False
  49. blnUndo    = False
  50. strHive    = HKEY_LOCAL_MACHINE
  51. strKeyPath = "System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces"
  52. strMsg     = ""
  53. strRegPath = "C:\apipa_undo_" _
  54.            & Year( Now )      _
  55.            & Right( 100 + Month( Now ),  2 ) _
  56.            & Right( 100 + Day( Now ),    2 ) _
  57.            & "_" _
  58.            & Right( 100 + Hour( Now ),   2 ) _
  59.            & Right( 100 + Minute( Now ), 2 ) _
  60.            & Right( 100 + Second( Now ), 2 ) _
  61.            & ".reg"
  62. strUndo    = "REGEDIT4" & vbCrLf & vbCrLf
  63.  
  64. ' Connect to the local registry
  65. Set objReg = GetObject( "winmgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv" )
  66.  
  67. ' List all subkeys of
  68. ' HKLM\System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces
  69. intErr = objReg.EnumKey( strHive, strKeyPath, arrSubKeys )
  70.  
  71. ' Loop through the list of subkeys
  72. For i = 0 To UBound( arrSubKeys )
  73. 	strKey = strKeyPath & "\" & arrSubKeys(i)
  74. 	' Check if the adapter is DHCP enabled
  75. 	blnDHCP = False
  76. 	intErr = objReg.GetDWORDValue( strHive, strKey, "EnableDHCP", blnDHCP )
  77. 	If intErr = 0 Then
  78. 		blnDHCP = CBool( blnDHCP )
  79. 	Else
  80. 		blnDHCP = False
  81. 	End If
  82. 	If blnDHCP Then
  83. 		' If so, look up its IP address
  84. 		strIP = ""
  85. 		intErr = objReg.GetStringValue( strHive, strKey, "DhcpIPAddress", strIP )
  86. 		strIP = "" & strIP
  87. 		If intErr = 0 Then
  88. 			' If the adapter does have an IP address, check if it is APIPA enabled
  89. 			intErr = objReg.GetDWORDValue( strHive, strKey, "IPAutoconfigurationEnabled", blnAPIPA )
  90. 			If intErr = 0 Then
  91. 				blnAPIPA = CBool( blnAPIPA )
  92. 			Else
  93. 				blnAPIPA = False
  94. 			End If
  95. 			If blnAPIPA Then
  96. 				' If APIPA enabled, display its IP address, ...
  97. 				strMsg  = strMsg _
  98. 				        & "Disabling IP AutoConfiguration (APIPA) for " _
  99. 				        & "the network adapter"                & vbCrLf _
  100. 				        & "with IP address "       & strIP     & vbCrLf
  101. 				' ... prepare the undo file, ...
  102. 				strUndo = strUndo _
  103. 				        & "[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet"  _
  104. 				        & "\Services\Tcpip\Parameters\Interfaces\"        _
  105. 				        & arrSubKeys(i) & "]"                    & vbCrLf _
  106. 				        & """IPAutoconfigurationEnabled""=dword:00000001" _
  107. 				        & vbCrLf & vbCrLf
  108. 				blnUndo = True
  109. 				' ... and disable APIPA
  110. 				intErr = objReg.SetDWORDValue( strHive, strKey, "IPAutoconfigurationEnabled", 0 )
  111. 				If intErr <> 0 Then
  112. 					strMsg = strMsg _
  113. 					       & "Error " & intErr _
  114. 					       & " while trying to change the value of" & vbCrLf _
  115. 					       & "[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet"  _
  116. 					       & "\Services\Tcpip\Parameters\Interfaces\"        _
  117. 					       & arrSubKeys(i) & "\IPAutoconfigurationEnabled"
  118. 				End If
  119. 			End If
  120. 		End If
  121. 	End If
  122. Next
  123.  
  124. ' Save the undo settings in the undo file, unless the /U switch was used
  125. If blnUndo Then
  126. 	If Not WScript.Arguments.Named.Exists( "U" ) Then
  127. 		On Error Resume Next
  128. 		Set objFSO  = CreateObject( "Scripting.FileSystemObject" )
  129. 		If Err Then
  130. 			blnErr = True
  131. 		End If
  132. 		Set objUndo = objFSO.CreateTextFile( strRegPath, True, False )
  133. 		If Err Then
  134. 			blnErr = True
  135. 		End If
  136. 		objUndo.Write strUndo
  137. 		If Err Then
  138. 			blnErr = True
  139. 		End If
  140. 		objUndo.Close
  141. 		If Err Then
  142. 			blnErr = True
  143. 		End If
  144. 		Set objUndo = Nothing
  145. 		Set objFSO  = Nothing
  146. 		On Error Goto 0
  147. 		If blnErr Then
  148. 			strMsg = "Error creating the Undo file." _
  149. 			       & vbCrLf _
  150. 			       & "You may want to write down or print the following values:" _
  151. 			       & vbCrLf _
  152. 			       & strUndo
  153. 		Else
  154. 			strMsg = strMsg _
  155. 			       & vbCrLf _
  156. 			       & "The changes will take effect after the next reboot." _
  157. 			       & vbCrLf _
  158. 			       & "An Undo file has been created (" & strRegPath & ")." _
  159. 			       & vbCrLf _
  160. 			       & "In case the change turns out to be a disaster, doubleclick this" _
  161. 			       & vbCrLf _
  162. 			       & "undo file to restore the previous settings."
  163. 		End If
  164. 	End If
  165. Else
  166. 	strMsg = strMsg _
  167. 	       & vbCrLf _
  168. 	       & "No network adapter needed to be reconfigured."
  169. End If
  170.  
  171. ' Display the results
  172. WScript.Echo strMsg
  173.  
  174. Set objReg  = Nothing
  175. Set objUndo = Nothing
  176. Set objFSO  = Nothing
  177.  
  178.  
  179. Sub Syntax
  180. 	strMsg = vbCrLf _
  181. 	       & UCase( WScript.ScriptName ) & ",  Version 1.00" _
  182. 	       & vbCrLf _
  183. 	       & "Disable IP Autoconfiguration (APIPA) for all DHCP enabled network adapters"  _
  184. 	       & vbCrLf & vbCrLf _
  185. 	       & "Usage: " & UCase( WScript.ScriptName ) & "  [ /C ]"                          _
  186. 	       & vbCrLf & vbCrLf _
  187. 	       & "Where: /Q  skips the prompt for Confirmation"                                _
  188. 	       & vbCrLf _
  189. 	       & "       /U  skips the Undo file"                                _
  190. 	       & vbCrLf & vbCrLf _
  191. 	       & "Notes:" _
  192. 	       & vbCrLf _
  193. 	       & "[1] APIPA is disabled by setting ""IPAutoconfigurationEnabled"" to 0 in the" _
  194. 	       & vbCrLf _
  195. 	       & "    ""HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces"""  _
  196. 	       & vbCrLf _
  197. 	       & "    subkeys of the registry."                                                _
  198. 	       & vbCrLf _
  199. 	       & "[2] An undo file will be created: ""C:\apipa_undo_YYYYMMDD_HHmmss.reg""."    _
  200. 	       & vbCrLf _
  201. 	       & "    To restore the previous settings, just doubleclick this undo file."      _
  202. 	       & vbCrLf _
  203. 	       & "[3] APIPA is an acronym for Automatic Private Internet Protocol Addressing." _
  204. 	       & vbCrLf _
  205. 	       & "    In short it means an IP address in the 169.254.*.* range is used if no"  _
  206. 	       & vbCrLf _
  207. 	       & "    DHCP server is available. More info about APIPA:"                        _
  208. 	       & vbCrLf _
  209. 	       & "    http://support.microsoft.com/kb/220874"                                  _
  210. 	       & vbCrLf _
  211. 	       & "    http://www.petri.co.il/disable_apipa_in_windows_2000_xp_2003.htm"        _
  212. 	       & vbCrLf & vbCrLf _
  213. 	       & "Written by Rob van der Woude" _
  214. 	       & vbCrLf _
  215. 	       & "http://www.robvanderwoude.com"
  216. 	WScript.Echo strMsg
  217. 	WScript.Quit 1
  218. End Sub
  219.  

page last modified: 2024-02-26; loaded in 0.0253 seconds