Saturday, December 26, 2015

Export DHCP Statistics

This script is extension to below script that I shared in the past.

https://gallery.technet.microsoft.com/scriptcenter/Export-DHCP-Scope-Options-7ddbc906

There was a requirement from one of our client to get the below details from each of the DHCP server:

  1. Server Name

  2. Name

  3. Subnet

  4. Dns Server

  5. IP Range

  6. Total Ips

  7. Free Ips

  8. Lease Period


IntroDHCP1

Again, Thx to the creator of DHCP powershell module...

Download the script zip from below link, extract it

https://gallery.technet.microsoft.com/scriptcenter/Export-DHCP-Statistics-8df9ea0d



Run the batch file to get the below report:

Capture

Capture


################################################################## 
#              Author: Vikas SUkhija (http://msexchange.me) 
#              Description: Find DHCP SCope Options Values  
#              for particular scope option 
#              Date: 12/17/2014 
#           Modified: 12/6/2015 
#           Update: Included subnet.subnetname,primarydns,secondarydns, 
#                   Total Ips, Free Ips, Leaseperiod 
#################################################################### 
############Import Module & define variables################## 
 
Import-module .\Microsoft.DHCP.PowerShell.Admin.psm1 
 
$collection =@() 
$idval = "6"  ### DNS Scope Option 
 
$date = get-date -format d 
$date = $date.ToString().Replace(“/”, “-”) 
$time = get-date -format t 
 
$time = $time.ToString().Replace(":""-"
               $time = $time.ToString().Replace(" """
$output = ".\" + "DHCPScopeOptions_" + $date + "_" + $time + "_.csv" 
 
 
########################################## 
$srv = Show-DHCPServers 
$servers$srv | select server 
$servers | foreach{  
$server = $_.server 
Write-host "Processing Server...............$server" -foregroundcolor Green 
$scope=Get-DHCPScope -server  $server 
#$scope 
$scp = $scope | where {$_.state -like "Active"| Select Name,Address,IPRanges,Lease 
#$scp 
 
    $scp | foreach{  
    $addr = $_.Address 
    $opt = Get-DHCPScope -server $server -scope $_.Address  
    $sts = Get-DHCPStatistics -Server $server | Select -expandproperty ScopeStatistics | where{$_.scope -like $addr| select TotalAddresses,Usedaddresses 
        $optscope = $opt | Select -expandproperty options | where{$_.optionId -like $idval| select Values  
 
$coll = “” | select ServerName,Name,Subnet,DNSserver,IPRange,TotalIPs,FreeIps,LeasePeriod 
 
$Tot = $sts.TotalAddresses 
$used = $sts.UsedAddresses 
$free = [int]$Tot -[int]$used 
$coll.ServerName = $server 
$coll.Name = $_.Name 
$coll.Subnet = $_.Address 
$coll.DNSserver = $optscope.values 
$coll.IPrange = $_.IPRanges 
$coll.TotalIPs = $sts.TotalAddresses 
$coll.FreeIps = $free 
$coll.LeasePeriod = $_.Lease 
 
$collection +$coll 
 
 
        } 
 

 
$collection | select ServerName,Name,Subnet,@{Name=”DNSserver”;Expression={$_.DNSserver}},@{Name=”IPRange”;Expression={$_.IPRange}},TotalIPs,FreeIps,Leaseperiod |  
 
export-csv $output -notypeinfo 
 
#######################################################################





 

Regards

Sukhija Vikas

http://msexchange.me

3 comments:

  1. How can i execute this script on a standaalone DHCP server?

    The server is a windows 2008 R2 box.

    ReplyDelete
  2. How can i run this script on a standalone DHCP server?

    I tried multiple modifications, nothing working

    ReplyDelete
  3. What error you are getting as it should work fine ..We even executed it from remote machine..

    ReplyDelete