我的脚本中有以下片段,该脚本使用 WebRequest 来 ping 网络/应用服务器列表,并且我根据服务器列表中列出的好/坏服务器的顺序获得随机结果。
例如,如果坏服务器(我返回 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 被报告为正常:
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.
为什么我会根据服务器的列出方式得到不同的结果?
提前致谢!
最佳答案
您可能需要关闭连接:
$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
}
}
}
关于powershell - 使用 PowerShell 随机 WebRequest 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9090849/