使用PowerShell进行随机WebRequest结果

Kei*_*ith 3 powershell webrequest

我的脚本下面有以下片段,它使用WebRequest来ping一个Web/app服务器列表,我根据服务器列表中列出好/坏服务器的顺序得到随机结果.

例如,如果列表中首先列出了错误的服务器(我收回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!!!"
}       
}
Run Code Online (Sandbox Code Playgroud)

这是示例服务器列表:

server1 (reports back a 404)
server2 (reports back a 503)
server3 (gets a status = "OK")
Run Code Online (Sandbox Code Playgroud)

这是我运行脚本时的"准确"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.
Run Code Online (Sandbox Code Playgroud)

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

server3 (gets a status = "OK")    
server1 (reports back a 404)
server2 (reports back a 503)
Run Code Online (Sandbox Code Playgroud)

我得到的结果不准确,服务器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.
Run Code Online (Sandbox Code Playgroud)

为什么我根据服务器的列出方式获得混合结果?

提前致谢!

And*_*ndi 10

您可能需要关闭连接:

$response.Close()
Run Code Online (Sandbox Code Playgroud)

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)