PoshCode Logo PowerShell Code Repository

Difference between
modified post 6781 by Cloud on Sat 11th Mar 11:31 and
original post 2496 by Boe Prox on Fri 11th Feb 19:27
Showold version | new version | both versions

    
11
function Get-WebSite {
22
<#
33
.SYNOPSIS
44
    Retrieves information about a website.
55
.DESCRIPTION
66
    Retrieves information about a website.
77
.PARAMETER Url
88
    URL of the website to test access to.
99
.PARAMETER UseDefaultCredentials
1010
    Use the currently authenticated user's credentials
1111
.PARAMETER Proxy
1212
    Used to connect via a proxy
1313
.PARAMETER TimeOut
1414
    Timeout to connect to site, in milliseconds
1515
.PARAMETER Credential
1616
    Provide alternate credentials
1717
.NOTES
1818
    Name: Get-WebSite
1919
    Author: Boe Prox
2020
    DateCreated: 08Feb2011
2121
.EXAMPLE
2222
    Get-WebSite -url "http://www.bing.com"
2424
Description
2525
------------
2626
Returns information about Bing.Com to include StatusCode and type of web server being used to host the site.
2828
#>
2929
[cmdletbinding(
3030
	DefaultParameterSetName = 'url',
3131
	ConfirmImpact = 'low'
3232
)]
3333
    Param(
3434
        [Parameter(
3535
            Mandatory = $True,
3636
            Position = 0,
3737
            ParameterSetName = '',
3838
            ValueFromPipeline = $True)]
3939
            [string][ValidatePattern("^(http|https)\://*")]$Url,
4040
        [Parameter(
4141
            Position = 1,
4242
            Mandatory = $False,
4343
            ParameterSetName = 'defaultcred')]
4444
            [switch]$UseDefaultCredentials,
4545
        [Parameter(
4646
            Mandatory = $False,
4747
            ParameterSetName = '')]
4848
            [string]$Proxy,
4949
        [Parameter(
5050
            Mandatory = $False,
5151
            ParameterSetName = '')]
5252
            [Int]$Timeout,
5353
        [Parameter(
5454
            Mandatory = $False,
5555
            ParameterSetName = 'altcred')]
5656
            [switch]$Credential
5858
        )
5959
Begin {
6060
    $psBoundParameters.GetEnumerator() | % {
6161
        Write-Verbose "Parameter: $_"
6262
        }
6464
    #Create the initial WebRequest object using the given url
6565
    Write-Verbose "Creating the web request object"
6666
    $webRequest = [net.WebRequest]::Create($url)
6868
    #Use Proxy address if specified
6969
    If ($PSBoundParameters.ContainsKey('Proxy')) {
7070
        #Create Proxy Address for Web Request
7171
        Write-Verbose "Creating proxy address and adding into Web Request"
7272
        $webRequest.Proxy = New-Object -TypeName Net.WebProxy($proxy,$True)
7373
        }
7575
    #Set timeout
7676
    If ($PSBoundParameters.ContainsKey('TimeOut')) {
7777
        #Setting the timeout on web request
7878
        Write-Verbose "Setting the timeout on web request"
7979
        $webRequest.Timeout = $timeout
8080
        }
8282
    #Determine if using Default Credentials
8383
    If ($UseDefaultCredentials) {
8484
        #Set to True, otherwise remains False
8585
        Write-Verbose "Using Default Credentials"
8686
        $webrequest.UseDefaultCredentials = $True
8787
        }
8888
    #Determine if using Alternate Credentials
8989
    If ($Credential) {
9090
        #Prompt for alternate credentals
9191
        Write-Verbose "Prompt for alternate credentials"
9292
        $wc.Credential = (Get-Credential).GetNetworkCredential()
9393
        }
9595
    #Set TimeStamp prior to attempting connection
9696
    $then = get-date
9797
    }
9898
Process {
9999
    Try {
100100
        #Make connection to gather response from site
101101
        $response = $webRequest.GetResponse()
102102
        #If successful, get the date for comparison
103103
        $now = get-date
105105
        #Generate report
106106
        Write-Verbose "Generating report from website connection and response"
107107
        $report = @{
108108
            URL = $url
109109
            StatusCode = $response.Statuscode -as [int]
110110
            StatusDescription = $response.StatusDescription
111111
            ResponseTime = "$(($now - $then).totalseconds)"
112112
            WebServer = $response.Server
113113
            Size = $response.contentlength
114114
            }
115115
        }
116116
    Catch {
117117
        #Get timestamp of failed attempt
118118
        $now = get-date
119119
        #Put the current error into a variable for later use
120120
        $errorstring = "$($error[0])"
122122
        #Generate report
123123
        $report = @{
124124
            URL = $url
125125
            StatusCode = ([regex]::Match($errorstring,"\b\d{3}\b")).value
126126
            StatusDescription = (($errorstring.split('\)')[2]).split('.\')[0]).Trim()
127127
            ResponseTime = "$(($now - $then).totalseconds)"
128128
            WebServer = $response.Server
129129
            Size = $response.contentlength
130130
            }
131131
        }
132132
    }
133133
End {
134134
    #Display Report
135135
    New-Object PSObject -property $report
136136
    }
137137
}

ContributeMost Recent Contributions (feed)

Contribute ... Next Page