Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I'm a freelance consultant, and your site's helped me with many issues. I just wanted you to know that someone does appreciate the intelligent help your site offers."

Geography

Where in the world do Tek-Tips members come from?
DarkOne72 (TechnicalUser)
9 Feb 11 11:21
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..
DarkOne72 (TechnicalUser)
9 Feb 11 11:49
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.
crobin1 (MIS)
12 Feb 11 13:07
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
 

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close