Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell调用RestMethod证书验证_Rest_Powershell - Fatal编程技术网

Powershell调用RestMethod证书验证

Powershell调用RestMethod证书验证,rest,powershell,Rest,Powershell,我一直在使用一个已经工作了一年没有问题的脚本,调用RestMethod调用一个安全(https)站点,但现在失败了。经过大量测试后,我发现我可以使调用在任何安全站点请求中一致失败且可重复,但我不知道为什么。以下代码失败 [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} Invoke-RestMethod -Uri https://blogs.msdn.com/powershell/rss

我一直在使用一个已经工作了一年没有问题的脚本,调用RestMethod调用一个安全(https)站点,但现在失败了。经过大量测试后,我发现我可以使调用在任何安全站点请求中一致失败且可重复,但我不知道为什么。以下代码失败

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
Invoke-RestMethod -Uri https://blogs.msdn.com/powershell/rss.aspx | Format-Table -Property Title, pubDate
产生的错误如下:

Invoke RestMethod:基础连接已关闭:发送时发生意外错误。 第1行字符:1 +调用RestMethod-Uri |格式表。。。 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:InvalidOperation:(System.Net.HttpWebRequest:HttpWebRequest)[调用RestMethod],WebException +FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

我发现如果我包括

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
在上面的一行中,我得到了前面显示的错误。上述行为和错误实际上与应该发生的相反。以前,如果不将ServerCertificateValidationCallback指定为true,我无法发出https请求

我曾在多台计算机上,在不同的Windows用户配置文件下尝试过使用管理权限启动PowerShell,但我发现添加上一行可以重现问题。为了使其正常工作,我必须关闭PowerShell,重新打开它并删除ServerCertificateValidationCallback行。如果我将该行添加到代码中,则返回错误

我最后一次运行代码是在两周前,所以这是我所能确定的时间范围。你知道为什么现在失败了吗


请注意,这不是我最初遇到问题的站点,但是,我所面临的问题可以用上面的代码重现。

查看网站上的解决方案

基于此,我开始使用这些:

function Disable-SslVerification
{
    if (-not ([System.Management.Automation.PSTypeName]"TrustEverything").Type)
    {
        Add-Type -TypeDefinition  @"
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public static class TrustEverything
{
    private static bool ValidationCallback(object sender, X509Certificate certificate, X509Chain chain,
        SslPolicyErrors sslPolicyErrors) { return true; }
    public static void SetCallback() { System.Net.ServicePointManager.ServerCertificateValidationCallback = ValidationCallback; }
    public static void UnsetCallback() { System.Net.ServicePointManager.ServerCertificateValidationCallback = null; }
}
"@
    }
    [TrustEverything]::SetCallback()
}
function Enable-SslVerification
{
    if (([System.Management.Automation.PSTypeName]"TrustEverything").Type)
    {
        [TrustEverything]::UnsetCallback()
    }
}
坏消息是,根据更新,我回到StackOverflow,因为这周某个时候它停止工作了


如果您使用的是较旧版本的Powershell,那么您可能仍然能够很好地使用我发布的代码。

请查看网站上的解决方案

基于此,我开始使用这些:

function Disable-SslVerification
{
    if (-not ([System.Management.Automation.PSTypeName]"TrustEverything").Type)
    {
        Add-Type -TypeDefinition  @"
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public static class TrustEverything
{
    private static bool ValidationCallback(object sender, X509Certificate certificate, X509Chain chain,
        SslPolicyErrors sslPolicyErrors) { return true; }
    public static void SetCallback() { System.Net.ServicePointManager.ServerCertificateValidationCallback = ValidationCallback; }
    public static void UnsetCallback() { System.Net.ServicePointManager.ServerCertificateValidationCallback = null; }
}
"@
    }
    [TrustEverything]::SetCallback()
}
function Enable-SslVerification
{
    if (([System.Management.Automation.PSTypeName]"TrustEverything").Type)
    {
        [TrustEverything]::UnsetCallback()
    }
}
坏消息是,根据更新,我回到StackOverflow,因为这周某个时候它停止工作了

如果您使用的是较旧版本的Powershell,那么您可能仍然能够很好地使用我发布的代码