using Newtonsoft.Json; using System.Collections.Generic; using System.IO; using System.Management.Automation; using System.Security.Cryptography.X509Certificates; namespace Alkami.Ops.Certificates.Cmdlets { [Cmdlet("Write", "CertStoreHashToFile")] [OutputType(typeof(string))] public class WriteCertStoreHashToFile : Cmdlet { [Parameter(Position = 0, Mandatory = false)] public string FilePath { get; set; } = @"C:\Tools\CertificateManagement\TrackedThumbprints.json"; protected override void ProcessRecord() { WriteCertHashesToFile(); } private void WriteCertHashesToFile() { List thumbprints = new List(); string[] storesToSearch = new string[] { "my", "CertificateAuthority", "root", "trustedpeople" }; foreach (string storeString in storesToSearch) { StoreName.TryParse(storeString, true, out StoreName storeName); X509Certificate2Collection Certificates = new X509Certificate2Collection(); Certificates.AddRange(Common.Cryptography.CertificateHelper.GetAllCertificates(storeName, StoreLocation.LocalMachine)); foreach (X509Certificate2 certificate in Certificates) { thumbprints.Add(certificate.Thumbprint); } } File.WriteAllText(FilePath, JsonConvert.SerializeObject(thumbprints)); } } }