How do I: Delete old Certificates windows servers using Power Shell (Azure Classic Release Pipelines)
Tried this code and I get:
2024-05-07T02:43:20.4279860Z ##[error]Atleast one remote job failed. Consult logs
for more details. ErrorCodes(s):
'RemoteDeployer_NonZeroExitCode***RemoteDeployer_NonZeroExitCode***RemoteDeployer_NonZeroEx
itCode***RemoteDeployer_NonZeroExitCode'
param(
$servers = "$(deploy-Hostesses)"
)
#
foreach ($server in $servers) {
Write-Host "Processing server: $server"
Invoke-Command -ComputerName $server -ScriptBlock {
# Retrieve all certificates from the certificate store
$certs = Get-ChildItem -Path Cert:LocalMachineMy
# Define the date threshold (current date minus expiration days)
$thresholdDate = (Get-Date).AddDays(-120)
foreach ($cert in $certs) {
# Check if the certificate is expired
if ($cert.NotAfter -lt $thresholdDate) {
Write-Host "Certificate $($cert.Thumbprint) is expired. Deleting..."
# Delete the expired certificate
Remove-Item -Path "Cert:LocalMachineMy$($cert.Thumbprint)" -Force
Write-Host "Certificate $($cert.Thumbprint) deleted."
}
}
}
}
Ideas on what I am missing? Windows server 2019 Psremoteing enabled