Skip to content

Commit

Permalink
[sslcertificates] De-duplicate code by using a loop
Browse files Browse the repository at this point in the history
  • Loading branch information
mueller-ma committed Sep 30, 2024
1 parent a0a5f71 commit 8bf5724
Showing 1 changed file with 14 additions and 27 deletions.
41 changes: 14 additions & 27 deletions sslcertificates/agents/windows/plugins/sslcertificates.ps1
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@

Write-Host '<<<sslcertificates:sep(0)>>>'

$UnixEpoch = (Get-Date -Date "01/01/1970") ;

foreach ($_ in Get-ChildItem -Recurse Cert:\LocalMachine\My) {
If ($_.DnsNameList) {$subject = $_.DnsNameList}
ElseIf ($_.Subject) {$subject = $_.Subject}
Else {$subject = $_.Thumbprint}
$CertLocations = "Cert:\LocalMachine\My", "Cert:\CurrentUser\My"

$data = [ordered]@{
starts = (New-TimeSpan -Start $UnixEpoch -End $_.NotBefore).TotalSeconds ;
expires = (New-TimeSpan -Start $UnixEpoch -End $_.NotAfter).TotalSeconds ;
subj = $subject.Unicode ;
thumb = $_.Thumbprint ;
algosign = $_.SignatureAlgorithm.FriendlyName ;
}
foreach ($CertLocation in $CertLocations) {
foreach ($_ in Get-ChildItem -Recurse $CertLocation) {
If ($_.DnsNameList) {$subject = $_.DnsNameList}
ElseIf ($_.Subject) {$subject = $_.Subject}
Else {$subject = $_.Thumbprint}

$data | ConvertTo-Json -Compress
}
$data = [ordered]@{
starts = (New-TimeSpan -Start $UnixEpoch -End $_.NotBefore).TotalSeconds ;
expires = (New-TimeSpan -Start $UnixEpoch -End $_.NotAfter).TotalSeconds ;
subj = $subject.Unicode ;
thumb = $_.Thumbprint ;
algosign = $_.SignatureAlgorithm.FriendlyName ;
}

foreach ($_ in Get-ChildItem -Recurse Cert:\CurrentUser\My) {
If ($_.DnsNameList) {$subject = $_.DnsNameList}
ElseIf ($_.Subject) {$subject = $_.Subject}
Else {$subject = $_.Thumbprint}

$data = [ordered]@{
starts = (New-TimeSpan -Start $UnixEpoch -End $_.NotBefore).TotalSeconds ;
expires = (New-TimeSpan -Start $UnixEpoch -End $_.NotAfter).TotalSeconds ;
subj = $subject.Unicode ;
thumb = $_.Thumbprint ;
algosign = $_.SignatureAlgorithm.FriendlyName ;
$data | ConvertTo-Json -Compress
}

$data | ConvertTo-Json -Compress
}

0 comments on commit 8bf5724

Please sign in to comment.