PowerShell utilise la puissance du Framework .Net et permet donc d’utiliser les classes de celui-ci avec une très grande facilité dans vos scripts.

Dans cet exemple, je vais vous montrer comment faire une résolution DNS à partir de la classe Net.Dns du Framework.

[System.Net.Dns]::GetHostentry("blog.paux.fr")

Le résultat sera (en fonction si votre entrée est un simple alias ou un vrai nom d’hôte) :

HostName                  Aliases            AddressList
--------                  -------            -----------
dedibox2.jeanb-net.com    {}                 {88.191.92.204}

On peut biensûr décorer un peu tout ça en pipant une liste de nom d’hôtes depuis un fichier, rediriger ça dans une grille via out-gridview (nouveauté PS2.0 !). (A noter que si vous voulez non pas avoir le nom d’hôte mais vraiment votre input, vous pouvez le rajouter via la commande Add-Member, testez avec et sans vous verrez).

gc .\hosts.txt |% { [System.Net.Dns]::GetHostentry($_) | Add-Member -Name Host -MemberType NoteProperty -Value $_ -PassThru } | Select Host,AddressList | out-gridview

image

On peut biensûr faire des résolutions inverses (IPv4 ou IPv6).

[System.Net.Dns]::GetHostentry("2001:4860:a005::68";)

La classe Dns du Framework reste sommaire, il n’est pas possible de résoudre autre chose que ça (impossible de récupérer les NS, MX, SOA, TXT etc.) mais on peut pour cela utiliser d’autres classes qui implémentent un autre resolver (ou utilisant de manière plus approfondie celui de Windows)

Tags Tags: , ,
Categories: Français, Microsoft
Posted By: Jean-Benoit Paux
Last Edit: 04 Aug 2009 @ 12 19 AM

EmailPermalinkComments (0)

PowerShell uses the power of .Net Framework thus you can leverage .Net classes in your PowerShell scripts.

In this example I’ll show you how you can perform DNS lookups using the Net.Dns class :

[System.Net.Dns]::GetHostentry(“blog.paux.fr”)

The result will be (depending if your input is a A or CNAME) :

HostName                  Aliases            AddressList
--------                  -------            -----------
dedibox2.jeanb-net.com    {}                 {88.191.92.204}

You can obviously pipe a list of hostname to this command and if you need to keep the original name you can add it to the resulted object and display it in a grid (new to PS2.0 !):

gc .\hosts.txt |% { [System.Net.Dns]::GetHostentry($_) | Add-Member -Name Host -MemberType NoteProperty -Value $_ -PassThru } | Select Host,AddressList | out-gridview

image

You can of course do the same with an IP address (or an IPv6 address):

[System.Net.Dns]::GetHostentry("2001:4860:a005::68")

The class in .Net Framework stay basic so you can’t resolve NS, SOA, MX or TXT records for example but you can try to use other classes as this implementation of a resolver in C# or using this one (that use the internal DNS resolver of Windows)

Tags Tags: , ,
Categories: English, Microsoft
Posted By: Jean-Benoit Paux
Last Edit: 04 Aug 2009 @ 12 17 AM

EmailPermalinkComments (0)
\/ More Options ...
Change Theme...
  • Users » 1
  • Posts/Pages » 18
  • Comments » 11
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

Certifications



    No Child Pages.