PSCredentialStore/src/Certificate/Import-CSCertificate.ps1

73 lines
2.1 KiB
PowerShell
Raw Normal View History

function Import-CSCertificate {
<#
.SYNOPSIS
2019-04-08 12:37:30 +02:00
Imports a linked certificate to the valid store location.
.DESCRIPTION
2019-04-08 12:37:30 +02:00
Import-CSCertificate takes a pfx certificate file and imports it to the supposed certificate store for
private and shared credential stores.
2019-04-08 12:37:30 +02:00
.PARAMETER Type
Select between the a private and shared credential store.
.PARAMETER Path
Provide a valid path to pfx certificate file.
.INPUTS
2019-04-08 15:25:08 +02:00
[None]
.OUTPUTS
2019-04-08 15:25:08 +02:00
[None]
.EXAMPLE
2019-04-08 15:25:08 +02:00
Import-CSCertificate -Type 'Private' -Path (Join-Path -Path $Env:APPDATA -ChildItem 'PfxCertificate.pfx')
.NOTES
2019-04-08 12:37:30 +02:00
- File Name : Import-CSCertificate.ps1
- Author : Marco Blessing - marco.blessing@googlemail.com
- Requires :
.LINK
https://github.com/OCram85/PSCredentialStore
#>
[CmdletBinding()]
[OutputType()]
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
2019-04-05 11:14:18 +02:00
[ValidateSet('Private', 'Shared')]
[string]$Type,
2019-04-05 11:14:18 +02:00
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo]$Path
)
begin {
2019-04-05 11:14:18 +02:00
if (! (Test-Path -Path $Path)) {
$ErrorParams = @{
ErrorAction = 'Stop'
Exception = [System.Exception]::new(
2019-04-05 11:14:18 +02:00
('File {0} not found!') -f $Path
)
}
Write-Error @ErrorParams
}
}
2019-04-05 11:14:18 +02:00
process {
2019-04-08 12:37:30 +02:00
# Import to CurrentUser\My store for windows and linux
2019-04-05 11:14:18 +02:00
if ($Type -eq 'Private') {
Import-CSPfxCertificate -Path $Path -StoreName 'My' -StoreLocation 'CurrentUser' -OpenFlags 'ReadWrite'
}
elseif ( (! $isLinux ) -and ($Type -eq 'Shared') ) {
Import-CSPfxCertificate -Path $Path -StoreName 'My' -StoreLocation 'LocalMachine' -OpenFlags 'ReadWrite'
2019-04-05 11:14:18 +02:00
}
elseif ( ($isLinux) -and ($Type -eq 'Shared') ) {
Import-CSPfxCertificate -Path $Path -StoreName 'My' -StoreLocation 'CurrentUser' -OpenFlags 'ReadWrite'
2019-04-05 11:14:18 +02:00
}
}
end {
}
}