DHCP – Get a List of Scopes and Scope Options from 2012 R2 DHCP Server

This powershell script gets a list of scopes and scope options from the two servers in a split scope environment. This should work from a 2008 R2 DHCP server and higher It is rough and ready, but it works. I use out-gridview , so I can copy and pate into Excel. You will need to modify the $PrimaryDHCPServer and the $SecondaryDHCPServer to your servers.

I add a property to the object, so that you can tell which scope option is from which server.

See http://britv8.com/powershell-add-a-column-field-property-to-an-object/ for details

 

 

#
$ListofScopesandTheirOptions = $Null
$PrimaryDHCPServer = "2012-dhcp1"
$SecondaryDHCPServer = "2012-dhcp2"
 
#Get all the scopes in the Primary Server
$Scopes = Get-DhcpServerv4Scope -ComputerName $PrimaryDHCPServer
 
#For all scopes in the primary server, get the scope options and add them to $LIstofSCopesandTheirOptions 
foreach ($Scope in $Scopes)
{
$LIstofSCopesandTheirOptions += Get-DHCPServerv4OptionValue -ComputerName $PrimaryDHCPServer -ScopeID $Scope.ScopeId |select @{label=”DHCPServer”; Expression= {$PrimaryDHCPServer}},@{label=”ScopeID”; Expression= {$Scope.ScopeId}},@{label=”ScopeName”; Expression= {$Scope.Name}},*
}
 
#Get all the scopes in the Secondary Server
$Scopes = Get-DhcpServerv4Scope -ComputerName $SecondaryDHCPServer
 
#For all scopes in the secondary server, get the scope options and add them to $LIstofSCopesandTheirOptions 
foreach ($Scope in $Scopes)
{
$LIstofSCopesandTheirOptions += Get-DHCPServerv4OptionValue -ComputerName $PrimaryDHCPServer -ScopeID $Scope.ScopeId |select @{label=”DHCPServer”; Expression= {$PrimaryDHCPServer}},@{label=”ScopeID”; Expression= {$Scope.ScopeId}},@{label=”ScopeName”; Expression= {$Scope.Name}},*
}
 
#Now we have them all, output them
$LIstofSCopesandTheirOptions |out-gridview
#

 

3 thoughts on “DHCP – Get a List of Scopes and Scope Options from 2012 R2 DHCP Server

  1. Made a small alteration to get all DHCP Info in the domain

    # Find all DHCP Server in Domain
    $Servers = netsh dhcp show server
    $list=@()
    foreach ($server in $servers)
    {
    if($server.indexof(“[“) -gt 0)
    {
    $list+=$server.substring($server.indexof(“[“)+1,$server.indexof(“]”)-1-$server.indexof(“[“))
    }
    }

    #
    $ListofScopesandTheirOptions = $Null

    foreach ($DHCPServer in $List)
    {
    if (Test-Connection -BufferSize 32 -Count 1 -ComputerName $dhcpserver -Quiet)
    {
    $ErrorActionPreference = “silentlycontinue”
    $Scopes = Get-DhcpServerv4Scope -ComputerName $DHCPServer
    #For all scopes in the primary server, get the scope options and add them to $LIstofSCopesandTheirOptions
    foreach ($Scope in $Scopes)
    {
    $LIstofSCopesandTheirOptions += Get-DHCPServerv4OptionValue -ComputerName $DHCPServer -ScopeID $Scope.ScopeId |select @{label=”DHCPServer”; Expression= {$DHCPServer}},@{label=”ScopeID”; Expression= {$Scope.ScopeId}},@{label=”ScopeName”; Expression= {$Scope.Name}},*
    }
    $ErrorActionPreference = “continue”
    }
    }

    #Now we have them all, output them
    $LIstofSCopesandTheirOptions |out-gridview
    #

  2. I dared to enhance it even further. Not using netsh to get all DHCP servers but rather using PS. Furthermore, getting also server specific options and not only scope specific options. Also modified the script so that export-csv will correctly show the values. Later will add parallel execution to speed things up.

    # Find all DHCP Server in Domain
    $DhcpServers = Get-DhcpServerInDC

    foreach ($DHCPServer in $DhcpServers.DnsName){
    if (Test-Connection -BufferSize 32 -Count 1 -ComputerName $dhcpserver -Quiet){
    $ErrorActionPreference = “SilentlyContinue”
    $Scopes = Get-DhcpServerv4Scope -ComputerName $DHCPServer
    #For all scopes in the DHCP server, get the scope options and add them to $LIstofSCopesandTheirOptions
    foreach ($Scope in $Scopes){
    $LIstofSCopesandTheirOptions += Get-DHCPServerv4OptionValue -ComputerName $DHCPServer -ScopeID $Scope.ScopeId | Select-Object @{label=”DHCPServer”; Expression= {$DHCPServer}},@{label=”ScopeID”; Expression= {$Scope.ScopeId}},@{label=”ScopeName”; Expression= {$Scope.Name}},@{Name=’Value’;Expression={[string]::join(“;”, ($_.Value))}},*
    }
    $LIstofSCopesandTheirOptions += Get-DHCPServerv4OptionValue -ComputerName $DHCPServer | Select-Object @{label=”DHCPServer”; Expression= {$DHCPServer}},@{Name=’Value’;Expression={[string]::join(“;”, ($_.Value))}},*
    $ErrorActionPreference = “Continue”
    }
    }

    #Now we have them all, output them
    $LIstofSCopesandTheirOptions | Export-Csv -Path D:\EAADM\atrubajda\DhcpOptionsReport.csv -Force
    #$ListofScopesandTheirOptions | Out-GridView
    #

Leave a Reply to Jozef Trubac Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.