Inheritance: BatchClientParametersBase
        public override void ExecuteCmdlet()
        {
            NewCertificateParameters parameters = new NewCertificateParameters(this.BatchContext, this.FilePath, this.RawData,
                this.AdditionalBehaviors)
            {
                Password = this.Password
            };

            BatchClient.AddCertificate(parameters);
        }
        protected override void ProcessRecord()
        {
            NewCertificateParameters parameters = new NewCertificateParameters(this.BatchContext, this.FilePath, this.RawData,
                this.AdditionalBehaviors)
            {
                Password = this.Password
            };

            BatchClient.AddCertificate(parameters);
        }
        /// <summary>
        /// Adds a certificate to the specified Batch account.
        /// </summary>
        /// <param name="parameters">The parameters to use when creating the certificate.</param>
        public void AddCertificate(NewCertificateParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            CertificateOperations certOperations = parameters.Context.BatchOMClient.CertificateOperations;
            Certificate           unboundCert;

            if (!string.IsNullOrWhiteSpace(parameters.FilePath))
            {
                if (string.IsNullOrWhiteSpace(parameters.Password))
                {
                    unboundCert = certOperations.CreateCertificate(parameters.FilePath);
                }
                else
                {
                    unboundCert = certOperations.CreateCertificate(parameters.FilePath, parameters.Password);
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(parameters.Password))
                {
                    unboundCert = certOperations.CreateCertificate(parameters.RawData);
                }
                else
                {
                    unboundCert = certOperations.CreateCertificate(parameters.RawData, parameters.Password);
                }
            }

            WriteVerbose(string.Format(Resources.AddingCertificate, unboundCert.Thumbprint));
            unboundCert.Commit(parameters.AdditionalBehaviors);
        }
        /// <summary>
        /// Adds a certificate to the specified Batch account.
        /// </summary>
        /// <param name="parameters">The parameters to use when creating the certificate.</param>
        public void AddCertificate(NewCertificateParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            CertificateOperations certOperations = parameters.Context.BatchOMClient.CertificateOperations;
            Certificate unboundCert;

            if (!string.IsNullOrWhiteSpace(parameters.FilePath))
            {
                if (string.IsNullOrWhiteSpace(parameters.Password))
                {
                    unboundCert = certOperations.CreateCertificate(parameters.FilePath);
                }
                else
                {
                    unboundCert = certOperations.CreateCertificate(parameters.FilePath, parameters.Password);
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(parameters.Password))
                {
                    unboundCert = certOperations.CreateCertificate(parameters.RawData);
                }
                else
                {
                    unboundCert = certOperations.CreateCertificate(parameters.RawData, parameters.Password);
                }
            }

            WriteVerbose(string.Format(Resources.AddingCertificate, unboundCert.Thumbprint));
            unboundCert.Commit(parameters.AdditionalBehaviors);
        }
        /// <summary>
        /// Adds a test certificate for use in Scenario tests. Returns the thumbprint of the cert.
        /// </summary>
        public static string AddTestCertificate(BatchController controller, BatchAccountContext context, string filePath)
        {
            BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient);

            X509Certificate2 cert = new X509Certificate2(filePath);
            ListCertificateOptions getParameters = new ListCertificateOptions(context) 
            { 
                ThumbprintAlgorithm = BatchTestHelpers.TestCertificateAlgorithm, 
                Thumbprint = cert.Thumbprint,
                Select = "thumbprint,state"
            };

            try
            {
                PSCertificate existingCert = client.ListCertificates(getParameters).FirstOrDefault();
                DateTime start = DateTime.Now;
                DateTime end = start.AddMinutes(5);

                // Cert might still be deleting from other tests, so we wait for the delete to finish.
                while (existingCert != null && existingCert.State == CertificateState.Deleting)
                {
                    if (DateTime.Now > end)
                    {
                        throw new TimeoutException("Timed out waiting for existing cert to be deleted.");
                    }
                    Sleep(5000);
                    existingCert = client.ListCertificates(getParameters).FirstOrDefault();
                }
            }
            catch (AggregateException ex)
            {
                foreach (Exception inner in ex.InnerExceptions)
                {
                    BatchException batchEx = inner as BatchException;
                    // When the cert doesn't exist, we get a 404 error. For all other errors, throw.
                    if (batchEx == null || !batchEx.Message.Contains("CertificateNotFound"))
                    {
                        throw;
                    }
                }
            }

            NewCertificateParameters parameters = new NewCertificateParameters(context, null, cert.RawData);

            client.AddCertificate(parameters);

            return cert.Thumbprint;
        }