GET DHCP Scope informations - VERSION1


posted by Sylvain LESIRE
03-06-2009

Downloads: 1,372
File size: 2.5kB
Views: 13,491

Embed
GET DHCP Scope informations - VERSION1
  1. Function Get-DhcpScope ([string]$Servername){ 
  2. $Tab = [char]9 
  3. $DHCPIpAddress = (Get-WmiObject -Class Win32_PingStatus -Filter "Address='$servername' and ResolveAddressNames=true").ProtocolAddress 
  4.  
  5. trap {  
  6.         Write-host "Error !!"   $_.Exception.Message -ForegroundColor Red 
  7.         Write-Host "Solution :" -ForegroundColor green     
  8.         Write-Host "$tab Copy Dll dhcpobjs.dll to $env:windir\system32" 
  9.         Write-Host "$tab Register DLL : REGSVR32.exe $env:windir\system32\dhcpobjs.dll /s " 
  10.         exit } & {  
  11.     $dhcpmanager = New-Object -ComObject dhcp.manager 
  12.  
  13. $dhcpmanager = New-Object -ComObject dhcp.manager 
  14. $dhcpsrvr = $dhcpmanager.Servers.Connect($DHCPIpAddress
  15. $NBScopeDHCP = $dhcpsrvr.scopes.count 
  16. $ScopeDHCP = @{"" = ""
  17.  
  18. for ($a = 1; $a -le $NBScopeDHCP ; $a++) { 
  19.     $ScopeDHCP.add($dhcpsrvr.scopes.item($a).address , $dhcpsrvr.scopes.item($a).name
  20.  
  21. NetSHScopeDHCP $Servername 
  22. $DHCPArray = TransformDHCPToArray "c:\temp\DumpAllscope$servername.txt" $ScopeDHCP 
  23. $DHCPArray  
  24.  
  25. function TransformDHCPToArray ($filename,$Scope){ 
  26. $myinitFile = Get-Content $filename  
  27. $Nbredeligne = $myinitFile.length 
  28. $temporaire = $null 
  29. $temporaire = @() 
  30. for ($a = 0; $a -le $nbredeligne -1; $a++) { 
  31.     if ($myinitfile[$a].contains("Subnet")){ 
  32.         $personalarray = ""| select Subnet,NoOfAddressesinUse,NoOffreeAddresses, Comment 
  33.         $ip = $myinitfile[$a].substring( $myinitfile[$a].indexof("=")+2, $myinitfile[$a].length - ($myinitfile[$a].indexof("=")+3)) 
  34.         $personalarray.Subnet = $ip 
  35.         $personalarray.NoofAddressesinUse = $myinitfile[$a+1].substring( $myinitfile[$a+1].indexof("=")+2, $myinitfile[$a+1].length - ($myinitfile[$a+1].indexof("=")+3)) 
  36.         $personalarray.NoOffreeAddresses  = $myinitfile[$a+2].substring( $myinitfile[$a+2].indexof("=")+2, $myinitfile[$a+2].length - ($myinitfile[$a+2].indexof("=")+3)) 
  37.          $personalarray.Comment = $Scope.Get_Item("$ip"
  38.         $temporaire +=$personalarray 
  39.         }     
  40.          
  41. $temporaire  
  42. }      
  43.  
  44. function isIPAddress($object) {   
  45. [Boolean]($object -as [System.Net.IPAddress]) 
  46.  
  47.  
  48. function NetSHScopeDHCP($servername){ 
  49. $cmdline = "cmd /c netsh dhcp server show all > c:\temp\DumpAllscope.txt" 
  50. $RemoteExec = (Get-WmiObject -List -ComputerName $servername | where {$_.name -eq 'win32_process'}).create($cmdline
  51. Start-Sleep -Seconds
  52. Move-Item \\$servername\c$\temp\DumpAllscope.txt c:\temp -Force 
  53. if (Test-Path "c:\temp\DumpAllscope$servername.txt"){ Remove-Item "c:\temp\DumpAllscope$servername.txt"
  54. Rename-Item c:\temp\DumpAllscope.txt "DumpAllscope$servername.txt" -Force 
  55.  
  56.  
  57.  
  58. #Get-DhcpScope MyDHCPServer 

This script permits to get informations (or export) DHCP Scope informations from remote DHCP server.

It was tested on Windows DHCP server 2000 / 2003, not tested on Windows 2008.

Pre requist needed:

Get Full Windows 2000 Resource Kit , install it

Copy dhcpobjs.dll to $Env:Windir\system32\ , and register it with cmdline REGSVR32.exe $env:windir\system32\dhcpobjs.dll /s

or download dhcpobjs.zip on http://www.dynawell.com/download/reskit/microsoft/win2000/dhcpobjs.zip

extract dhcpobjs.zip , execute dhcpobjs.exe and follow instruction.

Syntax :

PS c:\MY Scripts>  . .\Get-dhcpscope.ps1

PS c:\MY Scripts>  Get-dhcpscope MYDHCPServer

Subnet              NoOfAddressesinUse  NoOffreeAddresses   Comment
------                        ------------------            -----------------           -------
10.19.115.0                      0                           103               GRAND NORD
10.248.3.16                      10                         4                   PETIT EST
10.248.3.80                      9                           1                   CENTRE COMMERCI ...
10.248.5.176                    12                         8                   OUEST VENTEUX
10.248.8.128                    100                       0                   SUD MEDITTERANE ...
10.248.12.144                  0                           4                   MA MAISON

To export on CSV File

PS c:\MY Scripts>  Get-dhcpscope MYDHCPServer | export-csv .\MyDHCPServer-AllScope.csv

To report on HTML

PS c:\MY Scripts>  Get-dhcpscope MYDHCPServer | ConvertTo-HTML | Out-File .\MyDHCPServer-AllScope.HTML

 

Contact

If you want more info or if you want an evolution, please contact me at:   sylvain . lesire AT powershell . com

Copyright 2012 PowerShell.com. All rights reserved.