Skip to content

Instantly share code, notes, and snippets.

@gravejester
Created September 20, 2016 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gravejester/e3c9752167a570aaa08add0f6d806b9f to your computer and use it in GitHub Desktop.
Save gravejester/e3c9752167a570aaa08add0f6d806b9f to your computer and use it in GitHub Desktop.
function Test-WebRequest {
<#
.SYNOPSIS
Test web request.
.DESCRIPTION
This function can be used to test a web request to a URI. It functions similarly to Test-Path and will either return
True or False.
.EXAMPLE
Test-WebRequest 'google.com'
This should return True.
.EXAMPLE
Test-WebRequest 'google.com/api'
This should return False.
.NOTES
Author: Øyvind Kallstad
Version: 1.0
Date: 20.09.2016
.OUTPUTS
System.Boolean
.INPUTS
System.String
System.Uri
.LINK
https://communary.net/
#>
[CmdletBinding()]
param (
# Specifies the Uniform Resource Identifier (URI) of the resource to test.
[Parameter()]
[uri] $Uri,
# Specifies a user account that has permission to send the request.
[Parameter()]
[PSCredential] $Credential,
# Specifies how long the request can be pending before it times out. Enter value in seconds.
# The default value, 0, specifies an indefinite time-out.
[Parameter()]
[int32] $TimeoutSec = 0
)
try {
$result = Invoke-WebRequest -Uri $Uri -Method Head -Credential $Credential -TimeoutSec $TimeoutSec
}
catch {
$result = $null
}
if ($result.StatusCode -eq 200) {
Write-Output $true
}
else {
Write-Output $false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment