Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-10-29
12
Medium Priority
?
197 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 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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
Loops Section Overview

916 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