Information Technology Grimoire

Version .0.0.1

IT Notes from various projects because I forget, and hopefully they help you too.

Hostname < > IP Converter

The Script

# Load required assemblies
Add-Type -AssemblyName PresentationFramework

# Define XAML for GUI using a here-string
$xaml = @"
<Window x:Class='System.Windows.Window'
        xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
        xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
        Title='IP-Domain Converter' Height='450' Width='750'>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width='*' />
            <ColumnDefinition Width='*' />
        </Grid.ColumnDefinitions>
        <Label Name='LabelIP' Content='Paste IP Addresses:' HorizontalAlignment='Center' Margin='0,10,0,0' VerticalAlignment='Top' Width='200' Grid.Column='0'/>
        <Button Name='ConvertToHostname' Content='To Hostname' Width='200' Height='30' VerticalAlignment='Top' Margin='0,40,0,0' HorizontalAlignment='Center' Grid.Column='0' />
        <TextBox Name='InputBox' Height='300' Width='300' VerticalAlignment='Top' Margin='0,80,0,0' AcceptsReturn='True' TextWrapping='Wrap' VerticalScrollBarVisibility='Auto' HorizontalAlignment='Center' Grid.Column='0'/>

        <Label Name='LabelDomain' Content='Paste Hostnames:' HorizontalAlignment='Center' Margin='0,10,0,0' VerticalAlignment='Top' Width='200' Grid.Column='1'/>
        <Button Name='ConvertToIP' Content='To IP' Width='200' Height='30' VerticalAlignment='Top' Margin='0,40,0,0' HorizontalAlignment='Center' Grid.Column='1' />
        <TextBox Name='OutputBox' Height='300' Width='300' VerticalAlignment='Top' Margin='0,80,0,0' AcceptsReturn='True' TextWrapping='Wrap' VerticalScrollBarVisibility='Auto' HorizontalAlignment='Center' Grid.Column='1'/>
    </Grid>
</Window>
"@

# Load the XAML
$window = [Windows.Markup.XamlReader]::Parse($xaml)

# Define events
$buttonConvertToHostname = $window.FindName('ConvertToHostname')
$buttonConvertToIP = $window.FindName('ConvertToIP')
$inputBox = $window.FindName('InputBox')
$outputBox = $window.FindName('OutputBox')

# Define DNS servers to use
$dnsServers = @('8.8.8.8', '8.8.4.4') # Example DNS servers

function Resolve-DnsNameCustom {
    param (
        [string]$Name,
        [string[]]$DnsServers,
        [string]$QueryType = 'A'
    )
    $result = $null
    foreach ($server in $DnsServers) {
        try {
            $queryOptions = @{
                Name = $Name
                Type = $QueryType
                Server = $server
            }
            $result = Resolve-DnsName @queryOptions -ErrorAction Stop
            break
        } catch {
            # Continue to the next DNS server if the current one fails
        }
    }
    return $result
}

$buttonConvertToHostname.Add_Click({
    $outputBox.Text = ''
    foreach ($ip in ($inputBox.Text -split "`r`n")) {
        try {
            $hostnameResult = Resolve-DnsNameCustom -Name $ip -DnsServers $dnsServers -QueryType 'PTR'
            if ($hostnameResult) {
                $hostname = $hostnameResult.NameHost
            } else {
                $hostname = "unknown host"
            }
        } catch {
            $hostname = "unknown host"
        }
        $outputBox.AppendText("$hostname`r`n")
    }
})

$buttonConvertToIP.Add_Click({
    $inputBox.Text = ''
    foreach ($domain in ($outputBox.Text -split "`r`n")) {
        try {
            $ipAddressesResult = Resolve-DnsNameCustom -Name $domain -DnsServers $dnsServers -QueryType 'A'
            if ($ipAddressesResult) {
                $ipAddresses = $ipAddressesResult.IPAddress
                $ipString = [string]::Join(",", $ipAddresses)
            } else {
                $ipString = "unknown host"
            }
        } catch {
            $ipString = "unknown host"
        }
        $inputBox.AppendText("$ipString`r`n")
    }
})

# Show the window
$window.ShowDialog()
Last updated on 27 Aug 2023
Published on 27 Aug 2023