Windows PowerShell 证书管理脚本使用指南
1. 证书存储操作基础
在处理证书存储时,可通过以下代码查看指定存储中的证书信息:
$objstore.Open("Readonly")
$colcerts = $objstore.Certificates
Write-Host -ForegroundColor blue
"
There are $($colcerts.count) certificates in the $store store.
They are listed below:
"
foreach($cert in $colCerts)
{
"FriendlyName:
$($cert.FriendlyName)"
"Serialnumber: $($cert.SerialNumber)"
"Thumbprint: $($cert.thumbprint)"
"Subject: $($cert.subject)`n"
}
$objstore.Close()
该代码首先以只读模式打开证书存储,获取存储中的所有证书,然后输出证书数量和每个证书的友好名称、序列号、指纹和主题信息,最后关闭存储。
2. 定位过期证书
随着证书的广泛使用,过期证书的问题也日益突出。为了快速有效地定位过期证书,可使用
FindExpiredCertificates.ps1
脚本。
2.1 脚本参数
该脚本使用
param
语句定义了四个命令行参数:
-
-store
:指定要搜索的证书存储,若未提供则默认使用
CurrentUser\My
存储。
-
-listcu
:列出
CurrentUser
位置的所有证书存储。
-
-listlm
:列出
LocalMachine
位置的所有证书存储。
-
-help
:打印帮助文本。
param(
$store,
[switch]$listcu,
[switch]$listlm,
[switch]$help
)
2.2 帮助函数
funHelp()
函数用于打印帮助文本,使用了 here-string 来创建帮助信息。
function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: FindExpiredCertificates.ps1
Finds expired certificates on the local machine
PARAMETERS:
-store
the certificate store on the computer
-help
prints help file
SYNTAX:
FindExpiredCertificates.ps1
Gets a listing of expired certificates in the my store of the
currentuser
FindExpiredCertificates.ps1 -store "currentuser\my"
Gets a listing of expired certificates in the my store of the
currentuser
FindExpiredCertificates.ps1 -store "currentuser\smartcardroot"
Gets a listing of expired certificates in the smartcardtoot store
of the currentuser
FindExpiredCertificates.ps1 -listcu
Gets a listing of certificate stores for the
currentuser
FindExpiredCertificates.ps1 -listlm
Gets a listing of certificate stores for the
localmachine
FindExpiredCertificates.ps1 -help
Prints the help topic for the script
"@
$helpText
exit
}
2.3 参数解析
脚本会依次检查命令行参数:
- 若
-help
开关存在,则打印提示信息并调用
funHelp()
函数。
if($help)
{ "Printing help now..." ; funHelp }
-
若
-listcu开关存在,则打印状态信息并列出CurrentUser位置的证书存储,然后退出脚本。
if($listcu) {
"Certificate stores in currentuser"
get-childitem cert:\currentuser ; exit
}
-
若
-listlm开关存在,则打印状态信息并列出LocalMachine位置的证书存储,然后退出脚本。
if($listlm) {
"Certificate stores in localmachine"
get-childitem cert:\localmachine ; exit
}
-
若
-store开关未使用,则默认使用CurrentUser\My存储,并提示用户可使用-help查看更多示例。
if(!$store) {
$store = "currentuser\my"
"Using default store: $store"
"See $($myinvocation.mycommand) -help" `
+ " for additional examples"
}
2.4 查找过期证书
获取当前日期,检索指定存储中的所有证书,遍历证书集合,找出过期证书并打印其指纹和过期日期。
$currentDate = Get-Date
$colcert = Get-ChildItem cert:\$store
Write-host -foregroundcolor cyan "Expired Certificates in $store"
foreach($cert in $colcert)
{
if($cert.notafter -lt $currentDate)
{
Write-host `
"
$($cert.thumbprint) `t $($cert.Notafter)
"
}
}
3. 识别即将过期的证书
许多用户证书的有效期通常只有一到两年,为了提前发现即将过期的证书,可使用
FindCertificatesAboutToExpire.ps1
脚本。
3.1 脚本参数
该脚本使用
param
语句定义了五个参数:
-
-store
:指定要搜索的证书存储,为必需参数。
-
-days
:指定未来的天数,用于评估证书是否即将过期,默认值为 30 天。
-
-listcu
:列出
CurrentUser
位置的所有证书存储。
-
-listlm
:列出
LocalMachine
位置的所有证书存储。
-
-help
:打印帮助文本。
param(
$store,
$days=30,
[switch]$listcu,
[switch]$listlm,
[switch]$help
)
3.2 帮助函数
funHelp()
函数用于打印帮助文本,包含脚本的描述、参数和语法示例。
function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: FindCertificatesAboutToExpire.ps1
Finds certificates about to expire with in a certain
number of days on the local machine
PARAMETERS:
-store
the certificate store on the computer
-days
number of days in the future to evaluate for
certificate expiration
-help
prints help file
SYNTAX:
FindCertificatesAboutToExpire.ps1
Gets a listing of certificates about to expire within 30 days
in the my store of the currentuser
FindCertificatesAboutToExpire.ps1 -days 45
Gets a listing of certificates about to expire within 45 days
in the my store of the currentuser
FindCertificatesAboutToExpire.ps1 -store "currentuser\my" -days 60
Gets a listing of certificates about to expire within 60 days
in the my store of the currentuser
FindCertificatesAboutToExpire.ps1 -store "currentuser\smartcardroot"
Gets a listing of certificates about to expire within 30 days
in the smartcardroot store of the currentuser
FindCertificatesAboutToExpire.ps1 -listcu
Gets a listing of certificate stores for the
currentuser
FindCertificatesAboutToExpire.ps1 -listlm
Gets a listing of certificate stores for the
localmachine
FindCertificatesAboutToExpire.ps1 -help
Prints the help topic for the script
"@
$helpText
exit
}
3.3 参数解析
脚本会依次检查命令行参数,处理方式与
FindExpiredCertificates.ps1
脚本类似。
3.4 查找即将过期的证书
计算未来指定天数的日期,检索指定存储中的所有证书,遍历证书集合,找出即将过期的证书并打印其指纹和过期日期。
$currentDate = (Get-Date).adddays($days)
$colcert = Get-ChildItem cert:\$store
Write-host -foregroundcolor cyan "Certificates in $store that" `
" expire in $days days"
foreach($cert in $colcert)
{
if($cert.notafter -lt $currentDate)
{
Write-host `
"
$($cert.thumbprint) `t $($cert.Notafter)
"
}
}
4. 证书管理操作流程
| 操作 | 脚本 | 参数 | 功能 |
|---|---|---|---|
| 定位过期证书 | FindExpiredCertificates.ps1 | -store, -listcu, -listlm, -help | 查找指定存储中的过期证书 |
| 识别即将过期的证书 | FindCertificatesAboutToExpire.ps1 | -store, -days, -listcu, -listlm, -help | 查找指定存储中在未来指定天数内即将过期的证书 |
5. 流程图
graph TD;
A[开始] --> B{是否使用 -help 参数};
B -- 是 --> C[打印帮助信息并退出];
B -- 否 --> D{是否使用 -listcu 参数};
D -- 是 --> E[列出 CurrentUser 证书存储并退出];
D -- 否 --> F{是否使用 -listlm 参数};
F -- 是 --> G[列出 LocalMachine 证书存储并退出];
F -- 否 --> H{是否指定 -store 参数};
H -- 否 --> I[使用默认存储];
H -- 是 --> J[使用指定存储];
I --> K[查找证书];
J --> K;
K --> L{是否过期或即将过期};
L -- 是 --> M[打印证书信息];
L -- 否 --> N[继续检查下一个证书];
M --> N;
N --> O{是否还有证书};
O -- 是 --> K;
O -- 否 --> P[结束];
通过以上脚本和操作流程,你可以方便地管理 Windows PowerShell 中的证书,及时发现过期和即将过期的证书,确保系统的正常运行。
Windows PowerShell 证书管理脚本使用指南
6. 检查证书
在导入证书之前,你可能需要检查它,以确保它是当前操作所需的正确证书。可以使用
InspectCertificate.ps1
脚本来检查证书。
6.1 脚本参数
该脚本使用
param
语句定义了两个参数:
-
-cert
:指定要检查的证书的完整路径和名称。
-
-help
:一个开关参数,用于在需要时显示帮助信息。
param($cert, [switch]$help)
6.2 帮助函数
funHelp()
函数用于在脚本使用
-help
参数启动时显示帮助信息。
function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: InspectCertificate.ps1
Finds certificates of a particular use on the local machine
PARAMETERS:
-cert
the full path to the certificate to inspect
-help
prints help file
SYNTAX:
InspectCertificate.ps1
Generates an error that a certificate is required
InspectCertificate.ps1 -cert "c:\fso\filerecovery.cer"
Inspects a certificate called filerecovery in the c:\fso
directory. This certificate could be DER encoded or base -64
encoded .cer file.
InspectCertificate.ps1 -help
Prints the help topic for the script
"@
$helpText
exit
}
6.3 参数检查
检查命令行参数:
- 若
-help
开关存在,则打印提示信息并调用
funHelp()
函数。
if($help) { "Printing help now..." ; funHelp }
-
若
-cert参数未提供,则提示需要证书并调用funHelp()函数。
if(!$cert) { "A certificate is required..." ; funHelp }
6.4 连接并检查证书
使用
.NET Framework
的
X509Certificate
类连接到证书,并打印证书的相关信息。
$objCert=[security.cryptography.x509certificates.x509certificate]"$cert"
"HashString: $($objCert.GetCertHashString())"
"EffectiveDate: $($objCert.GetEffectiveDateString())"
"ExpirationDate: $($objCert.GetExpirationDateString())"
"HashCode: $($objCert.GetHashCode())"
"KeyAlgorithm: $($objCert.GetKeyAlgorithm())"
"KeyAlgorithmParameters: $($objCert.GetKeyAlgorithmParametersString())"
"Name: $($objCert.GetName())`n"
"PublicKey: $($objCert.GetPublicKeyString())`n"
"RawCertData: $($objCert.GetRawCertDataString())`n"
"SerialNumber: $($objCert.GetSerialNumberString())"
"Cert: $($objCert.ToString())"
"Issuer: $($objCert.Issuer)"
"Subject: $($objCert.Subject)"
7. 操作步骤总结
以下是使用上述脚本进行证书管理的操作步骤总结:
| 操作 | 步骤 |
|---|---|
| 定位过期证书 |
1. 运行
FindExpiredCertificates.ps1
脚本,可根据需要使用
-store
、
-listcu
、
-listlm
、
-help
参数。
2. 若未指定
-store
,默认使用
CurrentUser\My
存储。
3. 脚本将输出指定存储中的过期证书信息。 |
| 识别即将过期的证书 |
1. 运行
FindCertificatesAboutToExpire.ps1
脚本,可根据需要使用
-store
、
-days
、
-listcu
、
-listlm
、
-help
参数。
2. 若未指定
-store
,默认使用
CurrentUser\My
存储;若未指定
-days
,默认值为 30 天。
3. 脚本将输出指定存储中在未来指定天数内即将过期的证书信息。 |
| 检查证书 |
1. 运行
InspectCertificate.ps1
脚本,使用
-cert
参数指定要检查的证书的完整路径和名称,或使用
-help
参数获取帮助。
2. 脚本将输出证书的详细信息。 |
8. 代码示例总结
下面是三个脚本的完整代码示例:
8.1 FindExpiredCertificates.ps1
param(
$store,
[switch]$listcu,
[switch]$listlm,
[switch]$help
)
function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: FindExpiredCertificates.ps1
Finds expired certificates on the local machine
PARAMETERS:
-store
the certificate store on the computer
-help
prints help file
SYNTAX:
FindExpiredCertificates.ps1
Gets a listing of expired certificates in the my store of the
currentuser
FindExpiredCertificates.ps1 -store "currentuser\my"
Gets a listing of expired certificates in the my store of the
currentuser
FindExpiredCertificates.ps1 -store "currentuser\smartcardroot"
Gets a listing of expired certificates in the smartcardtoot store
of the currentuser
FindExpiredCertificates.ps1 -listcu
Gets a listing of certificate stores for the
currentuser
FindExpiredCertificates.ps1 -listlm
Gets a listing of certificate stores for the
localmachine
FindExpiredCertificates.ps1 -help
Prints the help topic for the script
"@
$helpText
exit
}
if($help)
{ "Printing help now..." ; funHelp }
if($listcu) {
"Certificate stores in currentuser"
get-childitem cert:\currentuser ; exit
}
if($listlm) {
"Certificate stores in localmachine"
get-childitem cert:\localmachine ; exit
}
if(!$store) {
$store = "currentuser\my"
"Using default store: $store"
"See $($myinvocation.mycommand) -help" `
+ " for additional examples"
}
$currentDate = Get-Date
$colcert = Get-ChildItem cert:\$store
Write-host -foregroundcolor cyan "Expired Certificates in $store"
foreach($cert in $colcert)
{
if($cert.notafter -lt $currentDate)
{
Write-host `
"
$($cert.thumbprint) `t $($cert.Notafter)
"
}
}
8.2 FindCertificatesAboutToExpire.ps1
param(
$store,
$days=30,
[switch]$listcu,
[switch]$listlm,
[switch]$help
)
function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: FindCertificatesAboutToExpire.ps1
Finds certificates about to expire with in a certain
number of days on the local machine
PARAMETERS:
-store
the certificate store on the computer
-days
number of days in the future to evaluate for
certificate expiration
-help
prints help file
SYNTAX:
FindCertificatesAboutToExpire.ps1
Gets a listing of certificates about to expire within 30 days
in the my store of the currentuser
FindCertificatesAboutToExpire.ps1 -days 45
Gets a listing of certificates about to expire within 45 days
in the my store of the currentuser
FindCertificatesAboutToExpire.ps1 -store "currentuser\my" -days 60
Gets a listing of certificates about to expire within 60 days
in the my store of the currentuser
FindCertificatesAboutToExpire.ps1 -store "currentuser\smartcardroot"
Gets a listing of certificates about to expire within 30 days
in the smartcardroot store of the currentuser
FindCertificatesAboutToExpire.ps1 -listcu
Gets a listing of certificate stores for the
currentuser
FindCertificatesAboutToExpire.ps1 -listlm
Gets a listing of certificate stores for the
localmachine
FindCertificatesAboutToExpire.ps1 -help
Prints the help topic for the script
"@
$helpText
exit
}
if($help)
{ "Printing help now..." ; funHelp }
if($listcu) {
"Certificate stores in currentuser"
get-childitem cert:\currentuser ; exit
}
if($listlm) {
"Certificate stores in localmachine"
get-childitem cert:\localmachine ; exit
}
if(!$store) {
$store = "currentuser\my"
"Using default store: $store"
"See $($myinvocation.mycommand) -help" `
+ " for additional examples"
}
$currentDate = (Get-Date).adddays($days)
$colcert = Get-ChildItem cert:\$store
Write-host -foregroundcolor cyan "Certificates in $store that" `
" expire in $days days"
foreach($cert in $colcert)
{
if($cert.notafter -lt $currentDate)
{
Write-host `
"
$($cert.thumbprint) `t $($cert.Notafter)
"
}
}
8.3 InspectCertificate.ps1
param($cert, [switch]$help)
function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: InspectCertificate.ps1
Finds certificates of a particular use on the local machine
PARAMETERS:
-cert
the full path to the certificate to inspect
-help
prints help file
SYNTAX:
InspectCertificate.ps1
Generates an error that a certificate is required
InspectCertificate.ps1 -cert "c:\fso\filerecovery.cer"
Inspects a certificate called filerecovery in the c:\fso
directory. This certificate could be DER encoded or base -64
encoded .cer file.
InspectCertificate.ps1 -help
Prints the help topic for the script
"@
$helpText
exit
}
if($help) { "Printing help now..." ; funHelp }
if(!$cert) { "A certificate is required..." ; funHelp }
$objCert=[security.cryptography.x509certificates.x509certificate]"$cert"
"HashString: $($objCert.GetCertHashString())"
"EffectiveDate: $($objCert.GetEffectiveDateString())"
"ExpirationDate: $($objCert.GetExpirationDateString())"
"HashCode: $($objCert.GetHashCode())"
"KeyAlgorithm: $($objCert.GetKeyAlgorithm())"
"KeyAlgorithmParameters: $($objCert.GetKeyAlgorithmParametersString())"
"Name: $($objCert.GetName())`n"
"PublicKey: $($objCert.GetPublicKeyString())`n"
"RawCertData: $($objCert.GetRawCertDataString())`n"
"SerialNumber: $($objCert.GetSerialNumberString())"
"Cert: $($objCert.ToString())"
"Issuer: $($objCert.Issuer)"
"Subject: $($objCert.Subject)"
9. 流程图补充
graph TD;
A[开始] --> B{选择操作};
B -- 定位过期证书 --> C[运行 FindExpiredCertificates.ps1];
B -- 识别即将过期的证书 --> D[运行 FindCertificatesAboutToExpire.ps1];
B -- 检查证书 --> E[运行 InspectCertificate.ps1];
C --> F{是否使用 -help 参数};
F -- 是 --> G[打印帮助信息并退出];
F -- 否 --> H{是否使用 -listcu 参数};
H -- 是 --> I[列出 CurrentUser 证书存储并退出];
H -- 否 --> J{是否使用 -listlm 参数};
J -- 是 --> K[列出 LocalMachine 证书存储并退出];
J -- 否 --> L{是否指定 -store 参数};
L -- 否 --> M[使用默认存储];
L -- 是 --> N[使用指定存储];
M --> O[查找过期证书];
N --> O;
O --> P{是否过期};
P -- 是 --> Q[打印证书信息];
P -- 否 --> R[继续检查下一个证书];
Q --> R;
R --> S{是否还有证书};
S -- 是 --> O;
S -- 否 --> T[结束];
D --> U{是否使用 -help 参数};
U -- 是 --> G;
U -- 否 --> V{是否使用 -listcu 参数};
V -- 是 --> I;
V -- 否 --> W{是否使用 -listlm 参数};
W -- 是 --> K;
W -- 否 --> X{是否指定 -store 参数};
X -- 否 --> Y[使用默认存储];
X -- 是 --> Z[使用指定存储];
Y --> AA{是否指定 -days 参数};
AA -- 是 --> AB[使用指定天数];
AA -- 否 --> AC[使用默认 30 天];
Z --> AA;
AB --> AD[查找即将过期的证书];
AC --> AD;
AD --> AE{是否即将过期};
AE -- 是 --> Q;
AE -- 否 --> R;
E --> AF{是否使用 -help 参数};
AF -- 是 --> G;
AF -- 否 --> AG{是否指定 -cert 参数};
AG -- 否 --> AH[提示需要证书并打印帮助信息];
AG -- 是 --> AI[检查证书并输出信息];
AI --> T;
AH --> T;
通过上述脚本和操作流程,你可以方便地管理 Windows PowerShell 中的证书,及时发现过期和即将过期的证书,确保系统的正常运行。同时,在导入证书前进行检查,能有效避免使用错误的证书。
超级会员免费看
1073

被折叠的 条评论
为什么被折叠?



