Archive for February, 2018

Pull DHCP info using PowerShell

This short script will get all the DHCP servers authorized in your Windows domain, and pull all the scopes and IPs. It exports these to two separate CSV files.

$DHCPServers = Get-DhcpServerInDC
If (Test-Path -Path $env:TEMP\Scopes.csv) {Remove-Item $env:TEMP\Scopes.csv}
If (Test-Path -Path $env:TEMP\Leases.csv) {Remove-Item $env:TEMP\Leases.csv}
If (Test-Path -Path $env:TEMP\Leases.csv) {Remove-Item $env:TEMP\DNSExport.csv}
foreach ($_ In $DHCPServers) {
    Get-DhcpServerv4Scope -ComputerName $_.DnsName | Select-Object -Property ScopeId,SubnetMask,Name,State,StartRange,EndRange,LeaseDuration,Description,type | Export-Csv $env:TEMP\Scopes.csv -Append -NoTypeInformation
    Get-DhcpServerv4Scope -ComputerName $_.DnsName | Get-DhcpServerv4Lease -ComputerName $_.DnsName -AllLeases | Select-Object -Property IPAddress,ScopeId,ClientId,HostName,AddressState,LeaseExpiryTime,ClientType,Description,DnsRegistration,DnsRR,ServerIP    | Export-Csv $env:TEMP\Leases.csv -Append -NoTypeInformation

The formatting for the script is a little off.  I need to get a code display plugin, but I am too lazy.

DNS PowerShell one liner

This is a “one liner” that pulls all the DNS Entries for a particular zone, including the IPv4 and IPv6.  If you don’t care about the IPv6, you can remove that segment of the code.

Get-DnsServerResourceRecord –ComputerName <DNSServerName> –ZoneName <YourZoneName> | Select-Object DistinguishedName,HostName,@{Name=’IPv4Address’;Expression={$_.RecordData.IPv4Address.IPAddressToString}},@{Name=’IPv6Address’;Expression={$_.RecordData.IPv6Address.IPAddressToString}},RecordType,Timestamp,TimeToLive | Export-Csv $env:TEMP\DNSExport.csv –NoTypeInformation

It is amazing how much you can do with just one line (even if the line is obnoxiously long.)