|
| 1 | +function Import-CSCertificate { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + adds a given pfx certificate file to current uerers personal certificate store. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + This function is used to import existing pfx certificate files. The Import-PFXCertificate cmdle from the |
| 8 | + PKI module imports the certficate into a deprecated store. Thus you can't read the private key afterwards or |
| 9 | + using it for decrypting data. |
| 10 | +
|
| 11 | + .PARAMETER Path |
| 12 | + Path to an existing *.pfx certificate file. |
| 13 | +
|
| 14 | + .PARAMETER StoreName |
| 15 | + Additionally you change change the store where you want the certificate into. |
| 16 | +
|
| 17 | + .INPUTS |
| 18 | + [None] |
| 19 | +
|
| 20 | + .OUTPUTS |
| 21 | + [None] |
| 22 | +
|
| 23 | + .EXAMPLE |
| 24 | + Import-CSCertificate -Path (Join-Path -Path $Env:APPDATA -ChildPath '/PSCredentialStore.pfx') |
| 25 | +
|
| 26 | + .NOTES |
| 27 | + File Name : Import-CSCertificate.ps1 |
| 28 | + Author : Marco Blessing - marco.blessing@googlemail.com |
| 29 | + Requires : |
| 30 | +
|
| 31 | + .LINK |
| 32 | + https://github.com/OCram85/PSCredentialStore |
| 33 | + #> |
| 34 | + [CmdletBinding()] |
| 35 | + [OutputType()] |
| 36 | + param( |
| 37 | + [Parameter(Mandatory = $true)] |
| 38 | + [ValidateNotNullOrEmpty()] |
| 39 | + [string]$Path, |
| 40 | + |
| 41 | + [Parameter(Mandatory = $false)] |
| 42 | + [ValidateSet( |
| 43 | + 'AddressBook', |
| 44 | + 'AuthRoot', |
| 45 | + 'CertificateAuthority', |
| 46 | + 'Disallowed', |
| 47 | + 'My', |
| 48 | + 'Root', |
| 49 | + 'TrustedPeople', |
| 50 | + 'TrustedPublisher' |
| 51 | + )] |
| 52 | + [string]$StoreName = 'My', |
| 53 | + |
| 54 | + [Parameter(Mandatory = $false)] |
| 55 | + [ValidateSet( |
| 56 | + 'CurrentUser', |
| 57 | + 'LocalMachine' |
| 58 | + )] |
| 59 | + [string]$StoreLocation = 'CurrentUser', |
| 60 | + |
| 61 | + [Parameter(Mandatory = $false)] |
| 62 | + [ValidateSet( |
| 63 | + 'ReadOnly', |
| 64 | + 'ReadWrite', |
| 65 | + 'MaxAllowed', |
| 66 | + 'OpenExistingOnly', |
| 67 | + 'InclueArchived' |
| 68 | + )] |
| 69 | + [string]$OpenFlags = 'ReadWrite' |
| 70 | + ) |
| 71 | + begin { |
| 72 | + $Store = [System.Security.Cryptography.X509Certificates.X509Store]::new($StoreName, $StoreLocation) |
| 73 | + try { |
| 74 | + $Store.Open($OpenFlags) |
| 75 | + } |
| 76 | + catch { |
| 77 | + $_.Exception.Message | Write-Error -ErrorAction Stop |
| 78 | + } |
| 79 | + } |
| 80 | + process { |
| 81 | + try { |
| 82 | + $cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new( |
| 83 | + $Path, |
| 84 | + $null, |
| 85 | + ( |
| 86 | + [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable -bor |
| 87 | + [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet |
| 88 | + ) |
| 89 | + ) |
| 90 | + |
| 91 | + if (Test-CSCertificate -Thumbprint $cert.Thumbprint) { |
| 92 | + Write-Warning -Message ('The certificate with thumbprint {0} is already present!' -f $cert.Thumbprint) |
| 93 | + } |
| 94 | + else { |
| 95 | + $Store.Add($cert) |
| 96 | + } |
| 97 | + } |
| 98 | + catch { |
| 99 | + $_.Exception.Message | Write-Error -ErrorAction Stop |
| 100 | + $ErrorParams = @{ |
| 101 | + ErrorAction = 'Stop' |
| 102 | + Exception = [System.Exception]::new( |
| 103 | + 'Could not read or add the pfx certificate!' |
| 104 | + ) |
| 105 | + } |
| 106 | + Write-Error @ErrorParams |
| 107 | + } |
| 108 | + } |
| 109 | + end { |
| 110 | + $Store.Close() |
| 111 | + } |
| 112 | +} |
0 commit comments