ps/Modules/Alkami.DevOps.SystemEngineering/Public/Get-DnsByIP.ps1
2023-05-30 22:51:22 -07:00

60 lines
1.9 KiB
PowerShell

function Get-DnsByIP {
<#
.SYNOPSIS
Retrieves all DNS records for a given IP address from Active Directory DNS.
.DESCRIPTION
Retrieves all DNS records for a given IP address from Active Directory DNS.
.PARAMETER DNSServer
[string] The DNS server to query.
.PARAMETER IPAddress
[string] The IP Address to query against.
.EXAMPLE
Get-DnsByIP -TargetIP 192.168.4.55 -DnsServer 'dc314212.fh.local'
#>
[CmdletBinding()]
[OutputType([System.Object[]])]
param(
[Alias("DomainController")]
[string]$DNSServer = "localhost",
[Parameter(Mandatory)]
[Alias("TargetIP")]
[string]$IPAddress
)
$logLead = (Get-LogLeadName)
#Verify that the 'Get-DnsServerZone' command is available on the workstation
if ($null -ne (Get-Command -Name Get-DnsServerZone -ErrorAction SilentlyContinue)) {
#Get all of the DNS Zones
$zones = @(Get-DnsServerZone -ComputerName $DNSServer).ZoneName
#Is the $zones array empty?
if (Test-IsCollectionNullOrEmpty -Collection $zones) {
Write-Host "$logLead : No zones found"
return
}
#Create an array
$resources = @()
#Iterate through each zone and add to $resources array if it matches the $IPAddress parameter value
foreach ($zone in $zones) {
$resources += (Get-DnsServerResourceRecord -ZoneName $zone -ComputerName $DNSServer) | Where-Object {$_.RecordData.IPv4Address.IPAddressToString -eq $IPAddress}
}
return $resources
}
else {
Write-Error "$logLead : The command 'Get-DnsServerZone' does not exist on this system. Please verify you are running this on a Domain Controller under an admin accont"
}
}