提供者名称
证书
驱动器
Cert:
能力
ShouldProcess
简短说明
提供对 PowerShell 中 X.509 证书存储和证书的访问权限。
详细说明
PowerShell 证书 提供程序允许你在 PowerShell 中获取、添加、更改、清除和删除证书和证书存储。
证书驱动器是一个分层命名空间,其中包含计算机上的证书存储和证书。
证书提供程序支持以下 cmdlet,本文将介绍这些 cmdlet。
- 获取位置
- Set-Location (设置位置)
- Get-Item (获取项)
- Get-ChildItem
- 调用项
- 移动项
- 新建项目
- 删除项
- Get-ItemProperty
- Set-Item属性
- Clear-ItemProperty
- Get-AuthenticodeSignature
- Set-AuthenticodeSignature
此提供方公开的类型
证书驱动器公开以下类型。
存储位置 (Microsoft.PowerShell.Commands.X509StoreLocation),它们是对当前用户和所有用户的证书进行分组的高级容器。 每个系统都有一个 CurrentUser 和 LocalMachine(所有用户)存储位置。
证书存储 (System.Security.Cryptography.X509Certificates.X509Store),这是保存和管理证书的物理存储。
X.509 System.Security.Cryptography.X509Certificates.X509Certificate2 证书,每个证书都表示计算机上的 X.509 证书。 证书由指纹标识。
导航证书驱动器
证书 提供程序在 PowerShell 中将证书命名空间公开为 Cert: 驱动器。 此命令使用命令 Set-Location 将当前位置更改为 LocalMachine 存储位置中的 Root 证书存储。 使用反斜杠 (\) 或正斜杠 (/) 表示驱动器的 Cert: 级别。
Set-Location Cert:
还可以从任何其他 PowerShell 驱动器使用证书提供程序。 若要从其他位置引用别名,请使用路径中的 Cert: 驱动器名称。
PS Cert:\> Set-Location -Path LocalMachine\Root
若要返回到文件系统驱动器,请键入驱动器名称。 例如,键入:
Set-Location C:
注释
PowerShell 使用别名来让你熟悉如何使用提供程序路径。
dir 和 ls 等命令现在是 Get-ChildItem的别名,cd 则是 Set-Location的别名。
pwd 是 Get-Location的别名。
显示证书:驱动器的内容
新的动态参数 DnsName、 、 EKUSSLServerAuthentication和 ExpiringInDays 已添加到Get-ChildItem驱动器中的 Cert: cmdlet 中。 新的动态参数在 Windows PowerShell 3.0 和更新的 PowerShell 版本中可用。 提供程序参数适用于 Windows Server 2012 及更高版本上的 IIS 8.0。
此命令使用 Get-ChildItem cmdlet 显示 CurrentUser 证书存储位置中的证书存储。
如果您不在驱动器中 Cert: ,请使用绝对路径。
PS Cert:\CurrentUser\> Get-ChildItem
在证书: 驱动器中显示证书属性
此示例获取具有 Get-Item 的证书,并将其存储在变量中。
该示例显示新的证书脚本属性(DnsNameList,EnhancedKeyUsageList,SendAsTrustedIssuer),使用 Select-Object。
$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-ChildItemCodeSigningCert 和 Recurse 参数来获取计算机上具有代码签名机构的所有证书。
Get-ChildItem -Path cert: -CodeSigningCert -Recurse
查找过期的证书
此命令使用 cmdlet 的 Get-ChildItemExpiringInDays 参数来获取将在接下来的 30 天内过期的证书。
Get-ChildItem -Path cert:\LocalMachine\WebHosting -ExpiringInDays 30
查找服务器 SSL 证书
此命令使用 cmdlet 的 Get-ChildItemSSLServerAuthentication 参数来获取 My 和 WebHosting 存储中的所有服务器 SSL 证书。
Get-ChildItem -Path cert:\LocalMachine\My, cert:\LocalMachine\WebHosting `
-SSLServerAuthentication
在远程计算机上查找过期的证书
此命令使用 Invoke-Command cmdlet 在 Srv01 和 Srv02 计算机上运行 Get-ChildItem 命令。
ExpiringInDays 参数中的值零 (0) 将获取已过期的 Srv01 和 Srv02 计算机上的证书。
Invoke-Command -ComputerName Srv01, Srv02 {Get-ChildItem -Path cert:\* `
-Recurse -ExpiringInDays 0}
组合筛选器以查找特定证书集
此命令获取 LocalMachine 存储位置中具有以下属性的所有证书:
- DNS 名称中的“fabrikam”
- EKU 中的“客户端身份验证”
-
$true属性的值 - 在接下来的 30 天内不会过期。
NotAfter 属性存储证书过期日期。
Get-ChildItem -Path cert:\* -Recurse -DNSName "*fabrikam*" `
-EKU "*Client Authentication*" | Where-Object {
$_.SendAsTrustedIssuer -and `
$_.NotAfter -gt (get-date).AddDays.(30)
}
打开证书 MMC 管理单元
该 Invoke-Item cmdlet 将使用默认应用程序打开您指定的路径。 对于证书,默认应用程序是证书 MMC 管理单元。
此命令将打开 Certificates 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 does not 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 store 移动到 WebHosting store。
Move-Item 不会移动证书存储,也不会将证书移动到其他存储位置,例如将证书从 LocalMachine 移动到 CurrentUser。 cmdlet Move-Item 移动证书,但不会移动私钥。
此命令使用 cmdlet 的 Get-ChildItemSSLServerAuthentication 参数来获取 MY 证书存储中的 SSL 服务器身份验证证书。
返回的证书通过管道传输到 Move-Item cmdlet,cmdlet 将证书移动到 WebHosting 存储。
Get-ChildItem cert:\LocalMachine\My -SSLServerAuthentication | Move-Item `
-Destination cert:\LocalMachine\WebHosting
删除证书和私钥
该 Remove-Item cmdlet 将删除您指定的证书。 dynamic -DeleteKey 参数删除私钥。
从 CA 存储中删除证书
此命令从 CA 证书存储中删除证书,但保留关联的私钥不变。
在 Cert: 驱动器中,Remove-Item cmdlet 仅支持 DeleteKey、Path、WhatIf和 Confirm 参数。 将忽略所有其他参数。
Remove-Item cert:\LocalMachine\CA\5DDC44652E62BF9AA1116DC41DE44AB47C87BDD0
使用 DNS 名称中的通配符删除证书
此命令将删除 DNS 名称包含“Fabrikam”的所有证书。 它使用 cmdlet 的 Get-ChildItem 参数来获取证书和 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
现在,您可以使用 WSMan: 驱动器中的 Set-Item 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 在 Remove-Item 变量中的会话中运行 $s 命令。
Remove-Item 命令使用 DeleteKey 参数删除私钥以及指定的证书。
Invoke-Command -Session $s { Remove-Item `
-Path cert:\LocalMachine\My\D2D38EBA60CAA1C12055A2E1C83B15AD450110C2 `
-DeleteKey
}
删除过期的证书
此命令使用值为 0 的 Get-ChildItem cmdlet 的 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: 驱动器中,cmdlet New-Item 在 LocalMachine 存储位置创建证书存储。 它支持 名称、Path、WhatIf,以及 确认 参数。 将忽略所有其他参数。 该命令返回表示新证书存储的 System.Security.Cryptography.X509Certificates.X509Store。
此命令在 LocalMachine 存储位置创建名为“CustomStore”的新证书存储。
New-Item -Path cert:\LocalMachine\CustomStore
在远程计算机上创建新的证书存储
此命令在 Server01 计算机上的 LocalMachine 存储位置创建名为“HostingStore”的新证书存储。
该命令使用 Invoke-Command cmdlet 在 Server01 计算机上运行 New-Item 命令。 该命令返回表示新证书存储的 System.Security.Cryptography.X509Certificates.X509Store。
Invoke-Command { New-Item -Path cert:\LocalMachine\CustomStore } `
-ComputerName Server01
为 WS-Man 创建客户端证书
此命令创建 ClientCertificate 条目,WS-Management 客户端可以使用该条目。 新的 ClientCertificate 将显示在 ClientCertificate 目录中显示为“ClientCertificate_1234567890”。 所有参数都是必需的。 颁发者 必须是颁发者证书的指纹。
$cred = Get-Credential
New-Item -Path WSMan:\localhost\ClientCertificate `
-Issuer 1b3fd224d66c6413fe20d21e38b304226d192dfe `
-URI wmicimv2/* -Credential $cred
删除证书存储
从远程计算机中删除证书存储
此命令使用 Invoke-Command cmdlet 在 S1 和 S2 计算机上运行 Remove-Item 命令。
Remove-Item 命令包括 recurse 参数,该参数在删除存储前删除存储中的证书。
Invoke-Command { Remove-Item -Path cert:\LocalMachine\TestStore -Recurse } `
-ComputerName S1, S2
动态参数
动态参数是由 PowerShell 提供程序添加的 cmdlet 参数,仅在启用提供程序的驱动器中使用 cmdlet 时才可用。 这些参数在证书提供程序的所有子目录中都有效,但仅对证书有效。
注释
对 EnhancedKeyUsageList 属性执行筛选的参数也会返回属性值为空 EnhancedKeyUsageList 的项目。
具有空 EnhancedKeyUsageList 的证书可用于所有目的。
项目类型 <字符串>
此参数允许您指定由 New-Item创建的项目的类型。
在 Certificate 驱动器中,允许以下值:
- 证书提供程序
- 证书
- Microsoft Store
- 店铺位置
支持的 Cmdlet
CodeSigningCert <System.Management.Automation.SwitchParameter>
支持的 cmdlet
此参数获取 EnhancedKeyUsageList 属性值中包含“Code Signing”的证书。
DnsName <Microsoft.PowerShell.Commands.DnsNameRepresentation>
支持的 cmdlet
此参数获取证书 DNSNameList 属性中具有指定域名或名称模式的证书。 此参数的值可以是 “Unicode” 或 “ASCII”。 Punycode 值将转换为 Unicode。 允许通配符 \ 。
此参数是在 Windows PowerShell 3.0 中引入的。
EKU <System.String>
支持的 cmdlet
此参数获取在证书属性中 EnhancedKeyUsageList 具有指定文本或文本模式的证书。 允许通配符 \ 。 该 EnhancedKeyUsageList 属性包含 EKU 的友好名称和 OID 字段。
此参数是在 Windows PowerShell 3.0 中引入的。
ExpiringInDays <System.Int32>
支持的 cmdlet
此参数获取在指定天数内或之前过期的证书。 值为 0(零)时,将获取已过期的证书。
此参数是在 Windows PowerShell 3.0 中引入的。
SSLServerAuthentication <System.Management.Automation.SwitchParameter>
支持的 cmdlet
仅获取 SSL Web 托管的服务器证书。 此参数获取其 EnhancedKeyUsageList 属性值中包含“Server Authentication”的证书。
此参数是在 Windows PowerShell 3.0 中引入的。
DeleteKey <System.Management.Automation.SwitchParameter>
支持的 cmdlet
此参数在删除证书时删除关联的私钥。
重要
要删除与远程计算机上存储中的 Cert:\CurrentUser 用户证书关联的私有密钥,您必须使用委托的凭证。
Invoke-Command cmdlet 使用 CredSSP 参数支持凭据委派。 在将 Remove-Item 与 Invoke-Command 和凭据委派一起使用之前,应考虑任何安全风险。
此参数是在 Windows PowerShell 3.0 中引入的。
脚本属性
新的脚本属性已添加到 x509Certificate2 对象,该对象表示证书,以便轻松搜索和管理证书。
DnsNameList:为了填充DnsNameList属性,证书提供程序从 SubjectAlternativeName (SAN) 扩展中的 DNSName 条目中复制内容。 如果 SAN 扩展为空,则使用证书的“使用者”字段中的内容填充该属性。EnhancedKeyUsageList:为了填充EnhancedKeyUsageList属性,证书提供程序会复制证书中 EnhancedKeyUsage (EKU) 字段的 OID 属性,并为其创建一个友好名称。SendAsTrustedIssuer:为了填充SendAsTrustedIssuer属性,证书提供程序会从证书中复制SendAsTrustedIssuer属性。 有关详细信息,请参阅 管理受信任的颁发者进行客户端身份验证。
借助这些新功能,可以根据证书的 DNS 名称和到期日期搜索证书,并根据增强型密钥使用情况(EKU)属性的值来区分客户端和服务器身份验证证书。
使用管道
提供程序 cmdlet 接受管道输入。 可以使用管道将提供程序数据从一个 cmdlet 发送到另一个提供程序 cmdlet 来简化任务。 若要详细了解如何将管道与提供程序 cmdlet 配合使用,请参阅本文中提供的 cmdlet 参考。
获取帮助
从 Windows PowerShell 3.0 开始,您可以获取针对提供程序 cmdlet 的自定义帮助主题,这些帮助主题解释了这些 cmdlet 在文件系统驱动器中的运作方式。
若要获取为文件系统驱动器自定义的帮助主题,请在文件系统驱动器中运行 Get-Help 命令,或使用 -Path 的 参数来指定文件系统驱动器。
Get-Help Get-ChildItem
Get-Help Get-ChildItem -Path cert: