使用 PowerShell 随机 WebRequest 结果 [英] Random WebRequest results with PowerShell

查看:18
本文介绍了使用 PowerShell 随机 WebRequest 结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本中有以下片段,该脚本使用 WebRequest 来 ping 网络/应用服务器列表,我根据服务器列表中列出的好/坏服务器的顺序获得随机结果.

例如,如果坏服务器(我返回 404 或 503 的代码)首先列在列表中,那么我的脚本似乎可以准确报告.但是,如果首先列出了良好的服务器(返回状态 =OK"),那么我的结果是不准确的.

这是我的代码片段:

$ServerList = gc "$pwd\servers\test_servers.lst"ForEach($ServerList 中的 $_){# Ping web 服务器测试$url = "http://$_.domain.net/logon"Write-Host "Ping 服务器的网址:$url ..."$request = [System.Net.WebRequest]::Create($url)$response = $request.GetResponse()如果 ($response.StatusCode -eq "OK"){#$真写入主机$_ 上的 Web Ping 成功."}别的{#$假写主机$_ 上的 Web Ping 失败!!!"}}

这是示例服务器列表:

server1(返回 404)server2(返回 503)server3(获取状态 =OK")

这是我运行脚本时准确"的 cmd 输出:

C:\TFS\Sandbox>powershell ./temp.ps1ping 服务器网址:http://server1.domain.net/wfc/logon ...使用0"参数调用GetResponse"的异常:远程服务器返回编辑错误:(404)未找到."在 C:\TFS\Sandbox\temp.ps1:8 字符:34+ $response = $request.GetResponse <<<<()+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException+ FullQualifiedErrorId:DotNetMethodException服务器 1 上的 Web Ping 失败!!!ping 服务器网址:http://server2.domain.net/wfc/logon ...使用0"参数调用GetResponse"的异常:远程服务器返回编辑错误:(503)服务器不可用."在 C:\TFS\Sandbox\temp.ps1:8 字符:34+ $response = $request.GetResponse <<<<()+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException+ FullQualifiedErrorId:DotNetMethodException服务器 2 上的 Web Ping 失败!!!ping 服务器网址:http://server3.domain.net/wfc/logon ...服务器 3 上的 Web Ping 成功.

现在,当我重新排列服务器列表时,首先列出好的服务器,如下所示:

server3(得到一个状态=OK")server1(返回 404 报告)server2(返回 503)

我得到的结果不准确,其中服务器 1 和服务器 2 被报告为正常:

ping 服务器网址:http://server3.domain.net/wfc/logon ...服务器 3 上的 Web Ping 成功.ping 服务器网址:http://server1.domain.net/wfc/logon ...使用0"参数调用GetResponse"的异常:远程服务器返回编辑错误:(404)未找到."在 C:\TFS\Sandbox\temp.ps1:8 字符:34+ $response = $request.GetResponse <<<<()+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException+ FullQualifiedErrorId:DotNetMethodException服务器 1 上的 Web Ping 成功.ping 服务器网址:http://server2.domain.net/wfc/logon ...使用0"参数调用GetResponse"的异常:远程服务器返回编辑错误:(503)服务器不可用."在 C:\TFS\Sandbox\temp.ps1:8 字符:34+ $response = $request.GetResponse <<<<()+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException+ FullQualifiedErrorId:DotNetMethodException服务器 2 上的 Web Ping 成功.

为什么根据服务器的列出方式,我会得到不同的结果?

提前致谢!

解决方案

这是因为 WebRequest 在尝试失败时引发异常,而 $response 并没有真正得到设置在您获得 503404 等的情况下.当您成功时,响应设置为响应(状态代码为 OK),但是,如果下一个服务器引发 404,例如,响应仍将是 OK 状态,因为 WebRequest 仅引发异常并且不会为 $response 分配任何内容.您可能希望 trap(或 try catch)异常并在那里处理成功或失败,或者在每次迭代中将 $response 设置为 null循环:

foreach($serverlist 中的 $server){$响应=$null...

另外,不要使用$_作为迭代变量,它是自动变量,在这里使用它不是很好的形式.

请注意,您编写的内容也可能会耗尽系统资源(对我来说它挂了一次),因此请适当处理响应.$response = $null 可能就足够了,但您可能仍然需要在循环结束时正确关闭响应.

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!

解决方案

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.

这篇关于使用 PowerShell 随机 WebRequest 结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