function Show-DnsView {
    $dnsIPv4Addresses = Get-DnsClientServerAddress -AddressFamily IPV4 | Select-Object -ExpandProperty ServerAddresses | Select-Object -Unique
    $dnsResults = @()

    foreach ($dns in $dnsIPv4Addresses) {
        $dnsTest = Test-Connection -ComputerName $dns -Count 1 -ErrorAction SilentlyContinue
        if ($dnsTest) {
            $dnsResults += "$dns est OP"
        } else {
            $dnsResults += "$dns est DOWN"
        }
    }

    Write-Host "Le/Les serveur(s) DNS est/sont : $($dnsIPv4Addresses -join '; ')"
    Write-Host "Résultats des tests de connectivité :"
    $dnsResults | ForEach-Object { Write-Host $_ }
    Start-Sleep 3
}

function Change-Dns {
    $networkInterfaces = Get-NetAdapter
    $dnsChoisi = Read-Host -Prompt "Choisir un dns : [1]10.1.1.1 ; [2]8.8.8.8 :"
  if ($dnsChoisi -eq "1") {
    $dnsAddress = "10.1.1.1"
} else {
    $dnsAddress = "8.8.8.8"
}


    foreach ($interface in $networkInterfaces) {
        Set-DnsClientServerAddress -InterfaceAlias $interface.InterfaceAlias -ServerAddresses $dnsAddress
        Write-Host "Serveurs DNS appliqués à l'interface : $($interface.InterfaceAlias)"
    }
    Start-Sleep 3
}

function Create-DnsRecord {
    $dnsChoisis = Read-Host -Prompt "Choisir un type d'enregistrement DNS : [1]A ; [2]CNAME :"
    $nomDuPC = Read-Host -Prompt "Nom de l'ordinateur CIBLE"
    $ipAddress = Read-Host -Prompt "Adresse IP"

    Switch ($dnsChoisis) {
        "1" { Invoke-Command -ComputerName $nomDuPC -ScriptBlock { param($ip) Add-DnsServerResourceRecordA} -ArgumentList $ipAddress }
        "2" { Invoke-Command -ComputerName $nomDuPC -ScriptBlock { param($name) Add-DnsServerResourceRecordCNAME}}
    }
    Start-Sleep 3
}

function Test-Dns {
    $dnsTestChoice = Read-Host "IP Cible"
    Resolve-DnsName -Name $dnsTestChoice
    Start-Sleep 3
}

function Remove-DnsRecord {
    $dnsAsupprimer = Read-Host -Prompt "Choisir un type d'enregistrement DNS : [1]A ; [2]CNAME"
    $nomDuPC = Read-Host -Prompt "Nom de l'ordinateur CIBLE"

    Switch ($dnsAsupprimer) {
        "1" { Invoke-Command -ComputerName $nomDuPC -ScriptBlock { 
                $nom = Read-Host "Name"
                Remove-DnsServerResourceRecord -Name $nom -RRType "A"
        } }
        "2" { Invoke-Command -ComputerName $nomDuPC -ScriptBlock {
                 $nom = Read-Host "Name"
                Remove-DnsServerResourceRecord -Name $nom -RRType "CName"
        } }
    }
    Start-Sleep 3
}

function Show-DnsErrors {
    $nomDuPC = Read-Host -Prompt "Nom de l'ordinateur CIBLE"
    Invoke-Command -ComputerName $nomDuPC -ScriptBlock {Get-EventLog -LogName "DNS Server" -EntryType Error -Newest 10}
    Start-Sleep 3
}

while ($true) {
    $choix = Read-Host -Prompt "Que voulez-vous faire ? [1]Voir DNs Actuel [2]Changer le DNS actuel [3]Créer un enregistrement DNS [4]Tester DNS [5]Supprimer un enregistrement DNS [6]Voir les erreurs [7]Exit"
    
    Switch ($choix) {
        "1" { Show-DnsView }
        "2" { Change-Dns }
        "3" { Create-DnsRecord }
        "4" { Test-Dns }
        "5" { Remove-DnsRecord }
        "6" { Show-DnsErrors }
        "7" { exit }
        Default { Write-Host "Choix invalide. Veuillez réessayer." }
    }
}
