Created
April 24, 2024 12:26
-
-
Save NenoLoje/e37e41a0368c1e3e956947bfda699dfd to your computer and use it in GitHub Desktop.
PowerShell script to check HTTP status code and for an expected string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param | |
( | |
[Parameter(Mandatory=$True,Position=1)] | |
[string]$url, | |
[Parameter(Mandatory=$True,Position=2)] | |
[string]$expectedString | |
) | |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 | |
try | |
{ | |
Write-Host "Testing URL: $url" | |
$response = Invoke-WebRequest $url -UseBasicParsing | |
Write-Host "Web request succeeded with HTTP" $response.StatusCode "("$response.StatusDescription")" | |
} | |
catch | |
{ | |
$errorMessage = "Web request FAILED with error: $_" | |
Write-Host $errorMessage | |
Write-Host "##vso[task.logissue type=error;]$errorMessage" | |
Write-Host '##vso[task.complete result=Failed;]' | |
} | |
Write-Host "Expected string: $expectedString" | |
# Let's check the response and look for something meaningful | |
$contentCheck = $response.Content.Contains($expectedString) | |
if ($contentCheck -eq $true) | |
{ | |
Write-Host "Content check: OK." | |
} | |
else | |
{ | |
$errorMessage = 'Content check FAILED.' | |
Write-Host $errorMessage | |
Write-Host "##vso[task.logissue type=error;]$errorMessage" | |
Write-Host '##vso[task.complete result=Failed;]' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment