Archive

Archive for the ‘DHCP’ Category

Export DHCP leases to HTML– version 2

An updated version of the previous script, now with the ability to export the MAC to the HTML as well as dumping all scopes to individual HTML and .csv files

param(
	[Parameter(Mandatory=$true)][String]$DHCPServer,
	[Parameter(Mandatory=$true)][string]$Scope,
	[switch]$IncludeMAC
	)

$sort = "DNS" #output file sorting, can also be "DNS" or "MAC" (MAC only if includeMAC switch selected

#####
#Functions
#####
Function DumpDHCPScope (){
Param(
	$subScope
)

$scopedump = (netsh dhcp server $DHCPServer scope $subScope show clients 1)
$lines = @()
foreach ($i in $scopedump){
	if ($i -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"){
		If ($i -match "[0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}"){	
			$lines += $i.Trim()
		}
	}
}
$csvfile = @()
foreach ($l in $lines){
	$Row = "" | select IP, DNS
	If ($IncludeMAC -eq $true){$Row = "" | select IP, DNS, MAC}
	#$l = $l -replace '[0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}', '' #removes MAC address
	$l = $l -replace ' - ',','
	$l = $l -replace '\s{4,}',''
	$l = $l -replace '--','-'
	$l = $l -replace '-D-','-'
	$l = $l -replace '[-]{1}\d{1,2}[/-]\d{1,2}[/-]\d{4}',''
	$l = $l -replace '\d{1,2}[:]\d{2}[:]\d{2}',''
	$l = $l -replace 'AM',''
	$l = $l -replace 'PM',''
	$l = $l -replace '[,][-]',''
	$l = $l -replace '\s{1}',','
	$l = $l -replace '[,]{2,4}',','
	$l = $l -replace '[,][-]',''
	#$l = $l + "`n"
	$Row.IP = ($l.Split(","))[0]
	#$Row.SubNetMask = ($l.Split(","))[1]
	if ($IncludeMAC -eq $true){$Row.MAC = ($l.Split(","))[2]}
	$Row.DNS = ($l.Split(","))[3]
	$csvfile += $Row
}

$csvfile | sort-object $sort | Export-Csv "C:\TMP\ScriptOut\$subScope-DHCPOut-List.csv" -NoTypeInformation

$a = "<style>"
$a = $a + "body {margin: 10px; width: 600px; font-family:arial; font-size: 12px;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color: rgb(179,179,179);align='left';}"
$a = $a + "TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color: white;}"
$a = $a + "</style>"

"Please contact theadminguy@wordpress.com in case of any questions" | Out-File "C:\TMP\ScriptOut\$subScope-DHCP-List.html"
$csvfile | sort-object $sort | ConvertTo-HTML -head $a | Out-File -Append "C:\TMP\ScriptOut\$subScope-DHCP-List.html"
get-date -Format dd/MM/yyyy@HH:mm:ss  | Out-File -Append "C:\TMP\ScriptOut\$subScope-DHCP-List.html"

}#End Function DumpDHCPScope

###
#Main Script
###

if ($Scope -eq "ALL"){
	$scopelist = @()
	$AllScopes = (netsh dhcp server $DHCPserver show scope 1)
	foreach ($i in $AllScopes){
		if ($i -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"){
			$scopelist += $i.Trim()
		}
	}
	Foreach ($i in $scopelist){
		$i = $i -replace '\s{2,}',''
		$i = $i -replace '- ',','
		$i = $i -replace '-',','
		DumpDHCPScope $i.Split(",")[0]
	}
}
Else {
	DumpDHCPScope $scope
}

Example commandlines:

Export-dhcptohtml.ps1 –dhcpserver 1.1.1.1 –scope 10.10.10.10 –includemac

This will dump the DHCP scope 10.10.10.10 including the MAC to an HTML file as well as a csv file.

Export-dhcptohtml.ps1 –dhcpserver 1.1.1.1 -scope ALL

This will dump all scopes on the dhcp server to individual HTML and .csv files.

Do post a comment, suggestion or request.

[Update 29-05-2012] – Added missing – (dash) in second example

/theadminguy

Categories: DHCP, Powershell Scripts

Export DHCP scopes and their address pools to a csv file

Because I love regular expressions, and I had a need for it, I have modified my previously posted DHCP export script:

$a = (netsh dhcp server 1.1.1.1 show scope)

$lines = @()
#start by looking for lines where there is IP present
foreach ($i in $a){
    if ($i -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"){
            $lines += $i.Trim()
    }
}

$csvfile = @()
$lines2 = @()
foreach ($l in $lines){
    $Row = "" | select Subnet,SubNetMask,ScopeStart,ScopeEnd,Location
    $l = $l + "`n"
    $l = $l -replace '-Active',''
    $l = $l -replace '-',','
    $l = $l -replace '\s',''
    $Row.Subnet = ($l.Split(","))[0]
    $c = $Row.Subnet
    $Row.SubNetMask = ($l.Split(","))[1]
    $Row.Location = ($l.Split(","))[2]
    $b = (netsh dhcp server 1.1.1.1 scope $c show iprange)
    foreach ($i2 in $b){
        if ($i2 -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" -and $i2 -notmatch 'Changed the current scope context to' `
            -and $i2 -notmatch 'No of IP Ranges : 1 in the Scope'){
            $lines2 += $i2.Trim()
        }
    }
    Foreach ($l2 in $lines2){
        $l2 = $l2 -replace '-',','
        $l2 = $l2 -replace '\s',''
        $Row.ScopeStart = ($l2.Split(","))[0]
        $Row.ScopeEnd = ($l2.Split(","))[1]
    }
    $csvfile += $Row
}    
$csvfile | sort-object Subnet | Export-Csv "C:\Users\Public\Documents\DHCPExport.csv"

