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
{
///
/// Gets the certificate thumbprints for every certificate in the local certificate store.
///
[Cmdlet("Get", "AllThumbprintsInStores")]
[OutputType(typeof(List))]
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 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 thumbprints = allCertificates.ToList()
.Select(cert => cert.Thumbprint);
return thumbprints;
}
}
}