Getting project internal URL status in non internet server

Hi ,

We have a server where we don’t have internet, but our internal project URL opens in that server without any issue. Now i written below PowerShell script to test this URL status code (up or not), but it is always returning status code 0 even the url is up.

If it is up it should return a status code of 200 , if it is down it should return a status code of 404 or etc…

function Get-UrlStatusCode([string] $Url)
{
try
{
(Invoke-WebRequest -Uri $Url -UseBasicParsing -DisableKeepAlive).StatusCode
}
catch [Net.WebException]
{
[int]$_.Exception.Response.StatusCode
}
}

$statusCode = Get-UrlStatusCode ‘https://mcs-098-web.america.com/api/
write-host $statusCode

As the above url is opening in internet explorer in the above said non-internet server, i tried with below script also. but not sure how to get status code with below script.

$ie = New-Object -ComObject internetexplorer.application
$ie.navigate2(“https://mcs-098-web.america.com/api/”)
$ie.

Firstly, when posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working).

If you’re getting 0 back, then the server is not responding. Do you get an error when you run the command below?

Invoke-WebRequest -Uri https://mcs-098-web.america.com/api/ -UseBasicParsing -DisableKeepAlive

Please copy and paste the error into a reply using the pre-formatted </> button.

i already tried with the above command you given, but it is returning the status code 0. The URL I am accessing is a internal project url and the server where i am accessing doesn’t have internet enabled.

is it possible to get the url status code using below code…

$ie = New-Object -ComObject internetexplorer.application
$ie.navigate2(“https://mcs-098-web.america.com/api/”)
$ie.

What I want you to do is run that command outside of the function. Just type it into a PowerShell console. That will give you an error message that you can troubleshoot, like the one below which should tell you why you’re getting 0 returned.

Invoke-WebRequest : The remote name could not be resolved: 'idontexist.blah'
At line:1 char:1
+ Invoke-WebRequest -Uri https://idontexist.blah -UseBasicParsing -Disa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Alternative, change your catch to throw the full error:

catch [Net.WebException] {
    $_.Exception
}
2 Likes