Archive

Archive for March, 2012

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
%d bloggers like this: