1

I have list of the servers, for them I'm used commands Resolve -DnsName, this command has different parameters and to receive all the data that I need (NS, MX, CNAME, A) records, I need to make multiple commands.

$servers = get-content "C:\Users\Olehsa\Desktop\ttt.txt"


    $a1 = foreach ($server in $servers) {

     [System.Net.Dns]::resolve($server)
    }

After every command I have a list with 1-3 columns.

#NS
$a2 = foreach ($server in $servers) {

    Resolve-DnsName -Name $server -Type NS -DnsOnly | select PrimaryServer, NameHost,NameExchange,Strings + $a3 

}

#MX
$a3 = foreach($server in $servers) {

    Resolve-DnsName -Name $server -Type MX -DnsOnly | select NameExchange
} 

#TXT
$a4 = foreach($server in $servers) {

    Resolve-DnsName -Name $server -Type TXT | select Strings
}

I receive this data:

HostName   Aliases AddressList    
--------   ------- -----------    
google.com {}      {172.217.22.46}

and this :

NameHost       NameExchange
-------------  --------       
              ns1.google.com             
              ns4.google.com             
              ns3.google.com             
              ns2.google.com   

And the question is, How I can merge the to one big list with multiple columns?

P.s. Without using the PS-objects, because if I using it, I'm putting all data to 1 cell. I have the list with 3 columns, I need to add one more column with

  • 1
    But there are different columns after different parameters. E. g. NS records have 'NameHost' and MX records haven't 'NameHost' but have 'NameExchange'. How you gonna merge different columns? – Vadim Sep 21 '18 at 11:29
  • I just want to add this columns to the whole list, where will be NameHost and NameExchange, – Oleg Savchuk Sep 21 '18 at 11:45
  • You are quite ambigous, you should specify what you have, what you get (with what code) and what you expect/want by [editing](https://serverfault.com/posts/932023/edit) your question. – LotPings Sep 21 '18 at 12:25
  • Already changed the questions, I think it will be more clear for you – Oleg Savchuk Sep 21 '18 at 12:37

1 Answers1

2

I'd put this into one ForEach and
when getting multiple results for a single field -Join them

## Q:\Test\2018\09\21\SF_932023.ps1
$servers = get-content "C:\Users\Olehsa\Desktop\ttt.txt"

$result = foreach ($server in $servers) {
    [PSCustomObject]@{
        Server        = $server
        NameHost      = ((Resolve-DnsName -Name $server -Type NS -DnsOnly).NameHost|Sort) -Join ', '
        NameExchange  = ((Resolve-DnsName -Name $server -Type MX -DnsOnly).NameExchange|Sort) -Join ', '
        Strings       = ((Resolve-DnsName -Name $server -Type TXT).Strings) -Join ', '
     }
}

$result|fl

Sample output:

Server       : google.com
NameHost     : ns1.google.com, ns2.google.com, ns3.google.com, ns4.google.com
NameExchange : alt1.aspmx.l.google.com, alt2.aspmx.l.google.com, alt3.aspmx.l.google.com, alt4.aspmx.l.google.com, aspmx.l.google.com
Strings      : docusign=05958488-4752-4ef2-95eb-aa7ba8a3bd0e, facebook-domain-verification=22rm551cu4k0ab0bxsw536tlds4h95, v=spf1 include:_spf.google.com ~all
LotPings
  • 1,015
  • 7
  • 12