PoshCode Logo PowerShell Code Repository

Get-WebSite by Boe Prox 6 years ago
View followups from Boe Prox, googleru, kokulan, Cloud and Cloud | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2496"></script>download | new post

This script will allow you to query a web site and retrieve information about the web site and whether it is available or not.

  1. function Get-WebSite {
  2. <#  
  3. .SYNOPSIS  
  4.     Retrieves information about a website.
  5. .DESCRIPTION
  6.     Retrieves information about a website.
  7. .PARAMETER Url
  8.     URL of the website to test access to.
  9. .PARAMETER UseDefaultCredentials
  10.     Use the currently authenticated user's credentials  
  11. .PARAMETER Proxy
  12.     Used to connect via a proxy
  13. .PARAMETER TimeOut
  14.     Timeout to connect to site, in milliseconds
  15. .PARAMETER Credential
  16.     Provide alternate credentials              
  17. .NOTES  
  18.     Name: Get-WebSite
  19.     Author: Boe Prox
  20.     DateCreated: 08Feb2011        
  21. .EXAMPLE  
  22.     Get-WebSite -url "http://www.bing.com"
  23.    
  24. Description
  25. ------------
  26. Returns information about Bing.Com to include StatusCode and type of web server being used to host the site.
  27.  
  28. #>
  29. [cmdletbinding(
  30.         DefaultParameterSetName = 'url',
  31.         ConfirmImpact = 'low'
  32. )]
  33.     Param(
  34.         [Parameter(
  35.             Mandatory = $True,
  36.             Position = 0,
  37.             ParameterSetName = '',
  38.             ValueFromPipeline = $True)]
  39.             [string][ValidatePattern("^(http|https)\://*")]$Url,
  40.         [Parameter(
  41.             Position = 1,
  42.             Mandatory = $False,
  43.             ParameterSetName = 'defaultcred')]
  44.             [switch]$UseDefaultCredentials,
  45.         [Parameter(
  46.             Mandatory = $False,
  47.             ParameterSetName = '')]
  48.             [string]$Proxy,
  49.         [Parameter(
  50.             Mandatory = $False,
  51.             ParameterSetName = '')]
  52.             [Int]$Timeout,
  53.         [Parameter(
  54.             Mandatory = $False,
  55.             ParameterSetName = 'altcred')]
  56.             [switch]$Credential            
  57.                        
  58.         )
  59. Begin {    
  60.     $psBoundParameters.GetEnumerator() | % {
  61.         Write-Verbose "Parameter: $_"
  62.         }
  63.    
  64.     #Create the initial WebRequest object using the given url
  65.     Write-Verbose "Creating the web request object"        
  66.     $webRequest = [net.WebRequest]::Create($url)
  67.    
  68.     #Use Proxy address if specified
  69.     If ($PSBoundParameters.ContainsKey('Proxy')) {
  70.         #Create Proxy Address for Web Request
  71.         Write-Verbose "Creating proxy address and adding into Web Request"
  72.         $webRequest.Proxy = New-Object -TypeName Net.WebProxy($proxy,$True)
  73.         }
  74.        
  75.     #Set timeout
  76.     If ($PSBoundParameters.ContainsKey('TimeOut')) {
  77.         #Setting the timeout on web request
  78.         Write-Verbose "Setting the timeout on web request"
  79.         $webRequest.Timeout = $timeout
  80.         }        
  81.    
  82.     #Determine if using Default Credentials
  83.     If ($UseDefaultCredentials) {
  84.         #Set to True, otherwise remains False
  85.         Write-Verbose "Using Default Credentials"
  86.         $webrequest.UseDefaultCredentials = $True
  87.         }
  88.     #Determine if using Alternate Credentials
  89.     If ($Credential) {
  90.         #Prompt for alternate credentals
  91.         Write-Verbose "Prompt for alternate credentials"
  92.         $wc.Credential = (Get-Credential).GetNetworkCredential()
  93.         }            
  94.        
  95.     #Set TimeStamp prior to attempting connection    
  96.     $then = get-date
  97.     }
  98. Process {    
  99.     Try {
  100.         #Make connection to gather response from site
  101.         $response = $webRequest.GetResponse()
  102.         #If successful, get the date for comparison
  103.         $now = get-date
  104.        
  105.         #Generate report
  106.         Write-Verbose "Generating report from website connection and response"  
  107.         $report = @{
  108.             URL = $url
  109.             StatusCode = $response.Statuscode -as [int]
  110.             StatusDescription = $response.StatusDescription
  111.             ResponseTime = "$(($now - $then).totalseconds)"
  112.             WebServer = $response.Server
  113.             Size = $response.contentlength
  114.             }
  115.         }
  116.     Catch {
  117.         #Get timestamp of failed attempt
  118.         $now = get-date
  119.         #Put the current error into a variable for later use
  120.         $errorstring = "$($error[0])"
  121.        
  122.         #Generate report
  123.         $report = @{
  124.             URL = $url
  125.             StatusCode = ([regex]::Match($errorstring,"\b\d{3}\b")).value
  126.             StatusDescription = (($errorstring.split('\)')[2]).split('.\')[0]).Trim()
  127.             ResponseTime = "$(($now - $then).totalseconds)"
  128.             WebServer = $response.Server
  129.             Size = $response.contentlength
  130.             }  
  131.         }
  132.     }
  133. End {        
  134.     #Display Report    
  135.     New-Object PSObject -property $report  
  136.     }    
  137. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me