Link to home
Start Free TrialLog in
Avatar of Kailash Kapal
Kailash KapalFlag for India

asked on

Powershell script to fetch IP address by pinging hostname

I want a powershell script for fetching IP address and outputting it into csv / text file by pinging hostname

I would also like other server details as well.
Avatar of becraig
becraig
Flag of United States of America image

Can you indicate all the details you need ?

$servers | % {
$server = $_
$ipaddr = gwmi -class Win32_NetworkAdapterConfiguration -computername $server -filter 'IPEnabled="True"' | select -expand IPAddress | ?{$_ -notmatch ':'} 
}

Open in new window

Avatar of Kailash Kapal

ASKER

I want  IP address, OS version to be pulled in a csv file

I wanted the hostnames to be put via an excel or text file in the powershell script

Also i would want the flexibility to add more details as and when required in future.

Hope that helps.
Here is a script I made for our environment that talks about the various ways you can get the data remotely (You must run the script as an administrator and have Quest CMDLets installed (http://www.quest.com/powershell/activeroles-server.aspx)

Clear-Host
$Computer = Read-Host "Enter the Computer Name (Partial is okay) you want to lookup"
$PC = Get-QADComputer "*$Computer*"
if ($PC -eq $null){
Write-Host "Host not found in AD" -foregroundcolor Yellow
}
else
{
""
""
$BIOS = gwmi win32_bios -computer $PC.name
$ComputerSystem = gwmi win32_ComputerSystem -computer $PC.Name
Write-Host "Computer Name: $($PC.name)" -foregroundcolor yellow
"Serial Number: $($BIOS.serialnumber)"
"Description: $($PC.Description)"
"Network: NIPR"
$IP = gwmi -class Win32_NetworkAdapterConfiguration -computername $PC.name -filter 'IPEnabled="True"' | select -expand IPAddress | ?{$_ -notmatch ':'} 
"Location: Building $($IP.split(".")[2])"
"Manufacturer: $($BIOS.manufacturer)"
"Model: $($ComputerSystem.model)"
"Managed by: $($PC.ManagedBy)"
"OS:$($PC.OSName)"
"OU: $(($PC.dn.split(",")[2]).split("=")[1])"
"IP Address: $IP"
"MAC Address: $((gwmi -Class Win32_NetworkAdapterConfiguration -Computername $PC.name | where { $_.IpAddress -eq $IP}).MACAddress)"
"Primary User: $($ComputerSystem.username)"
""
Write-Host "Pinging Computer for response:" -foregroundcolor Yellow
Ping $($PC.name) -n 2
""
""
}
$Pause = Read-Host "Press enter to continue"

Open in new window


If you want to do the script that does it based on a list, you can modify it to the following script:

$List = gc .\list.txt
foreach ($Computer in $List){
$PC = Get-QADComputer "*$Computer*"
	if ($PC -eq $null){
		Write-Host "$Computer match not found in AD" -foregroundcolor Yellow
	}
	else
	{
		""
		""
		$BIOS = gwmi win32_bios -computer $PC.name
		$ComputerSystem = gwmi win32_ComputerSystem -computer $PC.Name
		Write-Host "Computer Name: $($PC.name)" -foregroundcolor yellow
		"Serial Number: $($BIOS.serialnumber)"
		"Description: $($PC.Description)"
		"Network: NIPR"
		$IP = gwmi -class Win32_NetworkAdapterConfiguration -computername $PC.name -filter 'IPEnabled="True"' | 		select -expand IPAddress | ?{$_ -notmatch ':'} 
		"Location: Building $($IP.split(".")[2])"
		"Manufacturer: $($BIOS.manufacturer)"
		"Model: $($ComputerSystem.model)"
		"Managed by: $($PC.ManagedBy)"
		"OS:$($PC.OSName)"
		"OU: $(($PC.dn.split(",")[2]).split("=")[1])"
		"IP Address: $IP"
		"MAC Address: $((gwmi -Class Win32_NetworkAdapterConfiguration -Computername $PC.name | where { 				$_.IpAddress -eq $IP }).MACAddress)"
		"Primary User: $($ComputerSystem.username)"
		""
	}#end if
}#end for loop

Open in new window


If you want to output that to a CSV, you can easily convert the above with an online way to write to an excel file
http://blogs.technet.com/b/heyscriptingguy/archive/2011/09/23/use-powershell-to-work-with-csv-formatted-text.aspx

Personally, I like to output to a text file separated by semicolons, and import it into excel and separate by semicolon

That would take a big rework for that to happen, but it's not impossible.  It's ugly for code though.  You would first capture all of the output to variables instead of output lines.  Then you would add that to your output list.
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada 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
SOLUTION
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