Solved

Modify this Powershell script that captures DNS servers so it outputs to CSV

Posted on 2013-12-10
5
575 Views
Last Modified: 2013-12-20
Hi

I found the below script on the MS site that works well, however it displays the info in the console, is it possible someone could edit this so it outputs to a csv file, i tried stiicking an export-csv line at the end but that didnt work so there must be more to it.

Thanks

# Get-DNSandWINS.ps1 
# Written by Bill Stewart (bstewart@iname.com) 
 
#requires -version 2 
 
# Version history: 
# 
# 2.0 (2013-10-02) 
# * Added -Credential 
# * Output data only for IPv4 addresses 
# * Output separate row for each DNS server address 
# 
# 1.0 (2012-02-15) 
# * Initial version 
 
<# 
.SYNOPSIS 
Outputs the computer name, IP address(es), DNS server address(es), and WINS server addresses for one or more computers. 
 
.DESCRIPTION 
Outputs the computer name, IP address(es), DNS server address(es), and WINS server addresses for one or more computers. 
 
.PARAMETER ComputerName 
One or more computer names. This parameter accepts pipeline input. 
 
.PARAMETER Credential 
Specifies credentials for the WMI connection. 
 
.EXAMPLE 
PS C:\> Get-DNSandWINS server1 
Outputs information for server1. 
 
.EXAMPLE 
PS C:\> Get-DNSandWINS server1,server2 
Outputs information for server1 and server2. 
 
.EXAMPLE 
PS C:\> Get-DNSandWINS (Get-Content ComputerList.txt) 
Outputs information for the computers listed in ComputerList.txt. 
 
.EXAMPLE 
PS C:\> Get-Content ComputerList.txt | Get-DNSandWINS 
Same as previous example (Outputs information for the computers listed in ComputerList.txt). 
 
.EXAMPLE 
PS C:\> Get-DNSandWINS server1,server2 -Credential (Get-Credential) 
Outputs information for server1 and server2 using different credentials. 
#> 
 
param( 
  [parameter(ValueFromPipeline=$TRUE)] 
    [String[]] $ComputerName=$Env:COMPUTERNAME, 
    [System.Management.Automation.PSCredential] $Credential 
) 
 
begin { 
  $PipelineInput = (-not $PSBOUNDPARAMETERS.ContainsKey("ComputerName")) -and (-not $ComputerName) 
 
  # Outputs the computer name, IP address, and DNS and WINS settings for 
  # every IP-enabled adapter on the specified computer that's configured with 
  # an IPv4 address. 
  function Get-IPInfo($computerName) { 
    $params = @{ 
      "Class" = "Win32_NetworkAdapterConfiguration" 
      "ComputerName" = $computerName 
      "Filter" = "IPEnabled=True" 
    } 
    if ( $Credential ) { $params.Add("Credential", $Credential) } 
    get-wmiobject @params | foreach-object { 
      foreach ( $adapterAddress in $_.IPAddress ) { 
        if ( $adapterAddress -match '(\d{1,3}\.){3}\d{1,3}' ) { 
          foreach ( $dnsServerAddress in $_.DNSServerSearchOrder ) { 
            new-object PSObject -property @{ 
              "ComputerName" = $_.__SERVER 
              "IPAddress" = $adapterAddress 
              "DNSServer" = $dnsServerAddress 
              "WINSPrimaryServer" = $_.WINSPrimaryServer 
              "WINSSecondaryServer" = $_.WINSSecondaryServer 
            } | select-object ComputerName,IPAddress,DNSServer,WINSPrimaryServer,WINSSecondaryServer 
          } 
        } 
      } 
    } 
  } 
} 
 
process { 
  if ( $PipelineInput ) { 
    Get-IPInfo $_ 
  } 
  else { 
    $ComputerName | foreach-object { 
      Get-IPInfo $_ 
    } 
  } 
}

Open in new window

0
Comment
Question by:ncomper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:VirastaR
ID: 39708547
Hi,

Check this..

How to get servers’ DNS settings exported to CSV in Powershell

Hope that will help you in what you are trying to achieve exporting the Powershell command output in CSV File format.

Hope that helps :)
0
 
LVL 40

Expert Comment

by:footech
ID: 39708557
The script will output custom objects.  If you want to export to a .CSV you could just run like
.\Get-DNSandWINS.ps1 servername | export-csv dns.csv -notype

Open in new window

0
 
LVL 5

Author Comment

by:ncomper
ID: 39708905
Excellent that works thanks,

Would it be possible to add additional code that would right errors for the hosts it could not connect to, currently it just writes an error to the console but doesn't actually tell you what host has failed.

Thanks
0
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 39709246
Rather than rewriting the script at this point, I would just test the connection before calling the script.  So if you had a list of machines in a file "complist.txt"
GC complist.txt | % {
   $comp = $_
   If ( Test-Connection $comp -quiet -count 1)
   {
      .\Get-DNSandWINS.ps1 $comp
   }
   Else
   { Write-Host "Couldn't contact $comp" }
} | export-csv dns.csv -notype

Open in new window

0
 
LVL 5

Author Closing Comment

by:ncomper
ID: 39731530
Excellent as usual Thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question