Ping Router Script

Hi,

I am trying to get a Powershell script to ping out - now it works just writing to the console perfectly but trying to get it on a html file is it bit tricky.

$list = get-content "C:\Remote\ip.txt" 


foreach ($ip in $list)

{
$result = New-Object psObject
Write-Host "Trying $($ip)" -ForegroundColor White

Try {

$IPComputer = Test-Connection $ip -Count 1 -ErrorAction Stop | Select -ExpandProperty ipv4Address | `
Select -ExpandProperty ipAddressToString

$Sucess = Write-Host "Success" -ForegroundColor Green

Add-Content $IndexPage "<tr><td>$IP</td><td>$Sucess</td><td>$Failed</td><tr></table>"

}

catch

{

$Failed = Write-Host "Down" -ForegroundColor Red



}

}


$Date = Get-Date -Format "dddd dd MMMM yyyy"
$Time = Get-Date -Format "hh:mm:ss"

$IndexPage = "C:\Remote\Index.html" # This Creates The Index Page
New-Item -ItemType file $IndexPage -Force # Creates The Page

Add-Content $IndexPage "<table width='auto' align='center' border-collapse='collapse'>"
Add-Content $IndexPage "<tr bgcolor='#D0E6FF'>"
Add-Content $IndexPage "<td colspan='4' height='25' align='center'>"
Add-Content $IndexPage "<font face='tahoma' color='#000000' size='2'><b>Router Checker</b></font><BR>"
Add-Content $IndexPage "<font face='Arial' color='#000000' size='2'><b>Date: $Date</b><BR>"
Add-Content $IndexPage "<font face='Arial' color='#00000' size='2'><b>Time: $Time</b>"
Add-Content $IndexPage "</td>"
Add-Content $IndexPage "</tr>"
Add-Content $IndexPage "</table>"

Add-Content $IndexPage '<table id="main" class="sortable" <width="auto" align="center">'
Add-Content $IndexPage "<tr bgcolor='#D0E6FF'>"
#Add-Content $IndexPage "<td width='auto' align='center'>Client</td>"
Add-Content $IndexPage "<td width='auto' align='center'>IP Address</td>"
Add-Content $IndexPage "<td width='auto' align='center'>Up</td>"
Add-Content $IndexPage "<td width='auto' align='center'>Down</td>"
Add-Content $IndexPage "</tr>"

Open in new window


Can anyone suggest what i might be doing wrong at the moment?
LVL 1
Alex YoungAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SubsunCommented:
Check this...
$Date = Get-Date -Format "dddd dd MMMM yyyy"
$Time = Get-Date -Format "hh:mm:ss"

$IndexPage = "C:\Remote\Index.html" # This Creates The Index Page
New-Item -ItemType file $IndexPage -Force # Creates The Page

Add-Content $IndexPage "<table width='auto' align='center' border-collapse='collapse'>"
Add-Content $IndexPage "<tr bgcolor='#D0E6FF'>"
Add-Content $IndexPage "<td colspan='4' height='25' align='center'>"
Add-Content $IndexPage "<font face='tahoma' color='#000000' size='2'><b>Router Checker</b></font><BR>"
Add-Content $IndexPage "<font face='Arial' color='#000000' size='2'><b>Date: $Date</b><BR>"
Add-Content $IndexPage "<font face='Arial' color='#00000' size='2'><b>Time: $Time</b>"
Add-Content $IndexPage "</td>"
Add-Content $IndexPage "</tr>"
Add-Content $IndexPage "</table>"

Add-Content $IndexPage '<table id="main" class="sortable" <width="auto" align="center">'
Add-Content $IndexPage "<tr bgcolor='#D0E6FF'>"
#Add-Content $IndexPage "<td width='auto' align='center'>Client</td>"
Add-Content $IndexPage "<td width='auto' align='center'>IP Address</td>"
Add-Content $IndexPage "<td width='auto' align='center'>Up</td>"
Add-Content $IndexPage "<td width='auto' align='center'>Down</td></tr>"

$list = get-content "C:\Remote\ip.txt" 


foreach ($ip in $list)

{
$result = New-Object psObject
Write-Host "Trying $($ip)" -ForegroundColor White

	Try {

	$IPComputer = Test-Connection $ip -Count 1 -ErrorAction Stop | Select -ExpandProperty ipv4Address -ea STOP | `
	Select -ExpandProperty ipAddressToString

	Write-Host "Success" -ForegroundColor Green

	Add-Content $IndexPage "<tr><td>$IP</td><td>Sucess</td><td></td></tr>"

	}

	catch

	{
	Write-Host "Down" -ForegroundColor Red

	Add-Content $IndexPage "<tr><td>$IP</td><td></td><td>Failed</td></tr>"
	}

}
Add-Content $IndexPage "</tr></table>"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alex YoungAuthor Commented:
Brilliant Perfect!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.