locked
Dhcp statistics with 2k8 r2 RRS feed

  • Question

  • Hi guys,

    I have a DC with 2008r2 and it hasn't powershell cmdlets, so i have to use netsh.

    i'd like to convert these lines into a single array, so i can extract and create an html report.

    i'd like to create an array, or hashtable like this:

    Scopes Used Free Pending

    1    12  37  0

    Invoke-Expression "cmd /c netsh dhcp server \\dc-1 show mibinfo"
    
    Contatori MIB:
    	Ricerche = 16.
    	Offerte = 16.
    	Offerte ritardate = 0.
    	Richieste = 78.
    	Acks = 81.
    	Naks = 0.
    	Rifiuti = 0.
    	Versioni = 0.
    	ServerStartTime = giovedì 10 agosto 2017 06:27:02   
    	Scopes = 1.
    	Ambiti con ritardo configurati= 0.
    	Subnet = 192.168.10.0.
    		Numero indirizzi in uso (used) = 12.
    		Numero indirizzi liberi (free) = 37.
    		Numero di offerte in sospeso (pending) = 0.

    Maybe this link can help, but for me doesn't work: https://gallery.technet.microsoft.com/scriptcenter/05b1d766-25a6-45cd-a0f1-8741ff6c04ec

    function Get-DHCPStatistics { 
       
      Param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSObject]$Server) 
      $text = $(Invoke-Expression "cmd /c netsh dhcp server \\$Server show mibinfo") 
      if($text[2].Contains("Server may not function properly")) { Write-Host "ERROR: $Server is inaccessible or is not a DHCP server." -ForeGroundColor Red; return } 
      $serverStats = New-Object DHCPServerStatistics 
      $serverStats.Server = "$Server" 
      for($i=2;$i -lt $text.Count;$i++) { 
        if(!$text[$i]) { break } 
        $parts = $text[$i].Split("=") | %{ $_.Trim().Trim(".") } 
        switch($parts[0]) { 
          "Discovers"       { $serverStats.Discovers = [int]$parts[1] } 
          "Offers"          { $serverStats.Offers = [int]$parts[1] } 
          "Requests"        { $serverStats.Requests = [int]$parts[1] } 
          "Acks"            { $serverStats.Acks = [int]$parts[1] } 
          "Naks"            { $serverStats.Naks = [int]$parts[1] } 
          "Declines"        { $serverStats.Declines = [int]$parts[1] } 
          "Releases"        { $serverStats.Releases = [int]$parts[1] } 
          #"ServerStartTime" { $serverStats.StartTime = [DateTime]$parts[1] } 
          "Scopes"          { $serverStats.Scopes = [int]$parts[1] } 
          "Subnet"          { $scopeStats = New-Object DHCPScopeStatistics 
            $scopeStats.Scope = $parts[1]
            $scopeStats.Server = "$Server" 
            $parts = $text[++$i].Split("=") | %{ $_.Trim().Trim(".") } 
            $used = [int]$parts[1] 
            $parts = $text[++$i].Split("=") | %{ $_.Trim().Trim(".") } 
            
            $scopeStats.TotalAddresses = $used+[int]$parts[1] 
            $scopeStats.UsedAddresses = $used 
            
            $parts = $text[++$i].Split("=") | %{ $_.Trim().Trim(".") } 
            
            $scopeStats.PendingOffers = [int]$parts[1] 
            
            $serverStats.ScopeStatistics += $scopeStats 
            } 
          } 
        } 
      return $serverStats 
      } 



    Wednesday, August 16, 2017 6:36 PM

Answers

  • Simple:

    $stats = netsh dhcp server \\dc-1 show mibinfo | %{$_.Trim()} |select -skip 2 | ConvertFrom-StringData
    [pscustomobject]$stats


    \_(ツ)_/


    • Edited by jrv Wednesday, August 16, 2017 6:55 PM
    • Marked as answer by Ramses147 Wednesday, August 16, 2017 7:49 PM
    Wednesday, August 16, 2017 6:54 PM

All replies

  • Simple:

    $stats = netsh dhcp server \\dc-1 show mibinfo | %{$_.Trim()} |select -skip 2 | ConvertFrom-StringData
    [pscustomobject]$stats


    \_(ツ)_/


    • Edited by jrv Wednesday, August 16, 2017 6:55 PM
    • Marked as answer by Ramses147 Wednesday, August 16, 2017 7:49 PM
    Wednesday, August 16, 2017 6:54 PM
  • Great jrv !

    is it also possible to remove the "." at the and of each line ?

    and covert the date to short format ?

    mercoled 16 agosto 2017 20:42:44  to --> 16/08/2017

    Wednesday, August 16, 2017 6:58 PM
  • Yes.  Use string functions to clean up the data stream as I demonstrated with the "Trim()".  You will have to learn PowerShell a little more to do this but this item presents a way to help you learn.

    Your initial question was answered.  We don't encourage incremental "fix this for me" questions.  Try you hand at PowerShell scripting.  It will reward you in the long run.


    \_(ツ)_/

    Wednesday, August 16, 2017 7:02 PM