2019-04-29 16:05:43 +02:00
|
|
|
function Use-CSCertificate {
|
2019-01-16 12:55:29 +01:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
2019-04-29 16:05:43 +02:00
|
|
|
Links an existing PFX Certificate to a CredentialStore.
|
2019-01-16 12:55:29 +01:00
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
Linking a certificate is needed if you plan to use the same CredentialStore in cross platform scenarios.
|
|
|
|
|
|
|
|
.PARAMETER Path
|
|
|
|
Specify the path to the PFX Certificate you want to link for usage.
|
|
|
|
|
2019-04-29 16:05:43 +02:00
|
|
|
.PARAMETER CredentialStore
|
|
|
|
Specify a custom path for a shared credential store.
|
|
|
|
|
|
|
|
.PARAMETER Shared
|
|
|
|
Use the credential store in shared mode.
|
|
|
|
|
|
|
|
.PARAMETER UseCertStore
|
|
|
|
Use the given certificate and import it into the corresponding certificate store.
|
|
|
|
|
2019-01-16 12:55:29 +01:00
|
|
|
.INPUTS
|
|
|
|
[None]
|
|
|
|
|
|
|
|
.OUTPUTS
|
|
|
|
[None]
|
|
|
|
|
|
|
|
.EXAMPLE
|
2019-04-29 16:05:43 +02:00
|
|
|
Use-CSCertificate -Path 'C:\cert.pfx'
|
2019-01-16 12:55:29 +01:00
|
|
|
#>
|
2022-06-28 08:56:33 +02:00
|
|
|
|
|
|
|
[CmdletBinding(DefaultParameterSetName = 'Private')]
|
2019-01-16 12:55:29 +01:00
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "")]
|
2022-06-28 08:56:33 +02:00
|
|
|
param (
|
|
|
|
[Parameter(Mandatory = $true, ParameterSetName = 'Private')]
|
|
|
|
[Parameter(Mandatory = $true, ParameterSetName = 'Shared')]
|
2019-01-16 12:55:29 +01:00
|
|
|
[ValidateNotNullOrEmpty()]
|
|
|
|
[string]$Path,
|
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
[Parameter(Mandatory = $false, ParameterSetName = 'Shared')]
|
2019-01-16 12:55:29 +01:00
|
|
|
[ValidateNotNullOrEmpty()]
|
|
|
|
[string]$CredentialStore,
|
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
[Parameter(Mandatory = $true, ParameterSetName = 'Shared')]
|
2019-04-29 16:05:43 +02:00
|
|
|
[switch]$Shared,
|
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
[Parameter(Mandatory = $false, ParameterSetName = 'Private')]
|
|
|
|
[Parameter(Mandatory = $false, ParameterSetName = 'Shared')]
|
|
|
|
[switch]$UseCertStore
|
2019-01-16 12:55:29 +01:00
|
|
|
)
|
2022-06-28 08:56:33 +02:00
|
|
|
begin {}
|
2019-01-16 12:55:29 +01:00
|
|
|
|
|
|
|
process {
|
|
|
|
try {
|
|
|
|
# We need to resolve the path to make sure it has the correct platform specific syntax.
|
|
|
|
# And it should also exist.
|
|
|
|
$validPath = Resolve-Path -Path $Path -ErrorAction Stop
|
|
|
|
$PfxCertificate = Get-PfxCertificate -FilePath $validPath -ErrorAction Stop
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
$_.Exception.Error | Write-Error
|
|
|
|
$ErrorParams = @{
|
|
|
|
Message = 'The given PFX certificate does not exist!'
|
|
|
|
ErrorAction = 'Stop'
|
|
|
|
}
|
|
|
|
Write-Error @ErrorParams
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-06-28 08:56:33 +02:00
|
|
|
if ($PSCmdlet.ParameterSetName -eq 'Private') {
|
2019-01-16 12:55:29 +01:00
|
|
|
$StorePath = Get-DefaultCredentialStorePath
|
|
|
|
$CS = Get-CredentialStore
|
|
|
|
}
|
2022-06-28 08:56:33 +02:00
|
|
|
elseif ($PSCmdlet.ParameterSetName -eq 'Shared' ) {
|
2019-01-16 12:55:29 +01:00
|
|
|
if (!($PSBoundParameters.ContainsKey('CredentialStore'))) {
|
|
|
|
$StorePath = Get-DefaultCredentialStorePath -Shared
|
|
|
|
$CS = Get-CredentialStore -Shared
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$StorePath = $CredentialStore
|
|
|
|
$CS = Get-CredentialStore -Shared -Path $CredentialStore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
$_.Exception.Error | Write-Error
|
|
|
|
$ErrorParams = @{
|
|
|
|
Message = 'The given CredentialStore does not exist!'
|
|
|
|
ErrorAction = 'Stop'
|
|
|
|
}
|
|
|
|
Write-Error @ErrorParams
|
|
|
|
}
|
|
|
|
|
|
|
|
# Lets first check if the thumbprint matches
|
|
|
|
if (($CS.Thumbprint -notmatch $PfxCertificate.Thumbprint) -and ($CS.Thumbprint.Length -ne 0)) {
|
|
|
|
Write-Warning @"
|
|
|
|
You are trying to map an unknown certificate.
|
|
|
|
Make sure you used the same AES keys for encrypting!
|
|
|
|
"@
|
|
|
|
}
|
|
|
|
|
2019-04-29 16:05:43 +02:00
|
|
|
if ($UseCertStore) {
|
|
|
|
Import-CSCertificate -Type $PSCmdlet.ParameterSetName -Path $Path
|
|
|
|
$CS.Thumbprint = $PfxCertificate.Thumbprint
|
|
|
|
$CS.PfxCertificate = $null
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$CS.PfxCertificate = $validPath.Path
|
|
|
|
}
|
2019-01-16 12:55:29 +01:00
|
|
|
$CS | ConvertTo-Json -Depth 5 | Out-File -FilePath $StorePath -Force -Encoding utf8
|
|
|
|
}
|
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
end {}
|
2019-01-16 12:55:29 +01:00
|
|
|
}
|