【问题标题】:Powershell Script to Remove all Expired Certificates on a Group of Servers用于删除一组服务器上所有过期证书的 Powershell 脚本
【发布时间】:2023-03-24 03:49:02
【问题描述】:

我们正在清理我们的服务器环境,需要找到所有过期的证书并将其删除。环境由Windows 2008 R2、Windows 2012、Windows 2012 R2服务器组成

我尝试了几个不起作用的已发布脚本,它们只是说已完成而没有输出。我尝试了一篇发表在 stackoverflow 上的文章:Powershell Script to remove expired certificates。我也尝试了下面的一些脚本,但没有运气。使用指纹是可以的,但我需要删除所有过期的证书

'''

按指纹删除

获取子项证书:\LocalMachine\My\D20159B7772E33A6A33E436C938C6FE764367396 |删除项目

按主题/序列号/发行人/随便删除

获取子项证书:\LocalMachine\My | Where-Object { $_.Subject -match 'Frode F' } | 除去项目 ''' ''' $today = 获取日期 获取子项证书:\CurrentUser\My | Where-Object NotAfter -lt $today | 删除项目

获取子项证书:\CurrentUser\My | ForEach-Object -begin { $now = get-date } -process { if ($PSItem.NotAfter -lt $now ) { $PSItem } } | 除去项目 '''

我想要实现的是删除列表中所有服务器上的所有过期证书,利用引用文本文件中服务器列表的 foreach 语句,然后删除并删除从昨天日期和更早日期过期的所有证书

你们能想到的任何东西都会对我有很大帮助

非常感谢任何帮助

【问题讨论】:

    标签: powershell certificate ssl-certificate


    【解决方案1】:

    您是说您要删除本地证书或通过 ADCS 颁发的证书?

    你说你尝试过很多样例,但你有没有试过这些:

    Remove Local Windows Certificate Store Expired Certificates

    使用此脚本,您将能够运行、检测和删除所有 受影响的本地计算机上的过期证书。所有证书 根据日期检查商店(用户、服务和计算机) (运行时)检测截至日期的任何过期证书 运行。因此 t

    下载:Remove_local_expired_v2.ps1

    Script to query/delete (expired) certificates from a AD-CS (CA /PKI) database

    此 Cleanup-MSPKI_Cert.ps1 PowerShell 脚本包含 3 个函数 您的 CA(认证机构)AD-CS (ActiveDirectory-CertificationAuthority) 维护。你可以筛选 对于某个模板颁发的证书,如果 过期了!

    下载:Cleanup_MSPKI_Cert_v1.2.ps1

    或使用来自 MS powershellgallery.com 的完整 PKI 模块

    '*pki*','*certificate*' | 
    ForEach {Find-Module -Name $PSItem } | 
    Format-Table -AutoSize
    
    # Results
    
    Version Name                               Repository Description
    ------- ----                               ---------- ----------- 
    1.6     PKITools                           PSGallery  Get certificates and Templates ...
    3.4.1.0 PSPKI                              PSGallery  This module contains public key ...
    0.0.5   CustomPKI                          PSGallery  Extended PKI CmdLets                                                                                 
    3.2.0.0 xCertificate                       PSGallery  This module includes DSC resources ...
    4.5.0.0 CertificateDsc                     PSGallery  This module includes DSC resources ...
    0.0.4   SelfSignedCertificate              PSGallery  WARNING: This module is ...
    1.0     cEprsCertificate                   PSGallery  This module instals certificates, ...
    1.0.0.1 azureVpnP2SSelfSignedCertificate   PSGallery  A PowerShell module to help generate ...
    1.4     CertificateHealth                  PSGallery  Certificate Health Check Module                                                                      
    1.5     CertificatePS                      PSGallery  A module to enhance certificate ...
    0.2.0   ExportBase64Certificate            PSGallery  Export certificates from the local ...
    1.0     ACMEDNS01Certificate               PSGallery  Generate SSL Certificates using ...                                                   
    0.2     Get-ADUserCertificate              PSGallery  simple module to get single or ....
    2.1.0   RDPCertificate                     PSGallery  A module for generating and apply....
    1.0.0.2 PowerShell.X509Certificate.Utility PSGallery  A PowerShell X509Certificate Utili...          
    1.2.5   Get-WebCertificate                 PSGallery  This script makes an HTTPS web ...
    1.0     xCertificatePrivateKeyAccess       PSGallery  This resource helps you manage cer... 
    

    【讨论】:

    • 我已经尝试过您引用的 ps1 脚本,当我尝试时,它立即说完成,但所有过期的证书仍然存在。我在几台服务器上尝试过,确保也以管理员身份运行
    • 你真的拿回任何证书了吗?将 -Verbose 和 -force 添加到删除。您可能需要考虑使用 Trace-Command cmdlet 来获取更多详细信息。在此处查看详细信息:docs.microsoft.com/en-us/powershell/module/…
    • $today = Get-Date $ConfirmPreference = "None" $store = New-Object System.Security.Cryptography.x509Certificates.x509Store("My","LocalMachine") $store.Open(" ReadWrite") $certs = $store.Certificates | Where-Object {$_.NotAfter -lt $today} ForEach ($cert in $certs) { $store.Remove($cert) } $store.Close()
    • 我有 2 个问题,我注意到 $store.Open("readwrite") 应该修改还是 readwrite 允许删除?你能帮我看看我应该在哪里放置 -force 选项吗?
    • NTFS 权限: --- 读取权限:用户拥有文件或文件夹的读取权限,例如完全控制、读取和写入。 --- 写入属性:用户可以更改文件或文件夹的属性,例如只读或隐藏。 (NTFS 定义了这些属性。) --- 删除:用户可以删除文件或文件夹。 (如果用户没有文件或文件夹的删除权限,但如果他们对父文件夹具有删除子文件夹和文件的权限,他们仍然可以删除它。)
    猜你喜欢
    • 2019-06-24
    • 2019-04-24
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 2021-03-20
    • 2020-10-17
    • 2016-09-20
    相关资源
    最近更新 更多