PSCredentialStore/src/Certificate/Test-CSCertificate.ps1

77 lines
2.3 KiB
PowerShell
Raw Normal View History

function Test-CSCertificate {
<#
.SYNOPSIS
2019-04-08 12:37:30 +02:00
Tests if the linked certificate is store ein the specified cert stores.
.DESCRIPTION
2019-04-08 12:37:30 +02:00
Test-CSCertificate should be an easy high level test for the linked certificate.
2019-04-05 11:14:18 +02:00
.PARAMETER Type
Select between 'Private' or 'Shared'.
.INPUTS
[None]
.OUTPUTS
[bool]
.EXAMPLE
2019-04-08 12:37:30 +02:00
Test-CSCertificate -Type 'Shared'
.NOTES
2019-04-08 12:37:30 +02:00
- File Name : Test-CSCertificate.ps1
- Author : Marco Blessing - marco.blessing@googlemail.com
- Requires :
.LINK
https://github.com/OCram85/PSCredentialStore
#>
[CmdletBinding()]
[OutputType([bool])]
param(
2019-04-05 11:14:18 +02:00
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
2019-04-05 11:14:18 +02:00
[ValidateSet('Private', 'Shared')]
[string]$Type
)
begin {
2019-04-05 11:14:18 +02:00
if ($Type -eq 'Private') {
$CS = Get-CredentialStore
}
2019-04-05 11:14:18 +02:00
elseif ($Type -eq 'Shared') {
$CS = Get-CredentialStore -Shared
}
if ($null -ne $CS.PfxCertificate) {
2019-04-08 12:37:30 +02:00
Write-Warning 'There is a Pfx certificate file linked in the store. Certificates saved in the Cert store will be ignored!'
}
2019-04-05 11:14:18 +02:00
}
process {
2019-04-05 11:14:18 +02:00
if ($Type -eq 'Private') {
2019-04-05 12:38:26 +02:00
$cert = Get-CSPfXCertificate -Thumbprint $CS.Thumbprint -StoreName 'My' -StoreLocation 'CurrentUser'
2019-04-05 11:14:18 +02:00
}
elseif ($Type -eq 'Shared') {
if ( $isLinux) {
2019-04-05 12:38:26 +02:00
$cert = Get-CSPfxCertificate -Thumbprint $CS.Thumbprint -StoreName 'My' -StoreLocation 'CurrentUser'
2019-04-05 11:14:18 +02:00
if ($null -eq $cert) {
2019-04-05 12:38:26 +02:00
$cert = Get-CSPfxCertificate -Thumbprint $CS.Thumbprint -StoreName 'Root' -StoreLocation 'LocalMachine'
2019-04-05 11:14:18 +02:00
}
}
elseif ( (! $isLinux) -or ($isWindows) ) {
2019-04-05 12:38:26 +02:00
$cert = Get-CSPfxCertificate -Thumbprint $CS.Thumbprint -StoreName 'My' -StoreLocation 'LocalMachine'
2019-04-05 11:14:18 +02:00
if ($null -eq $cert) {
2019-04-05 12:38:26 +02:00
$cert = Get-CSPfxCertificate -Thumbprint $CS.Thumbprint -StoreName 'Root' -StoreLocation 'LocalMachine'
2019-04-05 11:14:18 +02:00
}
}
}
if ($null -eq $cert) {
return $false
}
else {
return $true
}
}
end {
}
}