The script put all the server scopes into a variable and then processes it.

As the netsh output is filled with headers and other stuff, quite a number of trims and –replacements are done.

The whole thing is then put into a csv file.

As usual any hints and pointers are welcomed.

/theadminguy

Export DHCP leases to html using powershell – update

There was a minor quirk in the original script, as lease dates where being mixed with hostnames in the final output html. As it turns out it was all a matter of what date it was, when I created the script 🙂

Line 21 in the original:

$l = $l -replace '[-]{1}\d{2}[/]\d{2}[/]\d{4}',''

matches dates in the following format: -xx/yy/zzzz but not -x/yy/zzzz or for that matter -xx/y/zzzz.

The correct –replace should have been:

$l = $l -replace '[-]{1}\d{1,2}[/]\d{1,2}[/]\d{4}',''

Thanks you for the comments, which was the final spark to get me to fix it in my production environment 🙂

Export DHCP leases to html using powershell

So I was in a need of having the active leases in my DHCP server exported to an web (html) page:

On the surface this would not present a problem, however as there is only one way to read the DHCP leases, netsh.exe, and the output format of this app is somewhat … I also got the chance to to play with regular expressions (love those) and <string> –replace

Just so everyone can share the pain, this is the netsh command:

netsh dhcp server 1.1.1.1 scope 1.1.1.0 show clients 1

And this is the output:

Changed the current scope context to 1.1.1.0 scope.

Type : N - NONE, D - DHCP B - BOOTP, U - UNSPECIFIED, R - RESERVATION IP
============================================================================================
IP Address      - Subnet Mask    - Unique ID           - Lease Expires        -Type -Name   
============================================================================================

1.1.1.5     - 255.255.0.0    - 00-ff-ff-ff-ff-ff   -10/13/2009 12:33:16 AM  -D-  host01.domain.com
:::
::: Lot of lines here
:::
1.1.1.227   - 255.255.0.0    - 00-ff-ff-ff-ff-ff   -10/13/2009 12:42:06 AM  -D-  host10.domain.com

No of Clients(version 4): 355 in the Scope : 1.1.1.0.

Command completed successfully.

So is there any logic here? Double tabs, single tabs from which I can split? No. only spaces…. and hyphens.. and useless data

As I only needed the IP and hostnames in a html table, this is what I ended up with in powershell:

$a = (netsh dhcp server 1.1.1.1 scope 1.1.1.0 show clients 1)

$lines = @()
#start by looking for lines where there is both IP and MAC present:
foreach ($i in $a){
    if ($i -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"){
        If ($i -match "[0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}"){    
            $lines += $i.Trim()
        }
    }
}
$csvfile = @()
#Trim the lines for uneeded stuff, leaving only IP, Subnet mask and hostname.
foreach ($l in $lines){
    $Row = "" | select Hostname,IP
    $l = $l -replace '[0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}', ''
    $l = $l -replace ' - ',','
    $l = $l -replace '\s{4,}',''
    $l = $l -replace '--','-'
    $l = $l -replace '-D-','-'
    $l = $l -replace '[-]{1}\d{2}[/]\d{2}[/]\d{4}',''
    $l = $l -replace '\d{1,2}[:]\d{2}[:]\d{2}',''
    $l = $l -replace 'AM',''
    $l = $l -replace 'PM',''
    $l = $l -replace '\s{1}',''
    $l = $l + "`n"
    $l = $l -replace '[,][-]',','
    $Row.IP = ($l.Split(","))[0]
    #Subnet mask not used, but maybe in a later version, so let's leave it in there:
    #$Row.SubNetMask = ($l.Split(","))[1]
    $Row.Hostname = ($l.Split(","))[2]
    $csvfile += $Row
}

#let create a csv file, in case we need i later..
$csvfile | sort-object Hostname | Export-Csv "Out_List.csv"

#Create the HTML formating
$a = "<style>"
$a = $a + "body {margin: 10px; width: 600px; font-family:arial; font-size: 12px;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color: rgb(179,179,179);align='left';}"
$a = $a + "TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color: white;}"
$a = $a + "</style>"

#And create HTML file...
Write-Host "Please contact theadmin@void.null for support" | Out-File "DHCPLeases.html"
$csvfile | sort-object Hostname | ConvertTo-HTML -head $a | Out-File -Append "DHCPLeases.html"

The resulting output, then looks like this:

HostName IP
host01.domain.com 1.1.1.5
host10.domain.com 1.1.1.227

which is what I wanted. Another great job by powershell.

I hope that this might prove useful to someone else, and a my thanks goes out to the powershell community and all the people who might recognize some of the code, no one mentioned, no one forgotten.

Please do drop a line if you have comments or suggestions to how the script could be optimized. Life is a learning curve and I love climbing 🙂

%d bloggers like this: