| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Imports a PFX/PEM certificate file and returns X509Certificate2. |
| | 4 | | .DESCRIPTION |
| | 5 | | The Import-KrCertificate function allows you to import a certificate into the Kestrun environment. |
| | 6 | | This may include loading a certificate from a file or other source and adding it to the appropriate certificate |
| | 7 | | .PARAMETER FilePath |
| | 8 | | The path to the certificate file to import. |
| | 9 | | .PARAMETER Password |
| | 10 | | The password for the certificate file, if applicable. |
| | 11 | | .PARAMETER PrivateKeyPath |
| | 12 | | The path to the private key file, if applicable. |
| | 13 | | .EXAMPLE |
| | 14 | | Import-KrCertificate -Path "C:\certs\mycert.pfx" -Password (ConvertTo-SecureString "password" -AsPlainText -Forc |
| | 15 | | This example imports a certificate from the specified path using the provided password. |
| | 16 | | .NOTES |
| | 17 | | This function is part of the Kestrun PowerShell module. |
| | 18 | | #> |
| | 19 | | function Import-KrCertificate { |
| | 20 | | [KestrunRuntimeApi('Everywhere')] |
| | 21 | | [CmdletBinding()] |
| | 22 | | [OutputType([System.Security.Cryptography.X509Certificates.X509Certificate2])] |
| | 23 | | param( |
| | 24 | | [Parameter(Mandatory)][string] $FilePath, |
| | 25 | | [securestring] $Password, |
| | 26 | | [string] $PrivateKeyPath |
| | 27 | | ) |
| 0 | 28 | | $resolvedPath = Resolve-KrPath -Path $FilePath -KestrunRoot -Test |
| 0 | 29 | | Write-KrLog -Level Verbose -Message "Resolved file path: $resolvedPath" |
| 0 | 30 | | if ( -not (Test-Path -Path $resolvedPath -PathType Leaf)) { |
| 0 | 31 | | throw "Certificate file not found at path: $resolvedPath" |
| | 32 | | } |
| 0 | 33 | | if ($null -eq $Password) { |
| 0 | 34 | | return [Kestrun.Certificates.CertificateManager]::Import($resolvedPath, $PrivateKeyPath) |
| | 35 | | } |
| 0 | 36 | | return [Kestrun.Certificates.CertificateManager]::Import($resolvedPath, $Password, $PrivateKeyPath) |
| | 37 | | } |
| | 38 | |
|