Confirm-Url.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<#
.Synopsis
   Confirm the health of a url by looking for a specific pattern.
.DESCRIPTION
   Confirm the health of a url by looking for a specific pattern.
   The pattern can also be accompanied by a specific class name.
   If a certain pattern should not be found, use the $InvertMatch
   switch to invert the output. The script outputs the Url with a
   boolean of the health status.
 
   Confirm-Url requires PowerShell 3.0, specifically the Invoke-WebRequest
   cmdlet.
.NOTES
   Created by: Jason Wasser @wasserja
   Modified: 5/22/2017
 
   Changelog:
   v1.1
    * Added Web request output to allow further investigation.
   v1.0
    * Initial script
.PARAMETER Uri
    Provide a single or an array url/uri to confirm.
.PARAMETER Pattern
    Provide a regular expression pattern for which to find on the
    uri.
.PARAMETER ClassName
    Optionally provide a class name in which to search for the
    pattern.
.PARAMETER InvertMatch
    By default if the pattern is found the URL is considered healthy.
    By using the InvertMatch switch, finding the pattern means the
    URL is unhealthy.
.EXAMPLE
   Confirm-Url -Uri www.google.com -Pattern 'Google Search'
   Grab the google.com home page and verify if the phrase Google Search is present.
.EXAMPLE
   Confirm-Url -Uri www.domain.com/application -ClassName 'Error' -Pattern 'Website Unavailable' -InvertMatch
   Grab the url and look for the pattern website unavailable in the class error.
   If it is present then the Url is unhealthy.
.LINK
   https://gallery.technet.microsoft.com/scriptcenter/Confirm-Url-PowerShell-d66e021f
#>

#Requires -version 3.0
function Confirm-Url
{
    [CmdletBinding()]
    [Alias()]
    Param
    (

        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   ValueFromPipeline=$true,
                   Position=0)]
        [string[]]$Uri,
        [string]$Pattern='.',#='Enrollment Unavailable',
        [string]$ClassName,#='main-container'
        [switch]$InvertMatch#=$true
    )

    Begin
    {
    }
    Process
    {
        foreach ($UriInstance in $Uri) {
            try {

                # Attempt to grab Uri provided by user
                Write-Verbose -Message "Trying Uri $UriInstance"
                $WebRequest = Invoke-WebRequest -Uri $UriInstance -ErrorAction Stop
                
                # If a class name was provided apply filter script to that section of the page.
                if ($ClassName) {
                    Write-Verbose "ClassName Parameter detected: $ClassName"
                    if ($WebRequest.AllElements | Where-Object -FilterScript {$_.class -eq $ClassName} | Select-Object -First 1 -ExpandProperty InnerText | Select-String -Pattern $Pattern) {
                        Write-Verbose "Found pattern $Pattern in $UriInstance."
                        if ($InvertMatch) {
                            Write-Verbose "InvertMatch switch detected."
                            Write-Verbose "Pattern $Pattern found in $UriInstance. Uri is not healthy."
                            $Healthy=$false
                            }
                        else {
                            Write-Verbose "Pattern $Pattern found in $UriInstance. Uri is healthy."
                            $Healthy=$true
                            }
                        }
                    else {
                        if ($InvertMatch) {
                            Write-Verbose "InvertMatch switch detected."
                            Write-Verbose "Pattern $Pattern not found in $UriInstance. Uri is healthy."
                            $Healthy=$true
                            }
                        else {
                            Write-Verbose "Pattern $Pattern not found in $UriInstance. Uri is not healthy."
                            $Healthy=$false
                            }
                        }
                    }
                # If no class name was provided, search the the entire page for the provided pattern.
                else {
                    if ($WebRequest | Select-String -Pattern $Pattern) {
                        Write-Verbose "Found pattern $Pattern in $UriInstance."
                        if ($InvertMatch) {
                            Write-Verbose "$InvertMatch switch detected."
                            Write-Verbose "Pattern $Pattern found in $UriInstance. Uri is not healthy."
                            $Healthy=$false
                            }
                        else {
                            Write-Verbose "Pattern $Pattern found in $UriInstance. Uri is healthy."
                            $Healthy=$true
                            }
                        }
                    else {
                        if ($InvertMatch) {
                            Write-Verbose "InvertMatch switch detected."
                            Write-Verbose "Pattern $Pattern not found in $UriInstance. Uri is healthy."
                            $Healthy=$true
                            }
                        else {
                            Write-Verbose "Pattern $Pattern not found in $UriInstance. Uri is not healthy."
                            $Healthy=$false
                            }
                        }
                    }
                $UriHealthParameters = [ordered]@{
                    Uri = $UriInstance
                    Healthy = $Healthy
                    WebRequest = $WebRequest
                    }
                $UriHealth = New-Object -TypeName PSObject -Property $UriHealthParameters
                $UriHealth
            }
        catch {
            Write-Warning -Message $Error[0].ErrorDetails.Message
        }   
    }
        
    }
    End
    {
    }
}