Solved

Script that pings machine hostname in a list and replys with IP address

Posted on 2014-10-29
12
194 Views
Last Modified: 2014-10-30
i need a script that pings hostnames in a list and returns with IP address even if its not up
0
Comment
Question by:Jorge Ocampo
[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
  • 6
  • 6
12 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40411964
$report = @()
gc serverlist.txt | % {
	$server = $_
	$ips = [System.Net.Dns]::GetHostAddresses($server).IPAddressToString
	if (Test-Connection $server) { $compstate = "ONLINE" }
	else { $compstate = "OFFLINE"}
	$ips | % {
		$ip = $_
		$item = New-Object PSObject
		$item | Add-Member -type NoteProperty -Name 'SERVER' -Value $server
		$item | Add-Member -type NoteProperty -Name 'STATE' -Value $compstate
		$item | Add-Member -type NoteProperty -Name 'IPADDRESS' -Value $ip
		$report += $item
		
	}
	$report | Export-Csv c:\report.csv -NTI

Open in new window

0
 
LVL 2

Author Comment

by:Jorge Ocampo
ID: 40412014
how would i tell it the location of my list?
0
 
LVL 29

Expert Comment

by:becraig
ID: 40412020
gc serverlist.txt

you change serverlist.txt to the full path of whatever your input file is.

e.g.  gc c:\somefile.txt
0
Are You Headed to Black Hat USA 2017?

Getting ready for Black Hat next week? Kick things off with the WatchGuard Badge Challenge and test your puzzle and cipher skills. Do you have what it takes to earn our limited edition Firebox Badge? Get started today - https://crimsonthorn.net

 
LVL 2

Author Comment

by:Jorge Ocampo
ID: 40412025
do i save ps1? because i try to run in powershell
0
 
LVL 29

Expert Comment

by:becraig
ID: 40412042
yes save it as a ps1 and run.
0
 
LVL 2

Author Comment

by:Jorge Ocampo
ID: 40412045
At C:\Users\jocampo\Desktop\test.ps1:2 char:23
+ gc c:\\server.txt | % {
+                       ~
Missing closing '}' in statement block.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndCurlyBrace
0
 
LVL 29

Accepted Solution

by:
becraig earned 500 total points
ID: 40412051
copy paste issue :(

$report = @()
gc serverlist.txt | % {
	$server = $_
	$ips = [System.Net.Dns]::GetHostAddresses($server).IPAddressToString
	if (Test-Connection $server) { $compstate = "ONLINE" }
	else { $compstate = "OFFLINE" }
	$ips | % {
		$ip = $_
		$item = New-Object PSObject
		$item | Add-Member -type NoteProperty -Name 'SERVER' -Value $server
		$item | Add-Member -type NoteProperty -Name 'STATE' -Value $compstate
		$item | Add-Member -type NoteProperty -Name 'IPADDRESS' -Value $ip
		$report += $item
		
	}
}
$report | Export-Csv c:\report.csv -NTI

Open in new window

0
 
LVL 2

Author Comment

by:Jorge Ocampo
ID: 40412064
NICE :)
0
 
LVL 2

Author Comment

by:Jorge Ocampo
ID: 40412102
so it actually return the report with same IP for multiple servers that if i did a ping it could find anything

172.17.2.84
172.17.2.84
172.17.2.84
172.17.2.84
172.17.2.84
172.17.2.84
172.17.2.84
172.17.2.84
172.17.2.84
0
 
LVL 29

Expert Comment

by:becraig
ID: 40412117
My first thought would be that there are multiple dns entries for the same ip.

However there is one way to verify if we need to change something.

For one of the computers in the list, simply run:
nslookup computername 

Open in new window


from a command prompt and see what ip it returns, whether it matches with the report or not.
0
 
LVL 2

Author Comment

by:Jorge Ocampo
ID: 40413216
wouldnt it only return the domain controller?

i did two and it doesnt match what is in the report
0
 
LVL 29

Expert Comment

by:becraig
ID: 40413588
No it would not so let me break down the script for you:

1.  We populate an array from your list of computers, then loop through that array:
gc serverlist.txt | % {

Open in new window


2. We assign a variable name of $server to each computer in the array:
$server = $_

Open in new window


3.  We populate a new array name $ips with all the ip addresses returned for the computer name value in the variable $server:
$ips = [System.Net.Dns]::GetHostAddresses($server).IPAddressToString

Open in new window


4: We loop through that array and create the labels and report values based on the current iteration of the loop:
	$ips | % {
		$ip = $_
		$item = New-Object PSObject
		$item | Add-Member -type NoteProperty -Name 'SERVER' -Value $server
		$item | Add-Member -type NoteProperty -Name 'STATE' -Value $compstate
		$item | Add-Member -type NoteProperty -Name 'IPADDRESS' -Value $ip
		$report += $item
		
	}

Open in new window


5. We then export that information to a csv file:

$report | Export-Csv c:\report.csv -NTI

Open in new window


So unless the values returned by the population of the $ips array are not correct, then you should be seeing the correct vales in the report.
0

Featured Post

What, When and Where - Security Threats from Q1

Join Corey Nachreiner, CTO, and Marc Laliberte, Information Security Threat Analyst, on July 26th as they explore their key findings from the first quarter of 2017.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

632 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