Exemple #1
0
        /// <summary>
        /// Process Record
        /// </summary>
        protected override void ProcessRecord()
        {
            Data.KeyVaultCertificate[] results = KeyVaultCertificateHelper.GetItems(Id, VaultName, Name, Thumbprint, Tags, Export, Exact);

            // Unroll the object
            foreach (Data.KeyVaultCertificate result in results)
            {
                WriteObject(result);
            }
        }
Exemple #2
0
        /// <summary>
        /// Process Record
        /// </summary>
        protected override void ProcessRecord()
        {
            KeyVaultHelper.GetItemThrow(null, VaultName, true);
            KeyVaultCertificateHelper.ThrowIfItemExists(null, VaultName, Name, null, null, false, true);

            byte[] CertificateByteArray;
            switch (ParameterSetName)
            {
            case ImportFromFileParameterSetName:
                CertificateByteArray = File.ReadAllBytes(FileName);
                break;

            case ImportFromStringParameterSetName:
                CertificateByteArray = Convert.FromBase64String(CertificateString);
                break;

            default:
                WriteError(
                    (new PSAdminException(PSAdminExceptionType.ParameterSetNotFound, Name, "Name")).GetErrorRecord()
                    );
                return;
            }
            X509Certificate2 x509 = new X509Certificate2(CertificateByteArray, Password, X509KeyStorageFlags.Exportable);

            /*
             * // Certificate should NOT throw if Private Key is not available.
             * if (x509.HasPrivateKey)
             * {
             *  x509.Dispose();
             *  WriteError(
             *      PSAdminException.Create(KevinExceptions.CertificateRequiresPrivateKey)
             *  );
             * }
             */

            byte[] rawcert = x509.Export(X509ContentType.Pkcs12, x509.Thumbprint);

            Hashtable item = new Hashtable {
                { "Id", Guid.NewGuid().ToString().Replace("-", "") },
                { "VaultName", VaultName },
                { "Name", Name },
                { "Version", Version },
                { "Enabled", true },
                { "DeletedDate", null },
                { "RecoveryLevel", RecoveryLevel },
                { "Created", DateTime.UtcNow },
                { "Updated", DateTime.UtcNow },
                { "NotBefore", x509.NotBefore },
                { "Expires", x509.NotAfter },
                { "ScheduledPurgeDate", ScheduledPurgeDate },
                { "SecretId", x509.SerialNumber },
                { "KeyId", x509.SerialNumber },
                { "Certificate", rawcert },
                { "Thumbprint", x509.Thumbprint },
                { "Tags", (Tags != null) ? String.Join(";", Tags) : null }
            };

            x509.Dispose();

            bool issuccessful = Call(item);

            if (!issuccessful)
            {
                WriteError(
                    (new PSAdminException(PSAdminExceptionType.RowCreate)).GetErrorRecord()
                    );
            }
            if (Passthru)
            {
                Data.KeyVaultCertificate[] results = KeyVaultCertificateHelper.GetItems(null, VaultName, Name, null, null, false, true);

                // Unroll the object
                foreach (Data.KeyVaultCertificate result in results)
                {
                    WriteObject(result);
                }
            }
        }