Help with Powershell script

Hi,
I have this powershell script below but I would like to export results into csv. Could you please help.
I would like to have column servername and then other columns for serverIP, DNS, Gateway, etc.
I dont need MacADD only. So, if you could remove it. When I ran the script it only displays results on the screen and I could not make it export to csv.

Thank you very much.



<#
                  " Satnaam WaheGuru Ji"      
                  
                  Author  :  Aman Dhally
                  E-Mail  :  amandhally@gmail.com
                  website :  www.amandhally.net
                  twitter : https://twitter.com/#!/AmanDhally
                  facebook: http://www.facebook.com/groups/254997707860848/
                  Linkedin: http://www.linkedin.com/profile/view?id=23651495

                  Date      : 18-Sept-2012
                  File      : Office_2010_Trusted_Locations
                  Purpose : Getting Network Information of Multile Servers
                  
                  Version : 1

                  my Spider runned Away :(


#>


      param (
      [array]$arrComputer="$env:computername"
      )
      
      "`n"
      
      Write-Host "Name|    NetworkCard                                  | IP          | SUBNET      | GateWay      | MacADD           | DNS "  -ForegroundColor Green

foreach ( $Computer in $arrComputer ) {
      

      $nwINFO = Get-WmiObject -ComputerName $Computer Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -ne $null } #| Select-Object DNSHostName,Description,IPAddress,IpSubnet,DefaultIPGateway,MACAddress,DNSServerSearchOrder | format-Table * -AutoSize
      #| Select-Object DNSHostName,Description,IPAddress,IpSubnet,DefaultIPGateway,MACAddress,DNSServerSearchOrder
      $nwServerName = $nwINFO.DNSHostName
      $nwDescrip = $nwINFO.Description
      $nwIPADDR = $nwINFO.IPAddress
      $nwSUBNET = $nwINFO.IpSubnet
      $nwGateWay = $nwINFO.DefaultIPGateway
      $nwMacADD = $nwINFO.MACAddress
      $nwDNS = $nwINFO.DNSServerSearchOrder
      #            Server/CompName   |NetworkCard | IPAdress  |  SubnetMask|  Gateway      | MAC Address|   DNS |
      Write-Host "$nwServerName | $nwDescrip | $nwIPADDR | $nwSUBNET | $nwGateWay | $nwMacADD | $nwDNS " | ft *

      }
creative555Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Thomas GrassiSystems AdministratorCommented:
Correct Write-Host only outputs to console

Found this that may help you

http://stackoverflow.com/questions/25461847/powershell-output-to-csv-file

HTH
0
creative555Author Commented:
Hi,
I wasn't able to get the IP. I get this error in the spreadsheet below.
i also need the script to read input file where I need to load all the servers that I need to get IPs, DNS info, etc

For example, do I need to type:
.\scriptname.ps1 servername1,servername2 ?
The above would also work for me as long as I can specify more than one server to query.

Thank you so much

Resolved      FinalIpAddress      MNme      Dte      Tme
<ERROR>      <ERROR>      USWES751      7/28/2015      12:41:32
<ERROR>      <ERROR>      USWES751      7/28/2015      12:41:32
0
creative555Author Commented:
hm weired. what is this script doing exactly? It exported IP for another server, but wrong IP. The IP is NOT for this server....

"Resolved","FinalIpAddress","MNme","Dte","Tme"
"<ERROR>","198.176.170.2","USSErver01","7/28/2015","12:40:57"
"<ERROR>","198.176.170.2","UsServer01","7/28/2015","12:40:57"

The script that I posted is perfect, but how do I get those results exported into csv same as it displays on the screen?

I can actually copy and paste into csv but afraid to make mistake during pasting. Would be great if you can revise the script that I already have since it is working perfectly.
0
Top Threats of Q1 & How to Defend Against Them

WEBINAR: Join WatchGuard CTO and our Threat Research Team on Aug. 2nd to hear the findings from our Q1 Internet Security Report! Learn more about the top threats detected in the first quarter and how you can defend your business against them!

footechCommented:
To export to .CSV you need to output objects (at least to make use of the Export-CSV cmdlet).
param (
    [string[]]$arrComputer="$env:computername"
)

foreach ( $Computer in $arrComputer ) { 
    Get-WmiObject -ComputerName $Computer Win32_NetworkAdapterConfiguration |
     Where-Object { $_.IPAddress -ne $null } |
     Select-Object @{n="Name";e={$_.DNSHostName}},
                   @{n="NetworkCard";e={$_.Description}},
                   @{n="IP";e={$_.IPAddress}},
                   @{n="Subnet";e={$_.IpSubnet}},
                   @{n="Gateway";e={$_.DefaultIPGateway}},
                   @{n="DNS";e={$_.DNSServerSearchOrder}}
}

Open in new window


Then to run
.\scriptname.ps1 servername1,servername2 | Export-CSV somefile.csv -notype

Open in new window


Reading from a file
.\scriptname.ps1 (Get-Content servers.txt) | Export-CSV somefile.csv -notype

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
creative555Author Commented:
Do I need to add this script  on the bottom to the original script? I tried replacing it with what I had and the excel file is empty.

      param (
    [string[]]$arrComputer="$env:computername"
)

foreach ( $Computer in $arrComputer ) {
    Get-WmiObject -ComputerName $Computer Win32_NetworkAdapterConfiguration |
     Where-Object { $_.IPAddress -ne $null } |
     Select-Object @{n="Name";e={$_.DNSHostName}},
                   @{n="NetworkCard";e={$_.Description}},
                   @{n="IP";e={$_.IPAddress}},
                   @{n="Subnet";e={$_.IpSubnet}},
                   @{n="Gateway";e={$_.DefaultIPGateway}},
                   @{n="DNS";e={$_.DNSServerSearchOrder}}
}
0
creative555Author Commented:
oh. I am sorry. I ran the old script. Replacing and running script like you posted worked!!!
1
creative555Author Commented:
Excellent!  Thank you so much!!!
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.