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..
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.
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?
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…
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: