Solved

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

Posted on 2014-10-29
12
184 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
  • 6
  • 6
12 Comments
 
LVL 28

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 28

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
 
LVL 2

Author Comment

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

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 28

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 28

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 28

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now