none
Verify Access to Websites RRS feed

  • Question

  • I'm working on a script that uses the Install-Package function.  I'd like to verify connectivity to a set of websites before the function attempts to install.  Below is what I know would sort of work, with the exception of the final part and how to inform the user of what site doesn't have connectivity.

    So two parts I'd like some input on

    1.  Is there a more efficient way to do all this instead of specifying 4 separate variables and then running 4 if checks?
    2.  Assuming a site/sites are not accessible, what's a graceful way of notifying the user of the site with connectivity issues?

    $site1 = Invoke-Webrequest site1
    $site2 = Invoke-Webrequest site2
    $site3 = Invoke-Webrequest site3
    $site4 = Invoke-Webrequest site4
    
    if ($site1.StatusDescription -ne "OK") {$Status = "No Connection to Site 1"}
    if ($site2.StatusDescription -ne "OK") {$Status += "No Connection to Site 2"}
    if ($site3.StatusDescription -ne "OK") {$Status += "No Connection to Site 3"}
    if ($site4.StatusDescription -ne "OK") {$Status += "No Connection to Site 4"}
    
    if ($Status -contains "No Connection") {Write-Host "Check Connection to $SiteWithNoConnection"}

    Friday, March 9, 2018 5:58 AM

Answers

  • The script needs to be self-contained unfortunately.  Here's what I have come up with thus far:

    $Sites = "https://www.powershellgallery.com", "https://www.yahoo.com", "https://www.google.com"
    Foreach ($site in $sites) {
      $result = Invoke-WebRequest $site -Method head | Select -ExpandProperty StatusDescription
      if ($result -ne "ok") {Write-Host "Unable to connect to $site"}
    }

    Friday, March 9, 2018 8:21 PM

All replies

  • you could put the sites in a text file and then for a foreach loop to test the connection.  i.e. something like this

    $sites = Get-content c:\sites.txt

    foreach ($site in $Sites){

     IF        test for connection here 

    Else write to a file with the name of the failed sites

    }

    Friday, March 9, 2018 4:15 PM
  • The script needs to be self-contained unfortunately.  Here's what I have come up with thus far:

    $Sites = "https://www.powershellgallery.com", "https://www.yahoo.com", "https://www.google.com"
    Foreach ($site in $sites) {
      $result = Invoke-WebRequest $site -Method head | Select -ExpandProperty StatusDescription
      if ($result -ne "ok") {Write-Host "Unable to connect to $site"}
    }

    Friday, March 9, 2018 8:21 PM
  • So you have a solution. What's your question?

    -- Bill Stewart [Bill_Stewart]

    Friday, March 9, 2018 9:24 PM
    Moderator
  • hahaha, I guess I don't have one anymore.  Doing more than one thing here not fully thinking.  YAY! lol
    Friday, March 9, 2018 9:26 PM