ps/Modules/Alkami.Ops.Certificates/Cmdlets/GetAllThumbprintsInStores.cs
2023-05-30 22:51:22 -07:00

52 lines
1.8 KiB
C#

using Alkami.Ops.Certificates.Utilities;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Security.Cryptography.X509Certificates;
namespace Alkami.Ops.Certificates.cmdlets
{
/// <summary>
/// Gets the certificate thumbprints for every certificate in the local certificate store.
/// </summary>
[Cmdlet("Get", "AllThumbprintsInStores")]
[OutputType(typeof(List<string>))]
public class GetAllThumbprintsInStores : Cmdlet
{
[Parameter(Position = 0, Mandatory = true)]
[ValidateSet("all", "my", "CertificateAuthority", "root", "trustedpeople")]
public string certStore;
protected override void ProcessRecord()
{
// Think of this like a return, but it returns the object to the powershell output stream, like write-output
WriteObject(GetName(this.certStore));
}
public IEnumerable<string> GetName(string certStore)
{
var storesToSearch = new string[4];
if (certStore == "all")
{
storesToSearch = new string[] { "my", "CertificateAuthority", "root", "trustedpeople" };
}
else
{
storesToSearch = new string[] { certStore };
}
X509Certificate2Collection allCertificates = new X509Certificate2Collection();
foreach (var storeString in storesToSearch)
{
StoreName.TryParse(storeString, true, out StoreName storeName);
allCertificates.AddRange(Common.Cryptography.CertificateHelper.GetAllCertificates(storeName, StoreLocation.LocalMachine));
}
IEnumerable<string> thumbprints = allCertificates.ToList()
.Select(cert => cert.Thumbprint);
return thumbprints;
}
}
}