Jorge Ocampo
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
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
you change serverlist.txt to the full path of whatever your input file is.
e.g. gc c:\somefile.txt
ASKER
do i save ps1? because i try to run in powershell
yes save it as a ps1 and run.
ASKER
At C:\Users\jocampo\Desktop\t est.ps1:2 char:23
+ gc c:\\server.txt | % {
+ ~
Missing closing '}' in statement block.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordE xception
+ FullyQualifiedErrorId : MissingEndCurlyBrace
+ gc c:\\server.txt | % {
+ ~
Missing closing '}' in statement block.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordE
+ FullyQualifiedErrorId : MissingEndCurlyBrace
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
NICE :)
ASKER
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
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:
from a command prompt and see what ip it returns, whether it matches with the report or not.
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
from a command prompt and see what ip it returns, whether it matches with the report or not.
ASKER
wouldnt it only return the domain controller?
i did two and it doesnt match what is in the report
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:
2. We assign a variable name of $server to each computer in the array:
3. We populate a new array name $ips with all the ip addresses returned for the computer name value in the variable $server:
4: We loop through that array and create the labels and report values based on the current iteration of the loop:
5. We then export that information to a csv file:
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.
1. We populate an array from your list of computers, then loop through that array:
gc serverlist.txt | % {
2. We assign a variable name of $server to each computer in the array:
$server = $_
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
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
}
5. We then export that information to a csv file:
$report | Export-Csv c:\report.csv -NTI
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.
Open in new window