none
How to export DHCP lease information AND scope names from DHCP server? RRS feed

  • Question

  • Hello,

    My customer needs report of all DHCP scopes and active leases. We have two Windows Server 2012 R2 DHCP servers and they are failover partners for each other.

    Report should include all following information:

    - Host name

    - Leased IP address

    - ScopeID

    - Scope name (we have over 400 scopes)

    I attempted to run following command, but it didn't return scope names :(

    Get-DhcpServerv4Scope -Computername dhcp1.domain.local | Get-DhcpServerv4Lease -Computername dhcp1.domain.local

    Tuesday, April 25, 2017 10:23 AM

All replies

  • Hi,

    name is scope name!

    Better one:

    $scopes = Get-DhcpServerv4Scope -ComputerName 2012r2dc 
    foreach($scope in $scopes)
    {
        write-host "The current scope name is:" $scope.name -ForegroundColor Red
        $lease = Get-DhcpServerv4Lease -ScopeId $scope.ScopeId
        $lease
       
    }

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Edited by Hello_2018 Friday, April 28, 2017 9:32 AM
    • Proposed as answer by Nedim Mehic Friday, April 28, 2017 9:46 AM
    Friday, April 28, 2017 9:12 AM
  • Hi Andy and thanks,

    It outputs the information I need, but I would like to have output to a CSV file and for some reason  only 17 first characters of hostname is displayed, rest is replaced by three dots (...)


    • Edited by weedee Tuesday, May 2, 2017 1:12 PM
    Tuesday, May 2, 2017 12:53 PM
  • Hi weedee

    >>only 17 first characters of hostname is displayed, rest is replaced by three dots (...)

    $lease | select -expandproperty *name* | export-csv /out-file xx.csv

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, May 3, 2017 4:25 AM
  • $scopes = Get-DhcpServerv4Scope -ComputerName SEA-DHCP01 
    foreach($scope in $scopes)
    {
        write-host "The current scope name is:" $scope.name -ForegroundColor Red
        $lease = Get-DhcpServerv4Lease -ScopeId $scope.ScopeId
        $lease | select -expandproperty *name* | export-csv /out-file C:\export.csv

    }

    It gives me errors:

    The current scope name is: Building 1
    Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "C:\export.csv" to type "System.Char". E
    rror: "String must be exactly one character long."
    At line:6 char:67
    +     $lease | select -expandproperty *name* | export-csv /out-file C:\export.csv
    +                                                                   ~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand
     
    The current scope name is: Building 2
    Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "C:\export.csv" to type "System.Char". E
    rror: "String must be exactly one character long."
    At line:6 char:67
    +     $lease | select -expandproperty *name* | export-csv /out-file C:\export.csv
    +                                                                   ~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand


    Wednesday, May 3, 2017 6:31 AM
  • oh,no!

    my fault: 

    $lease | select -expandproperty *name* | export-csv xx.csv 

    $lease | select -expandproperty *name* | out-file xx.csv

    I mean use export-csv  or out-file

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    • Edited by Hello_2018 Wednesday, May 3, 2017 6:53 AM
    Wednesday, May 3, 2017 6:52 AM
  • Nope, still not working :(

    S C:\Windows\system32> $scopes = Get-DhcpServerv4Scope -ComputerName SEA-DHCP01 
    foreach($scope in $scopes)
    {
        write-host "The current scope name is:" $scope.name -ForegroundColor Red
        $lease = Get-DhcpServerv4Lease -ScopeId $scope.ScopeId
        $lease | select -expandproperty *name* | out-file xx.csv

    }
    The current scope name is: Building 1
    select : Multiple properties cannot be expanded.
    At line:6 char:14
    +     $lease | select -expandproperty *name* | out-file xx.csv
    +              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (DhcpServerv4Lease:PSObject) [Select-Object], PSArgumentExcep 
       tion
        + FullyQualifiedErrorId : MutlipleExpandProperties,Microsoft.PowerShell.Commands.SelectObjectCommand

    The current scope name is: Building 2
    select : Multiple properties cannot be expanded.
    At line:6 char:14
    +     $lease | select -expandproperty *name* | out-file xx.csv
    +              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (DhcpServerv4Lease:PSObject) [Select-Object], PSArgumentExcep 
       tion
        + FullyQualifiedErrorId : MutlipleExpandProperties,Microsoft.PowerShell.Commands.SelectObjectCommand

      
    Wednesday, May 3, 2017 7:25 AM
  • Hi weedee,

    $scopes = Get-DhcpServerv4Scope -ComputerName 'DC' 
    foreach($scope in $scopes)
    {
        #write-host "The current scope name is:" $scope.name -ForegroundColor Red
        $lease = Get-DhcpServerv4Lease -ScopeId $scope.ScopeId
        #$lease
    }
        $pro = @{'name'=$scope.Name;'ipaddress'=$lease.IPAddress;'ScopeId'=$lease.ScopeId;'ClientId'=$lease.ClientId}
        $obj = New-Object -TypeName psobject -Property $pro
        $obj | Export-Csv c:\DhcpInfo.csv -NoTypeInformation -Append
    this above could be better.

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    • Edited by Hello_2018 Wednesday, May 3, 2017 8:14 AM
    Wednesday, May 3, 2017 8:13 AM
  • Hi weedee,

    $scopes = Get-DhcpServerv4Scope -ComputerName 'DC' 
    foreach($scope in $scopes)
    {
        #write-host "The current scope name is:" $scope.name -ForegroundColor Red
        $lease = Get-DhcpServerv4Lease -ScopeId $scope.ScopeId
        #$lease
    }
        $pro = @{'name'=$scope.Name;'ipaddress'=$lease.IPAddress;'ScopeId'=$lease.ScopeId;'ClientId'=$lease.ClientId}
        $obj = New-Object -TypeName psobject -Property $pro
        $obj | Export-Csv c:\DhcpInfo.csv -NoTypeInformation -Append
    this above could be better.

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Hi Andy!

    Otherwise okay, could you modify the script to also display scopes with NO leases?

    Thursday, May 4, 2017 1:31 PM
  • Hi,

    $scopes = Get-DhcpServerv4Scope -ComputerName 2012r2dc 
    foreach($scope in $scopes)
    {
        #write-host "The current scope name is:" $scope.name -ForegroundColor Red
        $lease = Get-DhcpServerv4Lease -ScopeId $scope.ScopeId
        $lease
    }
        $pro = @{'name'=$scope.Name;'ipaddress'=$($lease.IPAddress);'ScopeId'=$($lease.ScopeId);'ClientId'=$lease.ClientId;'LeaseExpiryTime'=$lease.LeaseExpiryTime}
        $obj = New-Object -TypeName psobject -Property $pro
        $obj | Export-Csv c:\fuc123k.csv -NoTypeInformation -Append
    add this line:

    'LeaseExpiryTime'=$lease.LeaseExpiryTime

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, May 5, 2017 7:24 AM
  • CSV file content does not look right. This is all content there is:

    "ScopeId","name","ClientId","ipaddress","LeaseExpiryTime"

    "System.Object[]","Scope 1","System.Object[]","System.Object[]","System.Object[]"


    • Edited by weedee Tuesday, May 9, 2017 10:42 AM
    Tuesday, May 9, 2017 10:41 AM
  • Hi weedee,

    sorry for the late reply.

    >>CSV file content does not look right. This is all content there is

    these are objects, you could use select -expandproperty to get all of them in your script.

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, May 16, 2017 8:37 AM
  • Hi,
    Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Andy

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, June 7, 2017 2:49 AM