Powershell hanging in script

半腔热情 提交于 2020-01-16 14:52:51

问题


I have written a script that will extract a large number of URLs from an excel spreadsheet and then make a web request and fetch the certificate information.

I am very new to powershell and have never worked to debug or process exception/error handling.

In my process there are certain points the script will just hang, no error is thrown and it does not terminate, just hangs for and indefinite period of time. Sometimes its a couple minutes other times a couple seconds.

I am trying to figure out if I can correct something in my code or if this hang is simply because of the servers I am contacting.

function GetCertificates($url) {
# // create a request
$req = [System.Net.WebRequest]::create($url)
$req.Method = "GET"
$req.Timeout = 600000 # = 10 minutes

# // set the proxy for connection
$proxy = New-Object System.Net.WebProxy "proxy.com:8000"
$req.Proxy = $proxy

# // Set if you need a username/password to access the resource
$Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
#$req.Credentials = New-Object Net.NetworkCredential("username", "password");
$req.Credentials = $Credentials

$req.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT; Windows NT 6.1; en-US)"

try { 
    [Net.HttpWebResponse] $result = $req.GetResponse()
} 
catch {}

# // define the cert 
$cert = ''
$cert = $req.ServicePoint.Certificate

$httpstatuscode = [int]$result.StatusCode

try {
    [IO.Stream] $stream = $result.GetResponseStream()
    [IO.StreamReader] $reader = New-Object IO.StreamReader($stream)
    [string] $output = $reader.readToEnd()
    $stream.flush()
    $stream.close()
}
catch {}

# // write the results

Write-host $url
Write-host "Issued By : " $cert.Issuer
#Write-host "Subject : " $cert.Subject
#Write-host "Issued On : " $cert.GetEffectiveDateString()
#Write-host "Expires On : " $cert.GetExpirationDateString()
}

function GetExcel{
# // import the csv file
$xcel = import-csv C:\Users\Desktop\excel.csv

# // extract and run the URLs for only required TYPEs
$xcel | foreach{
    if
        ($_.TYPE -like "u_external_url" -or "qa_url" -or "u_load_balancing_url")
    {GetCertificates $_.URL}
    return $_.APP_ID
}
}

来源:https://stackoverflow.com/questions/57561989/powershell-hanging-in-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!