about_Certificate_Provider
提供程序名称
证书
驱动器
Cert:
功能
ShouldProcess
简短说明
提供对 PowerShell 中的 X.509 证书存储和证书的访问。
详细说明
此信息仅适用于在 Windows 上运行的 PowerShell。
借助 PowerShell 证书 提供程序,可以在 PowerShell 中获取、添加、更改、清除和删除证书和证书存储。
证书驱动器是一个分层命名空间,其中包含计算机上的证书存储和证书。
证书提供程序支持以下 cmdlet。
- Get-Location
- Set-Location
- Get-Item
- Get-ChildItem
- Invoke-Item
- Move-Item
- New-Item
- Remove-Item
- Get-ItemProperty
- Set-ItemProperty
- Clear-ItemProperty
- Get-AuthenticodeSignature
- Set-AuthenticodeSignature
此提供程序公开的类型
证书驱动器公开以下类型。
- Microsoft.PowerShell.Commands.X509StoreLocation,它是高级容器,用于对当前用户和所有用户的证书进行分组。 每个系统都有 一个
CurrentUser
和LocalMachine
(所有用户) 存储位置。 - System.Security.Cryptography.X509Certificates.X509Store,是保存和管理证书的物理存储。
- System.Security.Cryptography.X509Certificates.X509Certificate2,每个表示计算机上的 X.509 证书。 证书由其指纹标识。
导航证书驱动器
证书提供程序将证书命名空间公开为 Cert:
PowerShell 中的驱动器。 此命令使用 Set-Location
命令将当前位置更改为存储位置中的Root
LocalMachine
证书存储。 使用反斜杠 (\
) 或正斜杠 (/
) 指示驱动器的 Cert:
级别。
Set-Location Cert:
还可以使用任何其他 PowerShell 驱动器中的证书提供程序。 若要从其他位置引用别名,请在路径中使用 Cert:
驱动器名称。
PS Cert:\> Set-Location -Path LocalMachine\Root
若要返回到文件系统驱动器,请键入驱动器名称。 例如,键入:
Set-Location C:
注意
PowerShell 使用别名使你能够以熟悉的方式处理提供程序路径。 和 ls
等dir
命令现在是 Get-ChildItem 的别名,cd
是 Set-Location 的别名,是 pwd
Get-Location 的别名。
显示证书的内容: 驱动器
此命令使用 Get-ChildItem
cmdlet 在证书存储位置 CurrentUser
中显示证书存储。
如果你不在驱动器中 Cert:
,请使用绝对路径。
PS Cert:\CurrentUser\> Get-ChildItem
在 Cert: 驱动器中显示证书属性
此示例使用 Get-Item
获取证书,并将其存储在变量中。
该示例演示使用 Select-Object
(DnsNameList、EnhancedKeyUsageList、SendAsTrustedIssuer) 的新证书脚本属性。
$c = Get-Item cert:\LocalMachine\My\52A149D0393CE8A8D4AF0B172ED667A9E3A1F44E
$c | Format-List DnsNameList, EnhancedKeyUsageList, SendAsTrustedIssuer
DnsNameList : {SERVER01.contoso.com}
EnhancedKeyUsageList : {WiFi-Machine (1.3.6.1.4.1.311.42.2.6),
Client Authentication (1.3.6.1.5.5.7.3.2)}
SendAsTrustedIssuer : False
查找所有 CodeSigning 证书
此命令使用 cmdlet 的 Get-ChildItem
CodeSigningCert 和 Recurse 参数获取计算机上具有代码签名权限的所有证书。
Get-ChildItem -Path cert: -CodeSigningCert -Recurse
查找过期的证书
此命令使用 cmdlet 的 Get-ChildItem
ExpiringInDays 参数获取在接下来的 30 天内过期的证书。
Get-ChildItem -Path cert:\LocalMachine\WebHosting -ExpiringInDays 30
查找服务器 SSL 证书
此命令使用 cmdlet 的 Get-ChildItem
SSLServerAuthentication 参数获取 和 WebHosting
存储中的所有My
服务器 SSL 证书。
$getChildItemSplat = @{
Path = 'cert:\LocalMachine\My', 'cert:\LocalMachine\WebHosting'
SSLServerAuthentication = $true
}
Get-ChildItem @getChildItemSplat
在远程计算机上查找过期的证书
此命令使用 Invoke-Command
cmdlet 在 Srv01 和 Srv02 计算机上运行 Get-ChildItem
命令。 在 ExpiringInDays 参数中) 值零 0
(获取已过期的 Srv01 和 Srv02 计算机上的证书。
$invokeCommandSplat = @{
ComputerName = 'Srv01', 'Srv02'
ScriptBlock = {
Get-ChildItem -Path cert:\* -Recurse -ExpiringInDays 0
}
}
Invoke-Command @invokeCommandSplat
组合筛选器以查找一组特定的证书
此命令获取存储位置中 LocalMachine
具有以下属性的所有证书:
fabrikam
在其 DNS 名称中Client Authentication
在 EKU 中- SendAsTrustedIssuer 属性的 值为
$true
- 不要在接下来的 30 天内过期。
NotAfter 属性存储证书过期日期。
[DateTime] $ValidThrough = (Get-Date) + (New-TimeSpan -Days 30)
$getChildItemSplat = @{
Path = 'cert:\*'
Recurse = $true
DnsName = "*fabrikam*"
Eku = "*Client Authentication*"
}
Get-ChildItem @getChildItemSplat |
Where-Object {$_.SendAsTrustedIssuer -and $_.NotAfter -gt $ValidThrough }
打开证书 MMC 管理单元
cmdlet Invoke-Item
使用默认应用程序打开指定的路径。 对于证书,默认应用程序是证书 MMC 管理单元。
此命令将打开证书 MMC 管理单元以管理指定的证书。
Invoke-Item cert:\CurrentUser\my\6B8223358119BB08840DEE50FD8AF9EA776CE66B
复制证书
证书提供程序不支持复制 证书 。 尝试复制证书时,会看到此错误。
$path = "Cert:\LocalMachine\Root\E2C0F6662D3C569705B4B31FE2CBF3434094B254"
PS Cert:\LocalMachine\> Copy-Item -Path $path -Destination .\CA\
Copy-Item : Provider operation stopped because the provider doesn't support
this operation.
At line:1 char:1
+ Copy-Item -Path $path -Destination .\CA\
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotImplemented: (:) [Copy-Item],
PSNotSupportedException
+ FullyQualifiedErrorId : NotSupported,
Microsoft.PowerShell.Commands.CopyItemCommand
移动证书
将所有 SSL 服务器身份验证证书移动到 WebHosting 存储
此命令使用 Move-Item
cmdlet 将证书从 My
存储移动到 WebHosting
存储。
Move-Item
无法移动证书存储,并且无法将证书移动到其他存储位置,例如将证书从 LocalMachine
移动到 CurrentUser
。 cmdlet Move-Item
可以在存储中移动证书,但不会移动私钥。
此命令使用 cmdlet 的 Get-ChildItem
SSLServerAuthentication 参数获取证书存储中的 My
SSL 服务器身份验证证书。
返回的证书通过管道传递到 Move-Item
cmdlet,该 cmdlet 会将证书移动到存储区 WebHosting
。
Get-ChildItem cert:\LocalMachine\My -SSLServerAuthentication |
Move-Item -Destination cert:\LocalMachine\WebHosting
删除证书和私钥
cmdlet Remove-Item
会删除指定的证书。 DeleteKey 动态参数删除私钥。
从 CA 存储中删除证书
此命令从 CA 证书存储中删除证书,但会使相关联的私钥保持不变。
Cert:
在驱动器中Remove-Item
,cmdlet 仅支持 DeleteKey、Path、WhatIf 和 Confirm 参数。 忽略所有其他参数。
Remove-Item cert:\LocalMachine\CA\5DDC44652E62BF9AA1116DC41DE44AB47C87BDD0
使用 DNS 名称中的通配符删除证书
此命令删除 DNS 名称包含 Fabrikam
的所有证书。 它使用 cmdlet 的 Get-ChildItem
DNSName 参数获取证书,并使用 Remove-Item
cmdlet 将其删除。
Get-ChildItem -Path cert:\LocalMachine -DnsName *Fabrikam* | Remove-Item
从远程计算机中删除私钥
这一系列命令将启用委派,然后在远程计算机上删除该证书和相关联的私钥。 若要删除远程计算机上的私钥,你必须使用委派的凭据。
Enable-WSManCredSSP
使用 cmdlet 在 S1 远程计算机上的客户端上启用凭据安全服务提供程序 (CredSSP) 身份验证。
CredSSP 允许使用委派的身份验证。
Enable-WSManCredSSP -Role Client -DelegateComputer S1
Connect-WSMan
使用 cmdlet 将 S1 计算机连接到本地计算机上的 WinRM 服务。 此命令完成后,S1 计算机将显示在 PowerShell 的本地 WSMan:
驱动器中。
Connect-WSMan -ComputerName S1 -Credential Domain01\Admin01
现在,可以使用驱动器中的 Set-Item
WSMan:
cmdlet 为 WinRM 服务启用 CredSSP 属性。
Set-Item -Path WSMan:\S1\Service\Auth\CredSSP -Value $true
使用 New-PSSession
cmdlet 在 S1 计算机上启动远程会话,并指定 CredSSP 身份验证。 将会话保存在 变量中 $s
。
$s = New-PSSession S1 -Authentication CredSSP -Credential Domain01\Admin01
最后,使用 Invoke-Command
cmdlet 在 变量的会话中$s
运行Remove-Item
命令。 该 Remove-Item
命令使用 DeleteKey 参数删除私钥以及指定的证书。
Invoke-Command -Session $s {
$removeItemSplat = @{
Path = 'cert:\LocalMachine\My\D2D38EBA60CAA1C12055A2E1C83B15AD450110C2'
DeleteKey = $true
}
Remove-Item @removeItemSplat
}
删除过期的证书
此命令使用值为 0
的 cmdlet 的 Get-ChildItem
ExpiringInDays 参数来获取存储区中WebHosting
已过期的证书。
包含返回的证书的变量通过管道传递到 Remove-Item
cmdlet,该 cmdlet 会删除它们。 该命令使用 DeleteKey 参数删除私钥和证书。
$expired = Get-ChildItem cert:\LocalMachine\WebHosting -ExpiringInDays 0
$expired | Remove-Item -DeleteKey
创建证书
cmdlet New-Item
不会在证书提供程序中创建新 证书 。 使用 New-SelfSignedCertificate cmdlet 创建用于测试的证书。
创建证书存储
在 Cert:
驱动器中 New-Item
,cmdlet 在 LocalMachine
存储位置创建证书存储。 它支持 Name、 Path、 WhatIf 和 Confirm 参数。 忽略所有其他参数。 该命令返回表示新证书存储的 System.Security.Cryptography.X509Certificates.X509Store 。
此命令在存储位置中创建名为 CustomStore
LocalMachine
的新证书存储。
New-Item -Path cert:\LocalMachine\CustomStore
在远程计算机上Create新的证书存储
此命令在 Server01 计算机上的存储位置创建名为 HostingStore
LocalMachine
的新证书存储。
命令使用 Invoke-Command
cmdlet 在 Server01 计算机上运行 New-Item
命令。 该命令返回表示新证书存储的 System.Security.Cryptography.X509Certificates.X509Store 。
Invoke-Command -ComputerName Server01 -ScriptBlock {
New-Item -Path cert:\LocalMachine\CustomStore
}
为 WS-Man 创建客户端证书
此命令创建可由 WS-Management 客户端使用的 ClientCertificate 条目。 新的 ClientCertificate 在 ClientCertificate 目录 ClientCertificate_1234567890
下显示为 。 所有参数都是必需参数。 颁发者必须是颁发者证书的指纹。
$newItemSplat = @{
Path = 'WSMan:\localhost\ClientCertificate'
Credential = Get-Credential
Issuer = '1b3fd224d66c6413fe20d21e38b304226d192dfe'
URI = 'wmicimv2/*'
}
New-Item @newItemSplat
删除证书存储
从远程计算机中删除证书存储
此命令使用 Invoke-Command
cmdlet 在 S1 和 S2 计算机上运行 Remove-Item
命令。 命令 Remove-Item
包含 Recurse 参数,该参数在删除存储区之前删除存储中的证书。
Invoke-Command -ComputerName S1, S2 -ScriptBlock {
Remove-Item -Path cert:\LocalMachine\TestStore -Recurse
}
动态参数
动态参数是由 PowerShell 提供程序添加的 cmdlet 参数,仅在启用提供程序的驱动器中使用 cmdlet 时才可用。 这些参数在 证书 提供程序的所有子目录中都有效,但仅在证书上有效。
注意
针对 EnhancedKeyUsageList 属性执行筛选的参数还返回具有空 EnhancedKeyUsageList 属性值的项。 具有空 EnhancedKeyUsageList 的证书可用于所有目的。
以下证书提供程序参数已在 PowerShell 7.1 中重新引入。
- DNSName
- DocumentEncryptionCert
- EKU
- ExpiringInDays
- SSLServerAuthentication
CodeSigningCert <System.Management.Automation.SwitchParameter>
支持的 Cmdlet
此参数获取其 EnhancedKeyUsageList 属性值中具有 Code Signing
的证书。
DeleteKey <System.Management.Automation.SwitchParameter>
支持的 Cmdlet
此参数在删除证书时删除关联的私钥。
重要
若要删除与远程计算机上存储区中的 Cert:\CurrentUser
用户证书关联的私钥,必须使用委托凭据。 cmdlet Invoke-Command
支持使用 CredSSP 参数进行凭据委派。 在使用 Remove-Item
Invoke-Command
和 凭据委派之前,应考虑任何安全风险。
此参数在 PowerShell 7.1 中重新引入
DnsName <Microsoft.PowerShell.Commands.DnsNameRepresentation>
支持的 Cmdlet
此参数获取证书的 DNSNameList 属性中具有指定域名或名称模式的证书。 此参数的值可以是 Unicode
或 ASCII
。 Punycode 值将转换为 Unicode。 允许使用通配符 (*
) 。
此参数在 PowerShell 7.1 中重新引入
DocumentEncryptionCert <System.Management.Automation.SwitchParameter>
支持的 Cmdlet
此参数获取其 EnhancedKeyUsageList 属性值中具有 Document Encryption
的证书。
EKU <System.String>
支持的 Cmdlet
此参数获取证书的 EnhancedKeyUsageList 属性中具有指定文本或文本模式的证书。 允许使用通配符 (*
) 。 EnhancedKeyUsageList 属性包含 EKU 的友好名称和 OID 字段。
此参数在 PowerShell 7.1 中重新引入
ExpiringInDays <System.Int32>
支持的 Cmdlet
此参数获取在指定天数内或之前过期的证书。 值为 0 (0) 获取已过期的证书。
此参数在 PowerShell 7.1 中重新引入
ItemType <System.String>
此参数用于指定 由 New-Item
创建的项的类型。 cmdlet New-Item
仅支持值 Store
。 New-Item
cmdlet 无法创建新证书。
支持的 Cmdlet
SSLServerAuthentication <System.Management.Automation.SwitchParameter>
支持的 Cmdlet
仅获取用于 SSL Web 托管的服务器证书。 此参数获取其 EnhancedKeyUsageList 属性值中具有 Server Authentication
的证书。
此参数在 PowerShell 7.1 中重新引入
脚本属性
新的脚本属性已添加到表示证书的 x509Certificate2 对象中,以便于搜索和管理证书。
- DnsNameList:若要填充 DnsNameList 属性,证书提供程序将从 SUBJECTAlternativeName (SAN) 扩展中的 DNSName 条目复制内容。 如果 SAN 扩展为空,则使用证书的 Subject 字段中的内容填充该属性。
- EnhancedKeyUsageList:若要填充 EnhancedKeyUsageList 属性,证书提供程序复制证书中 EnhancedKeyUsage (EKU) 字段的 OID 属性,并为其创建友好名称。
- SendAsTrustedIssuer:为了填充 SendAsTrustedIssuer 属性,证书提供程序从证书中复制 SendAsTrustedIssuer 属性。 有关详细信息 ,请参阅管理客户端身份验证的受信任颁发者。
这些新功能允许你根据证书的 DNS 名称和到期日期搜索证书,并通过证书的增强型密钥使用 (EKU) 属性的值将客户端和服务器身份验证证书区分开来。
使用管道
提供程序 cmdlet 接受管道输入。 可以通过将提供程序数据从一个 cmdlet 发送到另一个提供程序 cmdlet 来使用管道来简化任务。 若要详细了解如何将管道与提供程序 cmdlet 配合使用,请参阅本文中提供的 cmdlet 参考。
获取帮助
从 PowerShell 3.0 开始,你可以获取提供程序 cmdlet 的自定义帮助主题,这些 cmdlet 在文件系统驱动器中的行为方式。
若要获取为文件系统驱动器自定义的帮助主题,请在文件系统驱动器中运行 Get-Help 命令,或使用 -Path
的 参数 Get-Help
指定文件系统驱动器。
Get-Help Get-ChildItem
Get-Help Get-ChildItem -Path cert: