Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7197181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:55:33+00:00 2026-05-28T20:55:33+00:00

I have the following snippet below from my script that’s using a WebRequest to

  • 0

I have the following snippet below from my script that’s using a WebRequest to ping a list of web/app servers and I’m getting random results based on the order the good/bad servers are listed in the server list.

For example, if the bad servers (where I get back a code of 404 or 503) are listed first in the list then my script seems to report accurately. However, if the good server (which gets back a status = “OK”) is listed first then my results are inaccurate.

Here is my code snippet:

$ServerList = gc "$pwd\servers\test_servers.lst"
ForEach ($_ in $ServerList)
{   
# Ping web server test
$url = "http://$_.domain.net/logon"
Write-Host "Pinging web address for server: $url ..."
$request = [System.Net.WebRequest]::Create($url)
$response = $request.GetResponse()
If ($response.StatusCode -eq "OK") 
{
    #$True
    Write-Host "Web Ping on $_ Succeeded."
} 
Else 
{
    #$False
    Write-Host "Web Ping on $_ FAILED!!!"
}       
}

Here is the example server list:

server1 (reports back a 404)
server2 (reports back a 503)
server3 (gets a status = "OK")

And here is the “accurate” cmd output when I run the script:

C:\TFS\Sandbox>powershell ./temp.ps1

Pinging web address for server: http://server1.domain.net/wfc/logon ...
Exception calling "GetResponse" with "0" argument(s): "The remote server return
ed an error: (404) Not Found."
At C:\TFS\Sandbox\temp.ps1:8 char:34
+     $response = $request.GetResponse <<<< ()
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Web Ping on server1 FAILED!!!

Pinging web address for server: http://server2.domain.net/wfc/logon ...
Exception calling "GetResponse" with "0" argument(s): "The remote server return
ed an error: (503) Server Unavailable."
At C:\TFS\Sandbox\temp.ps1:8 char:34
+     $response = $request.GetResponse <<<< ()
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Web Ping on server2 FAILED!!!

Pinging web address for server: http://server3.domain.net/wfc/logon ...
Web Ping on server3 Succeeded.

Now when I re-order the server list where the good server is listed first, like so:

server3 (gets a status = "OK")    
server1 (reports back a 404)
server2 (reports back a 503)

I get inaccurate results where server 1 and server 2 are getting reported as OK:

Pinging web address for server: http://server3.domain.net/wfc/logon ...
Web Ping on server3 Succeeded.

Pinging web address for server: http://server1.domain.net/wfc/logon ...
Exception calling "GetResponse" with "0" argument(s): "The remote server return
ed an error: (404) Not Found."
At C:\TFS\Sandbox\temp.ps1:8 char:34
+     $response = $request.GetResponse <<<< ()
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Web Ping on server1 Succeeded.

Pinging web address for server: http://server2.domain.net/wfc/logon ...
Exception calling "GetResponse" with "0" argument(s): "The remote server return
ed an error: (503) Server Unavailable."
At C:\TFS\Sandbox\temp.ps1:8 char:34
+     $response = $request.GetResponse <<<< ()
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Web Ping on server2 Succeeded.

Why am I getting mixed results based on how the servers are listed?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:55:35+00:00Added an answer on May 28, 2026 at 8:55 pm

    It is because the the WebRequest raises exception on unsuccessful attempts and $response doesn’t really get set in the cases where you get 503, 404 etc. When you do get a success, response is set to, well, the response ( with status code being OK), but if the next server raises a 404, say, response would still be the OK state because the WebRequest only raises an exception and doesn’t assign anything to $response. You might want to trap (or try catch) the exception and handle the success or failure there or set $response to null in each iteration of the loop:

    foreach($server in $serverlist){
    $response=$null
    
    ...
    

    Also, don’t use $_ as the iteration variable, it is automatic variable and is not good form using it here.

    Note that what yo have written also might use up system resources, ( it hung once for me), so dispose off the response appropriately. $response = $null might be enough, but you may still have to close the response properly at end of loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following snippet is from a little app I wrote using the Qt framework.
I have the following snippet in my pom.xml (Full pom attached below which can
I have following code snippet that i use to compile class at the run
I have the following snippet of code that's generating the Use new keyword if
I have the following snippet from my code: switch ($extention) { case gif: $src
I have the following snippet: string base= tag1[j]; That gives the invalid conversion error.
Lets say I have the following code snippet below, how do I also apply
I have the following code snippet, which loads data from a CSV file into
I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
I have the following snippet in one of my html pages : <div class=inputboximage>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.