PowerShell: Get DHCP Information

To get the information like how many computers are in the domain, how many are not in the domain, how many computers are responsive. I wrote the following script.

1) Create a txt file named dhcp_scope.txt under the Windows folder in the DHCP server, and add the scope into the file. e.g.

2) Create a batch file name dhcp_get.bat under the Windows folder in the DHCP server.

del /f c:\windows\dhcp_ip.txt
Set scope=c:\windows\dhcp_scope.txt
for /f %%a in (%scope%) do (netsh dhcp server scope %%a show clients 1 >> c:\windows\dhcp_ip.txt)

3) Create a batch file named get_dhcp.bat in your local computer:

psexec \\dhcp-server-name -user test.domain\administrator –p ****** dhcp_get.bat
copy /y \\dhcp-server-name\c$\windows\dhcp_ip.txt .

4) Create a powershell script named find_dhcp_info.ps1 in your local computer as following:

cmd /c get_dhcp.bat

Remove-Item domain_pc.txt
Remove-Item domain_pc_ip.txt
Remove-Item non_domain_pc.txt
Remove-Item non_domain_pc_ip.txt
Remove-Item ip_pingable.txt

$dhcp = Get-Content dhcp_ip.txt
$n1 = 0
$n2 = 0
$n4 = 0
$n5 = 0
$n6 = 0

foreach ($i in $dhcp)
if ($i -match “” -and $i -match “test.domain” -and $i -notmatch “INACTIVE”)
Echo $i >> domain_pc.txt
$ip1 = $i.split(”  “)[0]
Echo $ip1 >> domain_pc_ip.txt
$n1 = $n1 + 1

# does not count the reserved IP address
if ($i -match “” -and $i -match “\b[A-Z0-9]+\.\Z” -and $i -notmatch “NEVER EXPIRES” -and $i -notmatch “INACTIVE”)
Echo $i >> non_domain_pc.txt
$ip2 = $i.split(”  “)[0]
Echo $ip2 >> non_domain_pc_ip.txt
$n2 = $n2 + 1

Write-Host *******************Real time info retrieved from the DHCP******************* `n
Write-Host $n1 machines are in Domain.”(See details in domain_pc.txt and domain_pc_ip.txt)”
Write-Host -BackgroundColor yellow $n2 machines are not in Domain. “(See details in non_domain_pc.txt and non_domain_pc_ip.txt)” `n

Echo (Get-Content domain_pc_ip.txt) > all_ip.txt
Echo (Get-Content non_domain_pc_ip.txt) >> all_ip.txt

$all_ip = Get-Content all_ip.txt
$total = $all_ip | Measure-Object -Line
$all = $total.Lines

foreach ($ip in $all_ip)
$total.lines = $total.lines – 1
$result = Get-WMIObject -query “select StatusCode from Win32_PingStatus where Address = ‘$ip'”
if ($result.statuscode -eq 0)
echo $ip >> ip_pingable.txt
$n4 = $n4 + 1
else {$n5 = $n5 + 1}
Write-Progress -activity “Please wait, checking the responsive machines ……” -status “Progress  –>” -percentcomplete (($all – $total.lines)/$all*100)

$n6 = $n4 + $n5

Write-Host $n6 computers have IP in DHCP. “(See details in all_ip.txt)”
Write-Host -BackgroundColor Yellow $n4 computers are responsive. “(See details in ip_pingable.txt)”
Write-Host $n5 computers are unresponsive.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s