ps/Modules/Alkami.Ops.Certificates/Cmdlets/GetCertThumbprintByName.cs

57 lines
1.9 KiB
C#
Raw Normal View History

2023-05-30 22:51:22 -07:00
using System;
using System.Management.Automation;
using System.Security.Cryptography.X509Certificates;
namespace Alkami.Ops.Certificates.cmdlets
{
/// <summary>
/// Gets the thumbprint value for a given cert Name
/// </summary>
[Cmdlet("Get", "CertThumbprintByName")]
[OutputType(typeof(string))]
public class GetCertThumbprintByName : Cmdlet
{
[Parameter(Position = 0, Mandatory = true)]
public string CertName;
[Parameter(Position = 1, Mandatory = true)]
[ValidateSet("all", "my", "CertificateAuthority", "root", "trustedpeople")]
public string CertStore;
protected override void ProcessRecord()
{
GetThumbprint(CertName, CertStore);
}
public void GetThumbprint(string CertName, string CertStore)
{
var StoresToSearch = new string[4];
if (CertStore == "all")
{
StoresToSearch = new string[] {"my", "CertificateAuthority", "root", "trustedpeople"};
}
else
{
StoresToSearch = new string[] {CertStore};
}
Console.WriteLine($"Searching {CertStore} Store(s) for {CertName}");
foreach (var storeString in StoresToSearch)
{
StoreName.TryParse(storeString, true, out StoreName storeName);
X509Certificate2 certificate =
Common.Cryptography.CertificateHelper.FindCertificatebySubjectOrSAN(CertName, storeName,
StoreLocation.LocalMachine);
if (certificate != null)
{
Console.WriteLine($"found {certificate.Thumbprint} in {storeString}");
}
else
{
Console.WriteLine($"cert not found in {storeName}");
}
}
}
}
}