Скрипт Powershell для удаления просроченных сертификатов - PullRequest
0 голосов
/ 30 января 2019

Я хочу создать скрипт powershell, который будет удалять сертификаты с истекшим сроком действия, но я получаю сообщение об ошибке.

Я также изменил свойство notafter для отображения в качестве даты истечения срока действия.


        $today = Get-Date
        dir Cert:\LocalMachine\My\|
        select  thumbprint, subject, @{Name="ExpirationDate";Expression= 
        {$_.NotAfter}}|
        Where-Object ExpirationDate -lt $today|
        Remove-Item

Remove-Item : Cannot find drive. A drive with the name '@{Thumbprint=XXXX; 
Subject=CN=xyz.org, OU=X, O=X, L=X, S=X, 
C=US; NotAfter=X' does not exist.
At C:\Users\Documents\Delete Expired Certs Script.ps1:10 char:2
+  Remove-Item 
+  ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (@{Thumbprint=70...r=:String) [Remove-Item], DriveNotFoun 
   dException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

1 Ответ

0 голосов
/ 02 февраля 2019

Я создал функцию для выполнения этой задачи.

Возможны следующие параметры: -CertificateStore LocalMachine или -CertificateStore CurrentUser

Необязательный параметр -WhatIf будет указывать, какие сертификаты будут удалены.

Необязательный -Verbose параметр будет указывать DN сертификата и дату его истечения.

function Remove-ExpiredCertificates {
    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory)]
        [ValidateSet('LocalMachine','CurrentUser')]
        [string]$CertificateStore
    )
    process{
        $today = Get-Date
        $path = "Cert:\$CertificateStore\My"
        $expiredCertList = Get-ChildItem -Path $path | Where-Object -Property NotAfter -lt $today

        foreach ($certificate in $expiredCertList){
            if ($PSCmdlet.ShouldProcess("certificate $($certificate.Subject) that expired $($certificate.NotAfter)",'Remove')){
                Remove-Item -Path $certificate.PSPath -Force
            }
        }
    }
}

Пример вывода:

PS > Remove-ExpiredCertificates -CertificateStore LocalMachine -WhatIf
What if: Performing the operation "Remove" on target "certificate CN=myoldcert.domain.local that expired 01/31/2018 11:59:00"

PS > Remove-ExpiredCertificates -CertificateStore LocalMachine
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...