Invoke-WebRequest:请求已中止:使用-Certificate时无法创建SSL / TLS安全通道

时间:2015-07-16 10:25:45

标签: windows powershell ssl amazon-web-services

尝试在PowerShell中使用Invoke-WebRequest和-Certificate参数来使用客户端证书时,我收到错误:

Invoke-WebRequest : The request was aborted: Could not create SSL/TLS secure channel. 
At C:\Windows\Temp\PO_WorkingDir_11a96bd3-e2b3-4016-b2b8-5977c92d0bdc\PO_PSScript_11a96bd3-e2b3-4016-b2b8-5977c92d0bdc.ps1:11 char:4
$r=Invoke-WebRequest -uri "$a1/tenants/$a2" -Headers $headers -Certificate C:\Us...
CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException    
FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

同一个脚本在本地工作,但在埃森哲工具中会抛出上述错误。 脚本:

$r=Invoke-WebRequest -uri "$a1/tenants/$a2" -Headers $headers -Certificate C:\Users\env1.ciac.pouser.AWSVA1011\Desktop\iapiclient-cpo.acp.env1.acp.aws.accenture.com\iapiclient-cpo.acp.env1.acp.aws.accenture.com\iapiclient-cpo.acp.env1_CERT.pem -ContentType application/json -Method GET

我在这里做错了吗?该工具使用应用程序用户,并且所有脚本都针对此用户运行,该用户是管理员。

1 个答案:

答案 0 :(得分:0)

我们遇到了同样的问题,发现它可能是由于剧本没有在" Elevated"具有管理员权限的会话。仅仅因为用户是管理员并不意味着脚本始终使用管理员权限运行。您可以在脚本中添加以下内容,以检测您当前是否具有管理员权限:

function Test-IsLocalAdministrator {
    $identity  = [System.Security.Principal.WindowsIdentity]::GetCurrent()
    $principal = New-Object System.Security.Principal.WindowsPrincipal( $identity )
    return $principal.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}

问题似乎是您无权使用所使用证书的私钥。在我们的例子中,我们使用Machine证书来设置双向TLS身份验证,而非管理用户则没有对PrivateKey属性的权限。

$ValidAt = [DateTime]::Now
$certs = @(gci Cert:\LocalMachine\My | ?{$_.NotBefore -lt $ValidAt -and $_.NotAfter -gt $ValidAt -and $_.Subject -eq ("CN={0}" -f [System.Net.Dns]::GetHostByName($env:computername).HostName)})
$Cert = $certs | sort NotAfter -Descending | select -first 1
$Cert.PrivateKey #This would return $null for non-admins

Invoke-WebRequest提供的The request was aborted: Could not create SSL/TLS secure channel.错误消息非常通用,但在这种情况下,它可能意味着当前会话无法访问私钥。

尝试以管理员身份运行脚本或明确授予用户帐户私钥的权限。