我的脚本中有以下代码片段,它使用WebRequest来ping web/应用程序服务器列表,并根据服务器列表中列出的好/坏服务器的顺序随机得到结果。
例如,如果坏服务器(我得到的代码是404或503)列在列表中的第一位,那么我的脚本似乎可以准确地报告。但是,如果首先列出的是良好的服务器(返回的状态为"OK"),则我的结果是不准确的。
下面是我的代码片段:
$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!!!"
}
}以下是示例服务器列表:
server1 (reports back a 404)
server2 (reports back a 503)
server3 (gets a status = "OK")下面是我运行脚本时“准确”的cmd输出:
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.现在,当我重新排序服务器列表时,首先列出的是好的服务器,如下所示:
server3 (gets a status = "OK")
server1 (reports back a 404)
server2 (reports back a 503)我得到不准确的结果,其中服务器1和服务器2被报告为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.为什么我会根据服务器的列出方式得到不同的结果?
提前感谢!
发布于 2012-02-01 12:49:30
这是因为WebRequest在不成功的尝试时引发异常,而$response并没有真正在你得到503,404等的情况下被设置。当你获得成功时,response被设置为响应(状态码为OK),但是如果下一个服务器引发404,那么响应仍然是OK状态,因为WebRequest只引发异常而不给$response分配任何东西。您可能希望对异常执行trap (或try catch)并处理成功或失败,或者在每次循环迭代中将$response设置为null:
foreach($server in $serverlist){
$response=$null
...此外,不要使用$_作为迭代变量,它是自动变量,不适合在这里使用它。
请注意,您所写的内容也可能会占用系统资源(它为我挂起了一次),因此请适当地处理响应。$response = $null可能就足够了,但您可能仍然需要在循环结束时正确地关闭响应。
发布于 2012-02-01 12:54:01
您可能需要关闭连接:
$response.Close()http://msdn.microsoft.com/en-us/library/system.net.webresponse.close(v=vs.90).aspx
我会这样写:
foreach ($server in $ServerList) {
try {
$url = "http://${server}.domain.net/logon"
Write-Host "Pinging web address for server: $url ..."
$request = [System.Net.WebRequest]::Create($url)
$response = $request.GetResponse()
Write-Host "Web Ping on $server Succeeded."
} catch {
Write-Host ("Web Ping on $server FAILED!!! The error was '{0}'." -f $_)
} finally {
if ($response) {
$response.Close()
Remove-Variable response
}
}
}https://stackoverflow.com/questions/9090849
复制相似问题