Author Posts

December 21, 2017 at 11:43 am

i am trying to get the status of different urls but it is returning me 200 ok for all urls. How can i get the url status and perform a task as per url status like status is 200, 503, 404.
Also getting below error but the url is working fine for me, i just want to know url is working or not(i want to ignore the below error and just want check url is up or not)
"Invoke-WebRequest : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS
secure channel."

This is my code

[string[]]$sites = ( "https://sdfad.asd.sd",
"https://www.google.co.in/",
"https://www.bing.com/?cc=be")

foreach ($site in $sites)
{
$securepassword = ConvertTo-SecureString "Password" -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential("Username", $securepassword)
$status = (Invoke-WebRequest -Uri $site -Credential $credentials).statuscode
if ($status -eq 200)
{
write-host 'success for site' $site
}else{
write-host 'failure for site' $site
}
}

December 21, 2017 at 1:18 pm

Use a switch statement as in

[string[]]$sites = ( 
    "https://sdfad.asd.sd",
    "https://www.google.co.in/",
    "https://www.bing.com/?cc=be"
)

$securepassword = ConvertTo-SecureString "Password" -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential("Username", $securepassword)
foreach ($site in $sites){
    $status = (Invoke-WebRequest -Uri $site -Credential $credentials).statuscode
    switch ($status) {
        200 { 
            "Status code $status for site $site"
            # further action/code specific to code 200 here 
        }
        404 { 
            "Status code $status for site $site"
            # further action/code specific to code 404 here 
        }
        503 { 
            "Status code $status for site $site"
            # further action/code specific to code 503 here 
        }
        default { 
            "Status code $status for site $site"
            # further action/code specific to 'other' here 
        }
    }
}

December 22, 2017 at 4:45 am

The whole login effort notwithstanding, how about this...

Clear-Host
[string[]]$sites = (
'https://sdfad.asd.sd',
'https://www.google.co.in/',
'https://www.bing.com/?cc=be'
)

foreach ($site in $sites)
{try {Write-Host "The resource $Site responded with status code : " -NoNewLine
($Response = Invoke-WebRequest -Uri $site).StatusCode}
catch {Write-Warning -Message "The resource $site failed to respond, is not online, or does not exist."}}

Results —

The resource https://sdfad.asd.sd responded with status code : WARNING: The resource https://sdfad.asd.sd failed to respond, is not online, or does not exist.
The resource https://www.google.co.in/ responded with status code : 200
The resource https://www.bing.com/?cc=be responded with status code : 200

December 22, 2017 at 10:37 am

It is returning same result for all urls if the url is status 404 or 503 for those it giving 200 ok only.