Private/Get-ClientCertificate.ps1
|
<#
.Synopsis Created on: 03/04/2024 Updated on: 16/11/2026 Created by: Ben Whitmore Filename: Get-ClientCertificate.ps1 .Description Function to get a client certificate from the local certificate store .PARAMETER LogId The component (script name) passed as LogID to the 'Write-Log' function. This parameter is built from the line number of the call from the function up the .PARAMETER Thumbprint The thumbprint of the client certificate to get. #> function Get-ClientCertificate { param ( [Parameter(Mandatory = $false, ValuefromPipeline = $false, HelpMessage = "The component (script name) passed as LogID to the 'Write-Log' function")] [string]$LogId = $($MyInvocation.MyCommand).Name, [Parameter(Mandatory = $true, ValueFromPipeline = $false, HelpMessage = 'Thumbprint of the client certificate to get')] [string]$Thumbprint ) process { # Search both certificate stores and return the certificate from the first store that has it foreach ($certStore in @('CurrentUser', 'LocalMachine')) { $result = Get-Item -Path "Cert:\$($certStore)\My\$($Thumbprint)" -ErrorAction SilentlyContinue if ($result) { Write-LogAndHost -Message ("Certificate with thumbprint '{0}' was found in the '{1}' certificate store with the subject '{2}'" -f $Thumbprint, $certStore, $result.Subject) -LogId $LogId -ForegroundColor Green return $result } Write-LogAndHost -Message ("Certificate with thumbprint '{0}' was not found in the '{1}' certificate store" -f $Thumbprint, $certStore) -LogId $LogId -Severity 2 } return $null } } |