39、Windows PowerShell 证书管理脚本使用指南

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 中的证书,及时发现过期和即将过期的证书,确保系统的正常运行。同时,在导入证书前进行检查,能有效避免使用错误的证书。

内容概要:本文介绍了ENVI Deep Learning V1.0的操作教程,重点讲解了如何利用ENVI软件进行深度学习模型的训练与应用,以实现遥感图像中特定目标(如集装箱)的自动提取。教程涵盖了从数据准备、标签图像创建、模型初始化与训练,到执行分类及结果优化的完整流程,并介绍了精度评价与通过ENVI Modeler实现一键化建模的方法。系统基于TensorFlow框架,采用ENVINet5(U-Net变体)架构,支持通过点、线、面ROI或分类图生成标签数据,适用于多/高光谱影像的单一类别特征提取。; 适合人群:具备遥感图像处理基础,熟悉ENVI软件操作,从事地理信息、测绘、环境监测等相关领域的技术人员或研究人员,尤其是希望将深度学习技术应用于遥感目标识别的初学者与实践者。; 使用场景及目标:①在遥感影像中自动识别和提取特定地物目标(如车辆、建筑、道路、集装箱等);②掌握ENVI环境下深度学习模型的训练流程与关键参数设置(如Patch Size、Epochs、Class Weight等);③通过模型调优与结果反馈提升分类精度,实现高效自动化信息提取。; 阅读建议:建议结合实际遥感项目边学边练,重点关注标签数据制作、模型参数配置与结果后处理环节,充分利用ENVI Modeler进行自动化建模与参数优化,同时注意软硬件环境(特别是NVIDIA GPU)的配置要求以保障训练效率。
内容概要:本文系统阐述了企业新闻发稿在生成式引擎优化(GEO)时代下的全渠道策略与效果评估体系,涵盖当前企业传播面临的预算、资源、内容与效果评估四大挑战,并深入分析2025年新闻发稿行业五大趋势,包括AI驱动的智能化转型、精准化传播、首发内容价值提升、内容资产化及数据可视化。文章重点解析央媒、地方官媒、综合门户和自媒体四类媒体资源的特性、传播优势与发稿策略,提出基于内容适配性、时间节奏、话题设计的策略制定方法,并构建涵盖品牌价值、销售转化与GEO优化的多维评估框架。此外,结合“传声港”工具实操指南,提供AI智能投放、效果监测、自媒体管理与舆情应对的全流程解决方案,并针对科技、消费、B2B、区域品牌四大行业推出定制化发稿方案。; 适合人群:企业市场/公关负责人、品牌传播管理者、数字营销从业者及中小企业决策者,具备一定媒体传播经验并希望提升发稿效率与ROI的专业人士。; 使用场景及目标:①制定科学的新闻发稿策略,实现从“流量思维”向“价值思维”转型;②构建央媒定调、门户扩散、自媒体互动的立体化传播矩阵;③利用AI工具实现精准投放与GEO优化,提升品牌在AI搜索中的权威性与可见性;④通过数据驱动评估体系量化品牌影响力与销售转化效果。; 阅读建议:建议结合文中提供的实操清单、案例分析与工具指南进行系统学习,重点关注媒体适配性策略与GEO评估指标,在实际发稿中分阶段试点“AI+全渠道”组合策略,并定期复盘优化,以实现品牌传播的长期复利效应。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值