Functions/SSL/Get-CertKeyInfo.ps1
Function Get-CertKeyInfo { [cmdletbinding()] Param () DynamicParam { # Get all Certs $Certs = Get-ChildItem -Path Cert:\ -Recurse | where Thumbprint # Instantiate Runtime Parameter Dictionary, Attach Runtime Parameters, and return $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary $RuntimeParameterDictionary.Add('Name', (New-DynamicParameter -ParamName 'Name' -ValueType string -Dataset ($Certs | where FriendlyName).FriendlyName -Mandatory $false -ParameterSetName "Name")) $RuntimeParameterDictionary.Add('Subject', (New-DynamicParameter -ParamName 'Subject' -ValueType string -Dataset ($Certs | where Subject).Subject -Mandatory $false -ParameterSetName "Subject")) $RuntimeParameterDictionary.Add('Thumbprint', (New-DynamicParameter -ParamName 'Thumbprint' -ValueType string -Dataset $Certs.Thumbprint -Mandatory $false -ParameterSetName "Thumbprint")) return $RuntimeParameterDictionary } Begin { # Convert Runtime Parameter Dictionary into Available Constants foreach ($key in $RuntimeParameterDictionary.keys){New-Variable -Name $key -Value $RuntimeParameterDictionary.$key.value} # Select Certificates $Certificates = if($Name) {[array]($Certs | where FriendlyName -eq $Name)} elseif($Subject) {[array]($Certs | where Subject -eq $Subject)} elseif($Thumbprint) {[array]($Certs | where Thumbprint -eq $Thumbprint)} else {[array]($Certs)} } Process { foreach ($Certificate in $Certificates) { $CertLoc = (($Certificate.PSParentPath -split '\\')[-2] -split "::")[-1] $CertPath = ($Certificate.PSParentPath -split '\\')[-1] $CUData = Certutil -store $CertPath $($certificate.Thumbprint) $CUData2 = $CUData[2..($CUData.count-2)].trim() $CSPName = "$($CUData2 -like "Provider = *" -replace "Provider = ",'')" $KCName = "$($CUData2 -like "Key Container = *" -replace "Key Container = ",'')" $UCName = "$($CUData2 -like "Unique container name: *" -replace "Unique container name: ",'')" $SCName = "$($CUData2 -like "Simple container name: *" -replace "Simple container name: ",'')" if($SCName -and $KCName) { $Container = $SCName $UniqueContainer = $KCName } elseif($KCName -and $UCName) { $Container = $KCName $UniqueContainer = $UCName } $CCKI = Get-CertCSPKeyInfo -CSPName $CSPName | where KeyContainerName -eq $Container | where UniqueContainerName -eq $UniqueContainer $Template = "$($CUData2 -like "Template: *" -replace "Template: ",'')" $HASHData = $CUData2 -like "Cert Hash*" $HashAlg = (($HASHData -split '\(')[-1] -split '\)')[0] $HashVal = ($HASHData -split ': ')[-1] [pscustomobject]([ordered]@{ Name = $Certificate.FriendlyName Subject = $Certificate.Subject Thumbprint = $Certificate.Thumbprint CertLocation = $CertLoc CertStore = $CertPath Serial = $Certificate.SerialNumber Template = $Template CertHashAlg = $HashAlg CertHash = $HashVal HasPrivateKey = $Certificate.HasPrivateKey PrivateKey = $Certificate.PrivateKey KeyProvider = $CSPName KeyProviderType = $CCKI.ProviderType KeyContainer = $Container KeyUniqueContainerName = $UniqueContainer KeyAlgorithm = $CCKI.KeyAlgorithm KeyAlgorithmGroup = $CCKI.KeyAlgorithmGroup }) } } } |