Powershell 能否提供有关 Invoke-WebRequest 使用的服务器证书的信息? [英] Can Powershell Give Me Information on the Server's Certificate Used By Invoke-WebRequest?

查看:73
本文介绍了Powershell 能否提供有关 Invoke-WebRequest 使用的服务器证书的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在 Powershell 4 中,

For instance, in Powershell 4,

$StackExAPIResponse = Invoke-WebRequest https://api.stackexchange.com/users/104624?site=serverfault -TimeoutSec 3 -ErrorAction Stop

该命令运行良好,但是,有什么方法可以让我同时获取有关该请求中使用的 SSL 证书的信息,例如哈希算法、到期日期等?

That command works perfectly well, but, is there any way for me to also get the information about the SSL certificate used in that request, such as hash algorithm, expiration date, etc.?

我也接受一些带有 Invoke-RestMethod 的东西......

I'd also accept something with Invoke-RestMethod, etc...

推荐答案

PowerShell cmdlet 没有执行此操作的本机方法.如果您需要证书信息,您可能必须下拉到 .Net 库.

The PowerShell cmdlets don't have a native way to do this. You will probably have to drop down to the .Net libraries if you want certificate information.

这样做的一种方法是使用System.Net.ServicePointManager 类.具体来说,FindServicePoint 方法.

One way of doing this will involve using the System.Net.ServicePointManager class. In specific, the FindServicePoint method.

这必须作为与 Invoke-WebRequest 分开的操作来完成.有关方法,请参阅 MSDN 文档和 返回值 有关您可以做什么的更多详细信息,但以下内容将为您提供您特别提到的数据.需要注意的一件事:我注意到,当我在实际调用 Invoke-WebRequestInvoke-RestMethod 之前尝试调用 FindServicePoint 时,服务点上的 Certificate 属性为空.

This will have to be done as a separate operation from the Invoke-WebRequest. See the MSDN documenation for the method and the return value for more details about what you can do, but the following would give you the data you specifically mentioned. One thing to note: I noticed that when I tried calling FindServicePoint prior to actually calling Invoke-WebRequest or Invoke-RestMethod, the Certificate property on the service point was null.

$StackExAPIResponse = Invoke-WebRequest https://api.stackexchange.com/users/104624?site=serverfault -TimeoutSec 3 -ErrorAction Stop
$servicePoint = [System.Net.ServicePointManager]::FindServicePoint("https://api.stackexchange.com")
$servicePoint.Certificate.GetCertHashString()
$servicePoint.Certificate.GetExpirationDateString()

这篇关于Powershell 能否提供有关 Invoke-WebRequest 使用的服务器证书的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