Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/classes/public/DnsRecord.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}
}
18 changes: 14 additions & 4 deletions src/functions/public/Resolve-DnsHost.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 @()
}
}
23 changes: 13 additions & 10 deletions tests/Dns.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,30 @@ Describe 'Dns' {
Context 'Resolve-DnsHost' {
It 'Test record <Name> - should exist <expected>' -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
}
Expand Down
Loading