Skip to content

Instantly share code, notes, and snippets.

@NenoLoje
Created April 24, 2024 12:26
Show Gist options
  • Save NenoLoje/e37e41a0368c1e3e956947bfda699dfd to your computer and use it in GitHub Desktop.
Save NenoLoje/e37e41a0368c1e3e956947bfda699dfd to your computer and use it in GitHub Desktop.
PowerShell script to check HTTP status code and for an expected string
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