使用智能卡身份验证或现有授权数据的 Powershell Invoke-WebRequest

Powershell Invoke-WebRequest using SmartCard authentication or existing authorization data

直到现在我习惯使用 Invoke-WebRequest 如下:

Invoke-WebRequest -u $url -header $header

$header 包含编码的用户名和密码。

自从网站转为使用 Active Directory,现在使用智能卡数据来授权访问

我如何使用 Invoke-WebRequest 和智能卡凭据?

谢谢

使用智能卡基本上等同于需要证书的网站。

我使用以下代码通过智能卡身份验证从 SharePoint 下载文件,您只需修改 Invoke-WebRequest 即可满足您的要求。

Add-Type -AssemblyName System.Security

# Filtering for cert requirements...
$ValidCerts = [System.Security.Cryptography.X509Certificates.X509Certificate2[]](dir Cert:\CurrentUser\My | where { $_.NotAfter -gt (Get-Date) })

# You could check $ValidCerts, and not do this prompt if it only contains 1...
$Cert = [System.Security.Cryptography.X509Certificates.X509Certificate2UI]::SelectFromCollection(
    $ValidCerts,
    'Choose a certificate',
    'Choose a certificate',
    'SingleSelection'
) | select -First 1

$WebRequestParams = @{
    Uri = $Url       # Uri to file to download
    OutFile = $Path  # Path to where file should be downloaded (include filename)
    Certificate = $Cert
}
Invoke-WebRequest @WebRequestParams