fr:powershell:dns
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| fr:powershell:dns [2021/04/05 09:24] – [Le piège] lonclegr | fr:powershell:dns [2021/04/05 11:26] (Version actuelle) – [Script final] lonclegr | ||
|---|---|---|---|
| Ligne 27: | Ligne 27: | ||
| <file powershell dns_v1.ps1 > | <file powershell dns_v1.ps1 > | ||
| Import-Csv -Path input_dns.csv | foreach { | Import-Csv -Path input_dns.csv | foreach { | ||
| - | nslookup $_.A_Record | + | nslookup $_.A_Record |
| } | } | ||
| </ | </ | ||
| + | |||
| + | On obtient des choses horribles. | ||
| + | |||
| + | < | ||
| + | nslookup : Non-authoritative answer: | ||
| + | At line:1 char:1 | ||
| + | + nslookup google.ca 8.8.8.8 | ||
| + | + ~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| + | + CategoryInfo | ||
| + | + FullyQualifiedErrorId : NativeCommandError | ||
| + | |||
| + | Server: | ||
| + | Address: | ||
| + | |||
| + | Name: google.ca | ||
| + | Addresses: | ||
| + | 172.217.13.131 | ||
| + | </ | ||
| + | |||
| + | Cela pose les problèmes suivants: | ||
| + | - Les résultats s' | ||
| + | - nslookup n'est pas une commande Powershell alors on n' | ||
| + | - Impossible d' | ||
| + | |||
| + | ===== Le bon outil ===== | ||
| + | |||
| + | Nous allons donc utiliser la commande DNS PowerShell: Resolve-Dns | ||
| + | |||
| + | <file powershell dns_v2.ps1 > | ||
| + | Import-Csv -Path input_dns.csv | foreach { | ||
| + | Resolve-DnsName $_.A_Record -Type A -Server 8.8.8.8 | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Cette fois-ci on obtient quelque chose de beaucoup plus sympa avec des objets que nous allons pouvoir manipuler. | ||
| + | |||
| + | < | ||
| + | Name | ||
| + | ---- | ||
| + | google.ca | ||
| + | google.com | ||
| + | google.fr | ||
| + | Resolve-DnsName : google.toto : DNS name does not exist | ||
| + | At line:2 char:7 | ||
| + | + | ||
| + | + | ||
| + | + CategoryInfo | ||
| + | + FullyQualifiedErrorId : DNS_ERROR_RCODE_NAME_ERROR, | ||
| + | </ | ||
| + | |||
| + | On obtient toujours une exception pour l' | ||
| + | |||
| + | |||
| + | <file powershell dns_v3.ps1> | ||
| + | Import-Csv -Path input_dns.csv | foreach { | ||
| + | $currentLine = $_ | ||
| + | $record = $currentLine.A_Record | ||
| + | try { | ||
| + | $result = Resolve-DnsName $record -Type A -Server 8.8.8.8 2> $null | ||
| + | Write-Host $result.getType().fullname | ||
| + | } catch { | ||
| + | Write-Host (" | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | On obtient des objets que nous allons pouvoir manipuler et exporter. | ||
| + | |||
| + | < | ||
| + | Microsoft.DnsClient.Commands.DnsRecord_A | ||
| + | Microsoft.DnsClient.Commands.DnsRecord_A | ||
| + | Microsoft.DnsClient.Commands.DnsRecord_A | ||
| + | Error with record: google.toto | ||
| + | |||
| + | Name | ||
| + | ---- | ||
| + | google.ca | ||
| + | google.com | ||
| + | google.fr | ||
| + | </ | ||
| + | |||
| + | ===== Script final ===== | ||
| + | |||
| + | On se rappelle ce que l'on souhaite: prendre tous les enregistrements DNS A demandés et s' | ||
| + | |||
| + | <file powershell dns_final.ps1> | ||
| + | $results = @() | ||
| + | |||
| + | Import-Csv -Path input_dns.csv | foreach { | ||
| + | $currentLine = $_ | ||
| + | $record = $currentLine.A_Record | ||
| + | try { | ||
| + | $result = Resolve-DnsName $record -Type A -Server 8.8.8.8 2> $null | ||
| + | Write-Host $result.getType().fullname | ||
| + | $exportLine = $result | Select-Object -Property Name, | ||
| + | $exportLine | Add-Member -MemberType NoteProperty -Name " | ||
| + | } catch { | ||
| + | Write-Host (" | ||
| + | $exportLine = New-Object psobject -Property @{ | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | } | ||
| + | } | ||
| + | $results += $exportLine | ||
| + | } | ||
| + | |||
| + | $results | Export-Csv -Path dnsPreCheck.csv -Delimiter "," | ||
| + | </ | ||
| + | |||
| + | Le résultat permet de mettre en valeur un domaine qui n'a pas d' | ||
| + | |||
| + | <csv> | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | </ | ||
fr/powershell/dns.1617629099.txt.gz · Dernière modification : de lonclegr
