Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ConvertTo-HTML Help

Status
Not open for further replies.

DarkOne72

Technical User
Jun 14, 2002
210
US
Hello All,

I hope you guys can help me out. I am building powershell script that checks the status of websites that is pulled from a text file names URLS.txt and displays if it is up or down. that works great! However, I am now trying to get it to do a convertto-html and have the results show up in internet explorer. I can not seem to get this to work. I can invoke the html it is outputting to but it is either the wrong data or nothing at all. I have tried different areas as well still no luck. I am only looking for the url it is checking (i think it is the responseuri) and the status of the site like in my code. I also want it in the same page showing all the results of the urls on one page.

Code:
#WebChecker.ps1

TRAP { CONTINUE }

$urls = Get-Content C:\URLS.txt # -Exclude ("-")
   Invoke-item C:\Scripts\WebChecker_Report.html	
	
function CheckSiteStatus()
{
    $req = [System.Net.WebRequest]::Create($url)
 
	 
	try
    {
        $res = $req.GetResponse()
	
			IF ($Res.StatusCode -eq "OK") {
				Write-Host -foregroundcolor Gray $url -NoNewline;  Write-Host -foreground GREEN " UP" 
			 }
								 				 
			 
    			Write-Host ""
    }
    catch
    {
		 	
			IF
				($Res.StatusCode -ne "OK") {
				Write-Host -foregroundcolor Gray $url -NoNewline; Write-Host -foreground Red " WARNING!..DOWN"
			}
				Write-Host ""
  }
}
FOREACH ($url IN $urls)
{

$url|ConvertTo-Html|Out-File C:\Scripts\WebChecker_Report.html
 
CheckSiteStatus
}

Thanks in advance..
 
I was also thinking of maybe an out-gridview but I don't know how to impliment that and show only the 2 items i want (responseuri and statuscode)

Any help in either situation is greatly appreciated.
 
There are a couple of issues with the script above. The Invoke-Item call is before you've actually created the html report. The CheckSiteStatus function doesn't store or return the results of the test. The ForEach loop only converts the url to html, and overwrites the report each time through the loop.

There are several ways to do this of course, but after playing around with it for a while I've come up with the following:
Code:
#WebChecker2.ps1

$urls = Get-Content C:\URLS.txt

# Create an empty array to hold the result objects
$urlres = @()
    
function CheckSiteStatus()
{
    $req = [System.Net.WebRequest]::Create($url)

    # Create an empty custom object, shortcut method
    $res1 = "" | Select Site, Status
    # Store the URL in the object
    $res1.Site = $url

    $res = $req.GetResponse()
    
    IF ($Res.StatusCode -eq "OK") {
        Write-Host -foregroundcolor Gray $url -NoNewline;  Write-Host -foreground GREEN " UP"
        Write-Host ""
        # Store the status in the object
        $res1.Status = "UP"
        }
    ELSE {
        Write-Host -foregroundcolor Gray $url -NoNewline; Write-Host -foreground Red " WARNING!..DOWN"
        Write-Host ""
        # Store the status in the object
        $res1.Status = "DOWN"
    }

    # Return the object
    $res1
}

FOREACH ($url IN $urls)
{
    # Check the site, and store the returned object in the array
    $urlres += CheckSiteStatus
}

# Create the report
$urlres | ConvertTo-HTML > C:\Scripts\WebChecker_Report.html

Invoke-Item C:\Scripts\WebChecker_Report.html

See if this gets you closer to what you want. You should also be able to use Out-GridView with this, as
$urlres | Out-GridView
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top