Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-10-29
12
Medium Priority
?
195 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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 2000 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
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…

722 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