Release all DHCP IP addresses using WQL (Perl)

This code can be found in Chapter 12 of Windows XP Cookbook

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# From the book "Windows XP Cookbook"
# ISBN: 0596007256

# This code performs a query for all DHCP enabled IP addresses and releases
# them.  Use this instead of the other example if you want to tailor the 
# query to return a subset of connections. Modify the WQL statement based
# on your criteria.
# ------ SCRIPT CONFIGURATION ------

use Win32::OLE qw(in);

$strComputer = '.';
# ------ END CONFIGURATION ---------
$objWMI = Win32::OLE->GetObject('winmgmts:\\\\' . $strComputer . '\\root\\cimv2');
$colNetworkAdapters = $objWMI->ExecQuery('Select * From Win32_NetworkAdapterConfiguration Where DHCPEnabled = True');
foreach my $objNetworkConfig (in $colNetworkAdapters) {
    $intRC = $objNetworkConfig->ReleaseDHCPLease();
    if ($intRC == 0) {
        print 'Released IP address for ' . $objNetworkConfig->Description, "\n";
    }
    elsif ($intRC == 1) {
        print 'You must reboot to release the IP address for ' . $objNetworkConfig->Description, "\n";
    }
    else {
        print 'There was an error releasing the IP address for ' . $objNetworkConfig->Description . ': ' . $intRC, "\n";
    }
}

This code has been viewed 796 times.

New from the creators of TechTasks.com: StatSheet.com