Trying to get Certificate Information in Powershel

2019-08-27 02:11发布

问题:

I recently posted some code here : Powershell hanging in script

And someone recommended using Invoke-WebRequest instead of the way I was getting the cert information to make it clean and short.

I was able to get the Invoke-WebRequest to work but can't figure out how to get the cert information.

I found this link : Can Powershell Give Me Information on the Server's Certificate Used By Invoke-WebRequest?

But this does not seem to be working for me. What am I doing wrong?

$url = "https://google.com"
$req = Invoke-WebRequest -Uri $url -Proxy $proxy -ProxyUseDefaultCredentials
$servicepoint = [System.Net.ServicePointManager]::FindServicePoint($url)
$servicepoint.Certificate.GetExpirationDateString()
$servicepoint.Certificate.Subject
$servicepoint.Certificate.Issuer
You cannot call a method on a null-valued expression.
At line:5 char:1
+ $servicepoint.Certificate.GetExpirationDateString()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

I also tried to do it exactly as the test and not use the $url variable and instead type it in, and received the same error.

$req = Invoke-WebRequest -Uri https://google.com -Proxy $proxy -ProxyUseDefaultCredentials
$servicepoint = [System.Net.ServicePointManager]::FindServicePoint("https://google.com")
$servicepoint.Certificate.GetExpirationDateString()
$servicepoint.Certificate.Subject
$servicepoint.Certificate.Issuer
You cannot call a method on a null-valued expression.
At line:4 char:1
+ $servicepoint.Certificate.GetExpirationDateString()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull