Link to home
Start Free TrialLog in
Avatar of Jorge Ocampo
Jorge OcampoFlag for United States of America

asked on

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

i need a script that pings hostnames in a list and returns with IP address even if its not up
Avatar of becraig
becraig
Flag of United States of America image

$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

Avatar of Jorge Ocampo

ASKER

how would i tell it the location of my list?
gc serverlist.txt

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

e.g.  gc c:\somefile.txt
do i save ps1? because i try to run in powershell
yes save it as a ps1 and run.
At C:\Users\jocampo\Desktop\test.ps1:2 char:23
+ gc c:\\server.txt | % {
+                       ~
Missing closing '}' in statement block.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndCurlyBrace
ASKER CERTIFIED SOLUTION
Avatar of becraig
becraig
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
NICE :)
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
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.
wouldnt it only return the domain controller?

i did two and it doesnt match what is in the report
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.