diff --git a/src/classes/public/DnsRecord.ps1 b/src/classes/public/DnsRecord.ps1 new file mode 100644 index 0000000..9c6b05a --- /dev/null +++ b/src/classes/public/DnsRecord.ps1 @@ -0,0 +1,37 @@ +class DnsRecord { + [string]$Name + [string]$Type + [int]$TTL + [string]$Section + [string]$IPAddress + [string]$Address + [string]$QueryType + [string]$IP4Address + [string]$IP6Address + [string]$CharacterSet + [int]$DataLength + + DnsRecord() {} + + DnsRecord([string]$Name, [string]$Type, [string]$IPAddress) { + $this.Name = $Name + $this.Type = $Type + $this.QueryType = $Type + $this.IPAddress = $IPAddress + $this.Address = $IPAddress + $this.Section = 'Answer' + $this.CharacterSet = 'Unicode' + $this.TTL = 60 # Default TTL since we can't get actual TTL from System.Net.Dns + + # Set IP4Address or IP6Address based on the IP type + if ($IPAddress -match ':') { + # IPv6 + $this.IP6Address = $IPAddress + $this.DataLength = 16 + } else { + # IPv4 + $this.IP4Address = $IPAddress + $this.DataLength = 4 + } + } +} \ No newline at end of file diff --git a/src/functions/public/Resolve-DnsHost.ps1 b/src/functions/public/Resolve-DnsHost.ps1 index b735aaa..c04d9c1 100644 --- a/src/functions/public/Resolve-DnsHost.ps1 +++ b/src/functions/public/Resolve-DnsHost.ps1 @@ -5,14 +5,15 @@ .DESCRIPTION This function resolves a hostname to an IP address using the System.Net.Dns class. + Returns detailed DNS record information similar to Windows' Resolve-DnsName. .EXAMPLE - Resolve-DnsHost -HostName 'google.com' + Resolve-DnsHost -Name 'google.com' .OUTPUTS - [DnsHost] + [DnsRecord[]] #> - [OutputType([DnsHost])] + [OutputType([DnsRecord[]])] [CmdletBinding()] param ( # The name of the host to resolve @@ -26,9 +27,18 @@ try { $entry = [System.Net.Dns]::GetHostEntry($Name, $AddressFamily) - return [DnsHost]::new($entry.HostName, $entry.Aliases, $entry.AddressList) + $results = @() + + foreach ($address in $entry.AddressList) { + $recordType = if ($address.AddressFamily -eq 'InterNetwork') { 'A' } else { 'AAAA' } + $dnsRecord = [DnsRecord]::new($entry.HostName, $recordType, $address.ToString()) + $results += $dnsRecord + } + + return $results } catch { Write-Debug "Failed to resolve DNS for [$Name]" Write-Debug $_ + return @() } } diff --git a/tests/Dns.Tests.ps1 b/tests/Dns.Tests.ps1 index 958051e..1b22a50 100644 --- a/tests/Dns.Tests.ps1 +++ b/tests/Dns.Tests.ps1 @@ -24,27 +24,30 @@ Describe 'Dns' { Context 'Resolve-DnsHost' { It 'Test record - should exist ' -ForEach @( @{ - Name = 'google.com' + Name = '127.0.0.1' Expected = $true }, @{ - Name = 'example.com' + Name = 'localhost' Expected = $true }, @{ - Name = 'nonexistent.example' + Name = 'nonexistent.invalid.domain.test' Expected = $false } ) { - $result = Resolve-DnsHost -Name $Name -ErrorAction Stop - LogGroup 'Results' { - Write-Host "$($result | Format-Table -AutoSize | Out-String)" - } + $result = Resolve-DnsHost -Name $Name -ErrorAction SilentlyContinue + Write-Host "Results: $($result | Format-Table -AutoSize | Out-String)" if ($Expected) { $result | Should -Not -BeNullOrEmpty - $result | Should -BeOfType [DnsHost] - $result.Name | Should -Be $Name - $result.AddressList.Count | Should -BeGreaterThan 0 + $result | Should -BeOfType [DnsRecord] + $result[0].Name | Should -Not -BeNullOrEmpty + $result[0].IPAddress | Should -Not -BeNullOrEmpty + $result[0].Type | Should -BeIn @('A', 'AAAA') + $result[0].Section | Should -Be 'Answer' + $result[0].CharacterSet | Should -Be 'Unicode' + $result[0].TTL | Should -BeGreaterThan 0 + $result[0].DataLength | Should -BeIn @(4, 16) } else { $result | Should -BeNullOrEmpty }