[CmdletBinding(SupportsShouldProcess = $true)] param( [Parameter(Mandatory = $true)] [string]$Destination, [string[]]$Store = @('Cert:\CurrentUser\My', 'Cert:\LocalMachine\My'), [string]$PemFolder ) $ErrorActionPreference = 'Stop' $destFull = [System.IO.Path]::GetFullPath($Destination) $stamp = Get-Date -Format 'yyyyMMdd-HHmmss' $targetRoot = Join-Path $destFull ('certificates-' + $stamp) $trimChars = [char[]]@('\', '/') $pemFull = '' if (-not [string]::IsNullOrWhiteSpace($PemFolder)) { $pemFull = [System.IO.Path]::GetFullPath($PemFolder) if (-not (Test-Path -LiteralPath $pemFull -PathType Container)) { throw "PEM folder not found: $pemFull" } if ($destFull.TrimEnd($trimChars) -eq $pemFull.TrimEnd($trimChars)) { throw 'Destination must differ from PemFolder.' } if ($destFull.StartsWith($pemFull.TrimEnd($trimChars) + [System.IO.Path]::DirectorySeparatorChar, [System.StringComparison]::OrdinalIgnoreCase)) { throw 'Destination must not be inside PemFolder.' } } if (Test-Path -LiteralPath $targetRoot) { throw "Destination already exists: $targetRoot" } if (-not $PSCmdlet.ShouldProcess($targetRoot, 'Export certificates')) { return } New-Item -ItemType Directory -Path $targetRoot -Force | Out-Null $manifest = New-Object System.Collections.Generic.List[object] foreach ($storePath in $Store) { try { $certs = @(Get-ChildItem -Path $storePath -ErrorAction Stop) } catch { Write-Warning "Store skipped $storePath : $($_.Exception.Message)" continue } foreach ($cert in $certs) { $label = ($cert.Subject -replace '[^A-Za-z0-9._-]', '_') if ($label.Length -gt 60) { $label = $label.Substring(0, 60) } $thumb = $cert.Thumbprint.Substring(0, 8) $file = Join-Path $targetRoot ($label + '-' + $thumb + '.cer') $index = 1 while (Test-Path -LiteralPath $file) { $file = Join-Path $targetRoot ($label + '-' + $thumb + '-' + $index + '.cer') $index++ } Export-Certificate -Cert $cert -FilePath $file -Type CERT -ErrorAction Stop | Out-Null $manifest.Add([pscustomobject]@{ Store = $storePath Subject = $cert.Subject Thumbprint = $cert.Thumbprint NotAfter = $cert.NotAfter File = $file SHA256 = (Get-FileHash -LiteralPath $file -Algorithm SHA256).Hash }) } } if ($pemFull) { $pemTarget = Join-Path $targetRoot 'pem' New-Item -ItemType Directory -Path $pemTarget -Force | Out-Null $pemFiles = @(Get-ChildItem -LiteralPath $pemFull -File -ErrorAction SilentlyContinue | Where-Object { $_.Extension.ToLowerInvariant() -in @('.pem', '.crt', '.cer') }) foreach ($file in $pemFiles) { $target = Join-Path $pemTarget $file.Name Copy-Item -LiteralPath $file.FullName -Destination $target -Force $manifest.Add([pscustomobject]@{ Store = 'PEM' Subject = $file.Name Thumbprint = '' NotAfter = '' File = $target SHA256 = (Get-FileHash -LiteralPath $target -Algorithm SHA256).Hash }) } } $manifestPath = Join-Path $targetRoot 'manifest.csv' if ($manifest.Count -gt 0) { $manifest | Export-Csv -LiteralPath $manifestPath -NoTypeInformation -Encoding UTF8 } else { Set-Content -LiteralPath $manifestPath -Value 'Store,Subject,Thumbprint,NotAfter,File,SHA256' -Encoding UTF8 } Write-Host "Certificates: $($manifest.Count)" Write-Host "Folder: $targetRoot"