Windows DHCP Server - Script to remove duplicate leases to the same hostname











up vote
0
down vote

favorite












I’m looking at creating a script that clears out duplicate DHCP leases from our Windows Server 2008 R2 based DHCP server. I’ve found out that you can’t do this natively in PowerShell, but you can use PowerShell to call a netsh.exe command and then manipulate the data.



Command to list all DHCP leases:



netsh dhcp server 10.100.2.241 scope 10.100.0.0 show clients 1


The resulting output is a bit of a mess:



Changed the current scope context to 10.100.0.0 scope.

Type : N - NONE, D - DHCP B - BOOTP, U - UNSPECIFIED, R - RESERVATION IP
============================================================================================
IP Address - Subnet Mask - Unique ID - Lease Expires -Type -Name
============================================================================================

10.100.0.51 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 21:29:01 -D- WT008064807e32.domain.com
10.100.0.52 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 20:13:47 -D- EXT1054.domain.com
10.100.0.53 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 22:54:14 -D- EXT1018.domain.com
10.100.0.54 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 11:01:57 -D- V2040.domain.com
10.100.0.55 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 19:50:19 -D- V1041.domain.com
----SNIP----


As an example, the host V2119 is listed twice within the output with different IP addresses and Lease Expiry times:



V2119 - 10.100.6.45 - 24/05/2016 23:24:43
V2119 - 10.100.5.167 - 24/05/2016 23:06:21


In this example, I would delete 10.100.5.167 as it will expire earlier and is therefore the older lease and redundant.



Command to delete a specific lease by IP:



netsh dhcp server 10.100.2.241 scope 10.100.0.0 delete lease 10.100.5.167


I found this article, whereby the OP uses the output of the netsh command in PowerShell and then trims various bits to only return a list of hostnames and IPs. Although the result isn’t what I'm after, the trimming might come in handy. - https://theadminguy.com/2009/10/14/export-dhcp-leases-to-html-using-powershell/



Basically, I need a script to output all leases in DHCP, work out which hostnames are duplicated, then of those that are duplicated work out which is going to expire earlier and return the IP address and then pipe that into the netsh delete lease command above.



Any help you can give me on this would be amazing and hugely appreciated.



Give me a shout if you need anything clarifying.










