Renew Dynamic IP Addresses in WSH

October 3rd, 2008

“Is there any way to release and renew a dynamic IP address in WSH?” - Chris. F

Releasing and renewing dynamic IP addresses can be a very effective method of repairing a broken internet connection. Effectively, you are instructing your network adapter to request a new IP address from a DHCP server. While the Windows Script Host doesn’t provide any method of doing this directly, the Windows Management Instrumentation, or WMI, does.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" _
		& strComputer & "\root\cimv2")
Set colNetCards = objWMIService.ExecQuery _
	("Select * From Win32_NetworkAdapterConfiguration" _
		& " Where IPEnabled = True")

You’ll need to start by connecting to the local WMI Service. Then you’ll need to execute a query that returns a collection of network adapter configurations. Notice that this query uses a Where clause to limit the results that are returned. By specifying that the IPEnabled property be set to True, we ensure that we’re only dealing will those network adapters that are set to use DHCP IP addresses.

For Each objNetCard In colNetCards
	objNetCard.ReleaseDHCPLease()
	objNetCard.RenewDHCPLease()
Next

Releasing and renewing IP addresses is now as simple as moving through the collection making calls to the ReleaseDHCPLease and RenewDHCPLease methods provided by the Network Adapter Configuration objects.

Please use the trackback link when linking to this post.

Related Posts:

Add to Technorati Favorites

One Response to “Renew Dynamic IP Addresses in WSH”

  1. i am trying to connect to internet using a belkin usb adapter to a belkin wireless router. i keep getting prob? Says:

    […] following action cannot be completed: Renewing IP Address" can some help me with this problem I am able to connect to the router on my laptop and on my other computer. I also tried to install a …to repair the connection it says "WIndows could not finish repairing problem because the […]

Leave a Reply

.htaccess Apache article articles by Nilpo ASP ASP Free automation clipboard copy desktop Dev Shed docx Internet Explorer message box Microsoft PHP registry remove script scripting text tutorial tweak tweets Twitter UAC updates User Account Control VBS VBScript vista volume Windows Windows 7 Windows Guru Windows Script Windows Script Host Windows Scripting Windows Vista Windows XP Word WScript wscript.exe WSH XP