我编写了一个脚本,该脚本将从excel电子表格中提取大量URL,然后发出Web请求并获取证书信息。
我对Powershell还是很陌生,从未尝试调试或处理异常/错误处理。
在我的过程中,脚本会挂起某些点,不会引发任何错误,并且不会终止,只是挂起了无限的时间。有时是几分钟,有时是几秒钟。
我试图弄清楚我是否可以更正我的代码中的某些内容,或者该挂起仅仅是由于我正在联系的服务器而引起的。
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
}
}