Blog Post Title Two

<#

.SYNOPSIS

  Gathers DHCP scope information, address pools, exclusions, leases, and reservations

  from a specified DHCP server and writes them to a CSV file in a custom format.


.DESCRIPTION

  This script:

    - Prompts for the DHCP server name (or you can hardcode it).

    - Uses the DhcpServer module to query scopes, pools, exclusions, leases, and reservations.

    - Converts the lease duration to a dd:HH:mm format.

    - Writes out lines to a CSV file to match the layout/format in your sample image.

    - Inserts two blank lines between each scope's data block.


.PARAMETER None

#>


Import-Module DhcpServer -ErrorAction Stop


# Prompt for DHCP server name (or hardcode it)

$DhcpServer = Read-Host "Enter DHCP server name or IP"


# Path to the CSV output file

$csvPath = "C:\Temp\DhcpReport.csv"


# If an existing report file is present, remove it first

if (Test-Path $csvPath) {

    Remove-Item $csvPath -Force

}


# Retrieve all IPv4 scopes from the specified DHCP server

$scopes = Get-DhcpServerv4Scope -ComputerName $DhcpServer


foreach ($scope in $scopes) {

    # -- SCOPE INFO --------------------------------------------------------

    $scopeName = $scope.Name

    $scopeId   = $scope.ScopeId

    $startIp   = $scope.StartRange

    $endIp     = $scope.EndRange

    $desc      = $scope.Description


    # Convert LeaseDuration (System.TimeSpan) to dd:HH:mm

    $leaseDuration = $scope.LeaseDuration

    $leaseDurationString = "{0}d:{1:D2}:{2:D2}" -f $leaseDuration.Days, $leaseDuration.Hours, $leaseDuration.Minutes


    # -- ADDRESS POOL (Scopes in Windows DHCP normally have one main range) --

    # If you wish to retrieve any defined pools specifically:

    #   $pool = Get-DhcpServerv4Scope -ComputerName $DhcpServer -ScopeId $scopeId

    # but the main scope’s StartRange/EndRange is typically the same thing.

    # If multiple ranges exist, you can iterate them similarly.


    # -- EXCLUSIONS -------------------------------------------------------

    $exclusions = Get-DhcpServerv4ExclusionRange -ComputerName $DhcpServer -ScopeId $scopeId -ErrorAction SilentlyContinue

   

    # -- LEASES -----------------------------------------------------------

    $leases = Get-DhcpServerv4Lease -ComputerName $DhcpServer -ScopeId $scopeId -ErrorAction SilentlyContinue

   

    # -- RESERVATIONS -----------------------------------------------------

    $reservations = Get-DhcpServerv4Reservation -ComputerName $DhcpServer -ScopeId $scopeId -ErrorAction SilentlyContinue


    # =========================================================================

    # Write out the block for this scope to CSV in the style of your example

    # =========================================================================

   

    # 1) SCOPE HEADER (Column A has labels, Column B has values)

    Add-Content $csvPath "SCOPE"

    Add-Content $csvPath "Scope Name,$scopeName"

    Add-Content $csvPath "Scope,$scopeId"

    Add-Content $csvPath "Start IP,$startIp"

    Add-Content $csvPath "End IP,$endIp"

    Add-Content $csvPath "Lease Duration,$leaseDurationString"

    Add-Content $csvPath "Description,$desc"


    # 2) ADDRESS POOL

    Add-Content $csvPath "ADDRESS POOL"

    Add-Content $csvPath "Start IP,$startIp"

    Add-Content $csvPath "End IP,$endIp"


    # 3) EXCLUSIONS

    Add-Content $csvPath "EXCLUSIONS"

    if ($exclusions) {

        foreach ($ex in $exclusions) {

            Add-Content $csvPath "Start IP,$($ex.StartRange)"

            Add-Content $csvPath "End IP,$($ex.EndRange)"

        }

    }

    else {

        # If no exclusions, still show the headers but no data lines

        Add-Content $csvPath "Start IP,"

        Add-Content $csvPath "End IP,"

    }


    # 4) ADDRESS LEASES

    Add-Content $csvPath "ADDRESS LEASES"

    # Write the column headers

    Add-Content $csvPath "Client IP,Name,Lease Expiration,Type,Unique ID,Description"


    if ($leases) {

        foreach ($l in $leases) {

            # Construct lease expiration info. If it's a reservation, often the Expiration might be a special value

            $leaseExpireStr = if ($l.AddressState -eq "Active") {

                "Reservation (active)"

            } elseif ($l.AddressState -eq "Inactive") {

                "Reservation (inactive)"

            } else {

                $l.LeaseExpiryTime

            }


            $clientIp = $l.IPAddress

            $clientName = $l.HostName

            $type = $l.AddressState   # or could be "DHCP" if $l.AddressState -eq 'Active' (adjust as needed)

            $uniqueId = $l.ClientId

            $leaseDesc = $l.Description


            Add-Content $csvPath "$clientIp,$clientName,$leaseExpireStr,$type,$uniqueId,$leaseDesc"

        }

    }


    # 5) RESERVATIONS

    Add-Content $csvPath "RESERVATIONS"

    # Column headers

    Add-Content $csvPath "Reservation Name,MAC Address"


    if ($reservations) {

        foreach ($r in $reservations) {

            $resName = $r.Name

            # Convert the raw client ID into a simpler MAC if needed

            # By default it might have a leading 01 and be hex pairs, e.g. 0100AA11BB22CC

            # Adjusting example below if you want to parse out the MAC more cleanly:

            $macClean = ($r.ClientId -replace '^..(.*)', '$1') -replace '(..)(?!$)', '$1:'


            # Alternatively you can just display $r.ClientId directly

            # $macClean = $r.ClientId

            Add-Content $csvPath "$resName,$macClean"

        }

    }


    # 6) Write two blank lines before moving on to the next scope block

    Add-Content $csvPath ""

    Add-Content $csvPath ""

}


Write-Host "DHCP report created at $csvPath"

Previous
Previous

Blog Post Title One

Next
Next

Blog Post Title Three