Fix Windows task and certificate collector reliability
This commit is contained in:
@@ -4,12 +4,23 @@ function Get-SamsaraCertificates {
|
||||
Invoke-SamsaraRead $Result 'Local machine certificate store list' {
|
||||
$collection.stores = @(Get-ChildItem -LiteralPath 'Cert:\LocalMachine' -ErrorAction Stop)
|
||||
}
|
||||
# Listing store names is not evidence that any listed store was readable.
|
||||
if ($collection.stores.Count -gt 0) { $Result.successful_subchecks-- }
|
||||
foreach ($store in $collection.stores) {
|
||||
Invoke-SamsaraRead $Result 'Local machine public certificate metadata' {
|
||||
Get-ChildItem -LiteralPath $store.PSPath -ErrorAction Stop | ForEach-Object {
|
||||
$Result.data += [pscustomobject]@{ subject = [string]$_.Subject; issuer = [string]$_.Issuer
|
||||
expires = $_.NotAfter.ToUniversalTime().ToString('o'); thumbprint = [string]$_.Thumbprint
|
||||
store = [string]$store.PSChildName }
|
||||
# Certificate provider PSPath may omit LocalMachine (including on 5.1).
|
||||
# Construct an explicit provider path from the enumerated store name.
|
||||
$storeName = [string]$store.PSChildName
|
||||
Invoke-SamsaraRead $Result 'Certificate store query' {
|
||||
if ($storeName -notmatch '^[A-Za-z0-9_. -]+$') { throw 'Invalid store name' }
|
||||
$certificates = @(Get-ChildItem -LiteralPath ('Cert:\LocalMachine\' + $storeName) -ErrorAction Stop)
|
||||
foreach ($certificate in $certificates) {
|
||||
Invoke-SamsaraRead $Result 'Certificate metadata projection' {
|
||||
$row = [pscustomobject]@{ subject = [string]$certificate.Subject; issuer = [string]$certificate.Issuer
|
||||
expires = $certificate.NotAfter.ToUniversalTime().ToString('o'); thumbprint = [string]$certificate.Thumbprint
|
||||
store = $storeName }
|
||||
$null = ConvertTo-Json -InputObject $row -Depth 4 -ErrorAction Stop
|
||||
$Result.data += $row
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user