I have the following snippet below my script that uses WebRequest to send a list of web application and application servers, and I get random results based on the order in which the good / bad servers are listed in the server list.
For example, if bad servers (where I return the code 404 or 503) are listed first in the list, then my script seems to report accurately. However, if a good server (which returns status = "OK"), then my results are first listed 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 an example server list:
server1 (reports back a 404) server2 (reports back a 503) server3 (gets a status = "OK")
And here is the "exact" output of cmd 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 reorder the list of servers, where the first server is listed first, for example:
server3 (gets a status = "OK") server1 (reports back a 404) server2 (reports back a 503)
I get inaccurate results when server 1 and server 2 are 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 do I get mixed results depending on how the servers are listed?
Thanks in advance!