Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Check to see if a URL request returns data
- .DESCRIPTION
- This function uses a WebClient object which attempts to download
- data from a given URL. This test will fail if there is an exception,
- or if the data returned is empty.
- .OUTPUTS
- PSObject. returns a custom object with the following properties:
- Pass - true/false Whether or not the test passed
- Exception - The exception that occurred (Only if Pass = false)
- ExceptionType - The type of exception that occurred (Only if Pass = false)
- .INPUTS
- The Test-HttpGet function does not accept piped input
- .LINK
- http://msdn.microsoft.com/en-us/library/system.net.webexception(v=vs.80).aspx
- .EXAMPLE
- PS [LAPTOP-TOSHIBA] >Test-HttpGet -url "http://msdn.microsoft.com" | Format-List
- Pass : True
- -- The GET request succeeded
- .EXAMPLE
- PS [LAPTOP-TOSHIBA] >Test-HttpGet -url "https://msdn.microsoft.com" | Format-List
- Pass : True
- -- HTTPS works as well
- .EXAMPLE
- PS [LAPTOP-TOSHIBA] >Test-HttpGet -url "http://false.whereisthis.net" | Format-List
- Pass : False
- Exception : The remote name could not be resolved: 'false.whereisthis.net'
- ExceptionType : Web Exception
- -- The GET request false because the site does not exist
- #>
- function Test-HttpGet {
- param(
- [parameter(Mandatory=$true)]
- [ValidateNotNullOrEmpty()]
- [string]
- $url
- )
- try {
- $data = (new-object System.Net.WebClient).DownloadString($url)
- if(!$data) {
- return New-Object PSObject -Property @{
- Pass=$false;
- Exception="No data was returned";
- ExceptionType="Data Validation";
- }
- }
- else {
- return New-Object PSObject -Property @{
- Pass=$true;
- }
- }
- }
- catch [System.Net.WebException] {
- return New-Object PSObject -Property @{
- Pass=$false;
- Exception=$_;
- ExceptionType="Web Exception";
- }
- }
- catch {
- return New-Object PSObject -Property @{
- Pass=$false;
- Exception=$_;
- ExceptionType="General";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement