Files
cert/install-root-ca.ps1
2026-10-01 16:13:02 +03:00

52 lines
2.3 KiB
PowerShell

# install-root-ca.ps1
$IsAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $IsAdmin) {
Write-Host "ERROR: SCRIPT MUST BE RUN AS ADMINISTRATOR!" -ForegroundColor Red
Write-Host "Please restart PowerShell as Administrator." -ForegroundColor Yellow
Exit
}
$CertFile = "root_ca.crt"
if (-not (Test-Path $CertFile)) {
Write-Host "Error: File $CertFile not found in current directory!" -ForegroundColor Red
Exit
}
try {
# Получаем полный абсолютный путь к файлу сертификата
$FullCertPath = (Get-Item $CertFile).FullName
# Читаем сертификат по полному пути
$Cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($FullCertPath)
$Thumbprint = $Cert.Thumbprint
$Subject = $Cert.Subject
} catch {
Write-Host "Error: Could not read certificate file $CertFile." -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Yellow
Exit
}
Write-Host "Checking root certificate..." -ForegroundColor Cyan
Write-Host "Certificate Name: $Subject" -ForegroundColor Gray
Write-Host "SHA1 Thumbprint: $Thumbprint" -ForegroundColor Gray
$StorePath = "Cert:\LocalMachine\Root"
$TargetCert = Get-ChildItem -Path $StorePath | Where-Object { $_.Thumbprint -eq $Thumbprint }
if ($TargetCert) {
Write-Host "[ OK ] Certificate is ALREADY installed in Windows Trusted Root Storage." -ForegroundColor Green
} else {
Write-Host "[ INFO ] Certificate not found. Installing..." -ForegroundColor Yellow
try {
$Store = New-Object System.Security.Cryptography.X509Certificates.X509Store("Root", "LocalMachine")
$Store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$Store.Add($Cert)
$Store.Close()
Write-Host "[ SUCCESS ] Certificate successfully added to Trusted Roots!" -ForegroundColor Green
} catch {
Write-Host "[ ERROR ] Failed to install certificate." -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
}
}