问题
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