PowerShell – IP to HostName along with Ping in Excel

The script I am sharing today is extension of below script:

PowerShell – Ping Machines and report in Excel

This script will use list of IP address , pings them report if its up or down and query the Hostname from DNS as well.

Extract the Zip file from below link

https://gallery.technet.microsoft.com/scriptcenter/PowerShell-IP-to-HostName-a856289b

 

Update the MachineList.txt with ip addresses

Run the batch file & below will be th result.

################################################################### 
##           Script to check the status of machines               
##           Author: Vikas Sukhija   http://msexchange.me                                 
##           Date: 01-28-2016                                
##           Update:  Converted from VBscript 
##         Modified: IP to Host Names 
##https://gallery.technet.microsoft.com/scriptcenter/Ping-Machines-and-report-19d590ce           
################################################################### 
 
$path = ".\results.xls" 
 
$objExcel = new-object -comobject excel.application  
 
if (Test-Path $path)  
{  
$objWorkbook = $objExcel.WorkBooks.Open($path)  
$objWorksheet = $objWorkbook.Worksheets.Item(1)  
} 
 
else {  
$objWorkbook = $objExcel.Workbooks.Add()  
$objWorksheet = $objWorkbook.Worksheets.Item(1) 
} 
 
$objExcel.Visible = $True 
 
#########Add Header#### 
 
$objWorksheet.Cells.Item(1, 1) = "MachineIP" 
$objWorksheet.Cells.Item(1, 2) = "Result" 
$objWorksheet.Cells.Item(1, 3) = "HostName" 
 
$machines = gc .\machinelist.txt 
$count = $machines.count 
 
$row=2 
 
$machines | foreach-object{ 
$ping=$null 
$hname =$null 
$machine = $_ 
$ping = Test-Connection $machine -Count 1 -ea silentlycontinue 
 
if($ping){ 
 
$objWorksheet.Cells.Item($row,1) = $machine 
$objWorksheet.Cells.Item($row,2) = "UP" 
     
$hname = [System.Net.Dns]::GetHostByAddress($machine).HostName 
 
$objWorksheet.Cells.Item($row,3) = $hname  
         
$row++else { 
 
$objWorksheet.Cells.Item($row,1) = $machine 
$objWorksheet.Cells.Item($row,2) = "DOWN" 
 
$row++} 
} 
 
#################################################################

Regards

Sukhija Vikas

http://msexchange.me

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s