share|improve this question






















  • Please note that Super User is not a script writing service. If you tell us what you have tried so far (including any scripts you are using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?.
    – DavidPostill
    Jul 22 '16 at 9:27















up vote
0
down vote

favorite












I’m looking at creating a script that clears out duplicate DHCP leases from our Windows Server 2008 R2 based DHCP server. I’ve found out that you can’t do this natively in PowerShell, but you can use PowerShell to call a netsh.exe command and then manipulate the data.



Command to list all DHCP leases:



netsh dhcp server 10.100.2.241 scope 10.100.0.0 show clients 1


The resulting output is a bit of a mess:



Changed the current scope context to 10.100.0.0 scope.

Type : N - NONE, D - DHCP B - BOOTP, U - UNSPECIFIED, R - RESERVATION IP
============================================================================================
IP Address - Subnet Mask - Unique ID - Lease Expires -Type -Name
============================================================================================

10.100.0.51 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 21:29:01 -D- WT008064807e32.domain.com
10.100.0.52 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 20:13:47 -D- EXT1054.domain.com
10.100.0.53 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 22:54:14 -D- EXT1018.domain.com
10.100.0.54 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 11:01:57 -D- V2040.domain.com
10.100.0.55 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 19:50:19 -D- V1041.domain.com
----SNIP----


As an example, the host V2119 is listed twice within the output with different IP addresses and Lease Expiry times:



V2119 - 10.100.6.45 - 24/05/2016 23:24:43
V2119 - 10.100.5.167 - 24/05/2016 23:06:21


In this example, I would delete 10.100.5.167 as it will expire earlier and is therefore the older lease and redundant.



Command to delete a specific lease by IP:



netsh dhcp server 10.100.2.241 scope 10.100.0.0 delete lease 10.100.5.167


I found this article, whereby the OP uses the output of the netsh command in PowerShell and then trims various bits to only return a list of hostnames and IPs. Although the result isn’t what I'm after, the trimming might come in handy. - https://theadminguy.com/2009/10/14/export-dhcp-leases-to-html-using-powershell/



Basically, I need a script to output all leases in DHCP, work out which hostnames are duplicated, then of those that are duplicated work out which is going to expire earlier and return the IP address and then pipe that into the netsh delete lease command above.



Any help you can give me on this would be amazing and hugely appreciated.



Give me a shout if you need anything clarifying.










share|improve this question






















  • Please note that Super User is not a script writing service. If you tell us what you have tried so far (including any scripts you are using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?.
    – DavidPostill
    Jul 22 '16 at 9:27













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I’m looking at creating a script that clears out duplicate DHCP leases from our Windows Server 2008 R2 based DHCP server. I’ve found out that you can’t do this natively in PowerShell, but you can use PowerShell to call a netsh.exe command and then manipulate the data.



Command to list all DHCP leases:



netsh dhcp server 10.100.2.241 scope 10.100.0.0 show clients 1


The resulting output is a bit of a mess:



Changed the current scope context to 10.100.0.0 scope.

Type : N - NONE, D - DHCP B - BOOTP, U - UNSPECIFIED, R - RESERVATION IP
============================================================================================
IP Address - Subnet Mask - Unique ID - Lease Expires -Type -Name
============================================================================================

10.100.0.51 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 21:29:01 -D- WT008064807e32.domain.com
10.100.0.52 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 20:13:47 -D- EXT1054.domain.com
10.100.0.53 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 22:54:14 -D- EXT1018.domain.com
10.100.0.54 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 11:01:57 -D- V2040.domain.com
10.100.0.55 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 19:50:19 -D- V1041.domain.com
----SNIP----


As an example, the host V2119 is listed twice within the output with different IP addresses and Lease Expiry times:



V2119 - 10.100.6.45 - 24/05/2016 23:24:43
V2119 - 10.100.5.167 - 24/05/2016 23:06:21


In this example, I would delete 10.100.5.167 as it will expire earlier and is therefore the older lease and redundant.



Command to delete a specific lease by IP:



netsh dhcp server 10.100.2.241 scope 10.100.0.0 delete lease 10.100.5.167


I found this article, whereby the OP uses the output of the netsh command in PowerShell and then trims various bits to only return a list of hostnames and IPs. Although the result isn’t what I'm after, the trimming might come in handy. - https://theadminguy.com/2009/10/14/export-dhcp-leases-to-html-using-powershell/



Basically, I need a script to output all leases in DHCP, work out which hostnames are duplicated, then of those that are duplicated work out which is going to expire earlier and return the IP address and then pipe that into the netsh delete lease command above.



Any help you can give me on this would be amazing and hugely appreciated.



Give me a shout if you need anything clarifying.










share|improve this question













I’m looking at creating a script that clears out duplicate DHCP leases from our Windows Server 2008 R2 based DHCP server. I’ve found out that you can’t do this natively in PowerShell, but you can use PowerShell to call a netsh.exe command and then manipulate the data.



Command to list all DHCP leases:



netsh dhcp server 10.100.2.241 scope 10.100.0.0 show clients 1


The resulting output is a bit of a mess:



Changed the current scope context to 10.100.0.0 scope.

Type : N - NONE, D - DHCP B - BOOTP, U - UNSPECIFIED, R - RESERVATION IP
============================================================================================
IP Address - Subnet Mask - Unique ID - Lease Expires -Type -Name
============================================================================================

10.100.0.51 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 21:29:01 -D- WT008064807e32.domain.com
10.100.0.52 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 20:13:47 -D- EXT1054.domain.com
10.100.0.53 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 22:54:14 -D- EXT1018.domain.com
10.100.0.54 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 11:01:57 -D- V2040.domain.com
10.100.0.55 - 255.255.248.0 - 00-11-22-33-44-55 -24/05/2016 19:50:19 -D- V1041.domain.com
----SNIP----


As an example, the host V2119 is listed twice within the output with different IP addresses and Lease Expiry times:



V2119 - 10.100.6.45 - 24/05/2016 23:24:43
V2119 - 10.100.5.167 - 24/05/2016 23:06:21


In this example, I would delete 10.100.5.167 as it will expire earlier and is therefore the older lease and redundant.



Command to delete a specific lease by IP:



netsh dhcp server 10.100.2.241 scope 10.100.0.0 delete lease 10.100.5.167


I found this article, whereby the OP uses the output of the netsh command in PowerShell and then trims various bits to only return a list of hostnames and IPs. Although the result isn’t what I'm after, the trimming might come in handy. - https://theadminguy.com/2009/10/14/export-dhcp-leases-to-html-using-powershell/



Basically, I need a script to output all leases in DHCP, work out which hostnames are duplicated, then of those that are duplicated work out which is going to expire earlier and return the IP address and then pipe that into the netsh delete lease command above.



Any help you can give me on this would be amazing and hugely appreciated.



Give me a shout if you need anything clarifying.







powershell dhcp






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jul 21 '16 at 20:16









sixarm

62




62












  • Please note that Super User is not a script writing service. If you tell us what you have tried so far (including any scripts you are using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?.
    – DavidPostill
    Jul 22 '16 at 9:27


















  • Please note that Super User is not a script writing service. If you tell us what you have tried so far (including any scripts you are using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?.
    – DavidPostill
    Jul 22 '16 at 9:27
















Please note that Super User is not a script writing service. If you tell us what you have tried so far (including any scripts you are using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?.
– DavidPostill
Jul 22 '16 at 9:27




Please note that Super User is not a script writing service. If you tell us what you have tried so far (including any scripts you are using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?.
– DavidPostill
Jul 22 '16 at 9:27










1 Answer
1






active

oldest

votes

















up vote
1
down vote













You're looking for the Get-DhcpServerv4Lease and Remove-DhcpServerv4Lease cmdlets. First, we'll get all the current leases:



Get-DhcpServerv4Lease |


That returns a bunch of instances of DhcpServerv4Lease. We'll group them by the host name:



group HostName |


The rest of these steps have to be done in a ForEach-Object because we need to look inside each group.



% { $_.Group |


We'll sort each group's contents by expiration time descending, so later times come earlier:



sort LeaseExpiryTime -Descending |


We want to leave the first (latest) one alone, so we'll skip it:



select -Skip 1 |


Finally, we'll blow away everything that remains:



Remove-DhcpServerv4Lease }


Putting it all together, we get this short script:



Get-DhcpServerv4Lease | group HostName | % {$_.Group | sort LeaseExpiryTime -Descending | select -Skip 1 | Remove-DhcpServerv4Lease}





share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1103733%2fwindows-dhcp-server-script-to-remove-duplicate-leases-to-the-same-hostname%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    You're looking for the Get-DhcpServerv4Lease and Remove-DhcpServerv4Lease cmdlets. First, we'll get all the current leases:



    Get-DhcpServerv4Lease |


    That returns a bunch of instances of DhcpServerv4Lease. We'll group them by the host name:



    group HostName |


    The rest of these steps have to be done in a ForEach-Object because we need to look inside each group.



    % { $_.Group |


    We'll sort each group's contents by expiration time descending, so later times come earlier:



    sort LeaseExpiryTime -Descending |


    We want to leave the first (latest) one alone, so we'll skip it:



    select -Skip 1 |


    Finally, we'll blow away everything that remains:



    Remove-DhcpServerv4Lease }


    Putting it all together, we get this short script:



    Get-DhcpServerv4Lease | group HostName | % {$_.Group | sort LeaseExpiryTime -Descending | select -Skip 1 | Remove-DhcpServerv4Lease}





    share|improve this answer

























      up vote
      1
      down vote













      You're looking for the Get-DhcpServerv4Lease and Remove-DhcpServerv4Lease cmdlets. First, we'll get all the current leases:



      Get-DhcpServerv4Lease |


      That returns a bunch of instances of DhcpServerv4Lease. We'll group them by the host name:



      group HostName |


      The rest of these steps have to be done in a ForEach-Object because we need to look inside each group.



      % { $_.Group |


      We'll sort each group's contents by expiration time descending, so later times come earlier:



      sort LeaseExpiryTime -Descending |


      We want to leave the first (latest) one alone, so we'll skip it:



      select -Skip 1 |


      Finally, we'll blow away everything that remains:



      Remove-DhcpServerv4Lease }


      Putting it all together, we get this short script:



      Get-DhcpServerv4Lease | group HostName | % {$_.Group | sort LeaseExpiryTime -Descending | select -Skip 1 | Remove-DhcpServerv4Lease}





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        You're looking for the Get-DhcpServerv4Lease and Remove-DhcpServerv4Lease cmdlets. First, we'll get all the current leases:



        Get-DhcpServerv4Lease |


        That returns a bunch of instances of DhcpServerv4Lease. We'll group them by the host name:



        group HostName |


        The rest of these steps have to be done in a ForEach-Object because we need to look inside each group.



        % { $_.Group |


        We'll sort each group's contents by expiration time descending, so later times come earlier:



        sort LeaseExpiryTime -Descending |


        We want to leave the first (latest) one alone, so we'll skip it:



        select -Skip 1 |


        Finally, we'll blow away everything that remains:



        Remove-DhcpServerv4Lease }


        Putting it all together, we get this short script:



        Get-DhcpServerv4Lease | group HostName | % {$_.Group | sort LeaseExpiryTime -Descending | select -Skip 1 | Remove-DhcpServerv4Lease}





        share|improve this answer












        You're looking for the Get-DhcpServerv4Lease and Remove-DhcpServerv4Lease cmdlets. First, we'll get all the current leases:



        Get-DhcpServerv4Lease |


        That returns a bunch of instances of DhcpServerv4Lease. We'll group them by the host name:



        group HostName |


        The rest of these steps have to be done in a ForEach-Object because we need to look inside each group.



        % { $_.Group |


        We'll sort each group's contents by expiration time descending, so later times come earlier:



        sort LeaseExpiryTime -Descending |


        We want to leave the first (latest) one alone, so we'll skip it:



        select -Skip 1 |


        Finally, we'll blow away everything that remains:



        Remove-DhcpServerv4Lease }


        Putting it all together, we get this short script:



        Get-DhcpServerv4Lease | group HostName | % {$_.Group | sort LeaseExpiryTime -Descending | select -Skip 1 | Remove-DhcpServerv4Lease}






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 30 '16 at 15:29









        Ben N

        29k1395142




        29k1395142






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1103733%2fwindows-dhcp-server-script-to-remove-duplicate-leases-to-the-same-hostname%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Plaza Victoria

            In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

            How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...