Загрузка данных


<#
.SYNOPSIS
    Поиск сертификатов, развернутых через групповые политики (GPO).
.DESCRIPTION
    Скрипт обращается к физическим хранилищам .GroupPolicy в реестре
    для LocalMachine и CurrentUser и выводит список сертификатов.
#>

# --- Определяем пути к физическим хранилищам GPO ---
$gpoStorePaths = @(
    # Компьютерные сертификаты, полученные через GPO
    @{ Path = "HKLM:\SOFTWARE\Microsoft\SystemCertificates\Root\Certificates"; Scope = "LocalMachine\Root (GPO)" },
    @{ Path = "HKLM:\SOFTWARE\Microsoft\SystemCertificates\CA\Certificates"; Scope = "LocalMachine\CA (GPO)" },
    @{ Path = "HKLM:\SOFTWARE\Microsoft\SystemCertificates\Trust\Certificates"; Scope = "LocalMachine\Trust (GPO)" },
    # Пользовательские сертификаты, полученные через GPO
    @{ Path = "HKCU:\SOFTWARE\Microsoft\SystemCertificates\Root\Certificates"; Scope = "CurrentUser\Root (GPO)" },
    @{ Path = "HKCU:\SOFTWARE\Microsoft\SystemCertificates\CA\Certificates"; Scope = "CurrentUser\CA (GPO)" },
    @{ Path = "HKCU:\SOFTWARE\Microsoft\SystemCertificates\Trust\Certificates"; Scope = "CurrentUser\Trust (GPO)" }
)

$gpoCerts = foreach ($store in $gpoStorePaths) {
    if (Test-Path $store.Path) {
        Get-ChildItem -Path $store.Path -ErrorAction SilentlyContinue | ForEach-Object {
            # Имя ключа реестра — это отпечаток сертификата (Thumbprint)
            $thumbprint = $_.PSChildName
            $cert = Get-ChildItem -Path "Cert:\$($store.Scope.Split(' ')[0])\$($store.Scope.Split('\')[1])" -ErrorAction SilentlyContinue |
                    Where-Object { $_.Thumbprint -eq $thumbprint }
            
            if ($cert) {
                [PSCustomObject]@{
                    GPOScope      = $store.Scope
                    Subject       = $cert.Subject
                    Thumbprint    = $thumbprint
                    KeySize       = if ($cert.PublicKey.Oid.Value -eq "1.2.840.113549.1.1.1") { $cert.PublicKey.Key.KeySize } else { "N/A (not RSA)" }
                    NotAfter      = $cert.NotAfter
                    HasPrivateKey = $cert.HasPrivateKey
                }
            }
        }
    }
}

# --- Вывод результатов ---
if ($gpoCerts) {
    Write-Host "`nНайдены сертификаты, развернутые через групповые политики:" -ForegroundColor Yellow
    $gpoCerts | Format-Table -AutoSize -Wrap
    $gpoCerts | Export-Csv -Path ".\GPO_Certificates_$(Get-Date -Format 'yyyyMMdd_HHmmss').csv" -NoTypeInformation -Encoding UTF8
    Write-Host "`nРезультаты сохранены в CSV-файл в текущей директории." -ForegroundColor Green
} else {
    Write-Host "`nСертификаты, развернутые через групповые политики, не найдены." -ForegroundColor Green
}