public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                base.ExecuteCmdlet();

                if (!Directory.Exists(TargetLocation))
                {
                    throw new ArgumentException(Resources.VaultCredPathException);
                }

                string subscriptionId = DefaultContext.Subscription.Id.ToString();
                string resourceType = "BackupVault";
                string displayName = subscriptionId + "_" + Vault.ResourceGroupName + "_" + Vault.Name;

                WriteDebug(string.Format(CultureInfo.InvariantCulture,
                                          Resources.ExecutingGetVaultCredCmdlet,
                                          subscriptionId, Vault.ResourceGroupName, Vault.Name, TargetLocation));

                X509Certificate2 cert = CertUtils.CreateSelfSignedCert(CertUtils.DefaultIssuer,
                                                                       CertUtils.GenerateCertFriendlyName(subscriptionId, Vault.Name),
                                                                       CertUtils.DefaultPassword,
                                                                       DateTime.UtcNow.AddMinutes(-10),
                                                                       DateTime.UtcNow.AddHours(this.GetCertificateExpiryInHours()));

                AcsNamespace acsNamespace = new AcsNamespace();
                string channelIntegrityKey = string.Empty;
                try
                {
                    // Upload cert into ID Mgmt
                    WriteDebug(string.Format(CultureInfo.InvariantCulture, Resources.UploadingCertToIdmgmt));
                    acsNamespace = UploadCert(cert, subscriptionId, Vault.Name, resourceType, Vault.ResourceGroupName);
                    WriteDebug(string.Format(CultureInfo.InvariantCulture, Resources.UploadedCertToIdmgmt));
                }
                catch (Exception exception)
                {
                    throw exception;
                }

                // generate vault credentials
                string vaultCredsFileContent = GenerateVaultCreds(cert, subscriptionId, resourceType, acsNamespace);

                // NOTE: One of the scenarios for this cmdlet is to generate a file which will be an input to DPM servers. 
                //       We found a bug in the DPM UI which is looking for a particular namespace in the input file.
                //       The below is a hack to circumvent this issue and this would be removed once the bug can be fixed.
                vaultCredsFileContent = vaultCredsFileContent.Replace("Microsoft.Azure.Commands.AzureBackup.Models",
                                                                      "Microsoft.Azure.Portal.RecoveryServices.Models.Common");

                // prepare for download
                string fileName = string.Format("{0}_{1}.VaultCredentials", displayName, DateTime.UtcNow.ToString("yyyy-dd-M--HH-mm-ss"));
                string filePath = Path.Combine(TargetLocation, fileName);
                WriteDebug(string.Format(Resources.SavingVaultCred, filePath));

                File.WriteAllBytes(filePath, Encoding.UTF8.GetBytes(vaultCredsFileContent));

                // Output filename back to user
                WriteObject(fileName);
            });
        }
 /// <summary>
 /// Initializes a new instance of the VaultCreds class
 /// </summary>
 /// <param name="subscriptionId">subscription id</param>
 /// <param name="resourceType">resource type</param>
 /// <param name="resourceName">resource name</param>
 /// <param name="managementCert">management cert</param>
 /// <param name="acsNamespace">acs namespace</param>
 public VaultCreds(string subscriptionId, string resourceType, string resourceName, string managementCert, AcsNamespace acsNamespace)
 {
     SubscriptionId = subscriptionId;
     ResourceType   = resourceType;
     ResourceName   = resourceName;
     ManagementCert = managementCert;
     AcsNamespace   = acsNamespace;
 }
 /// <summary>
 /// Initializes a new instance of the VaultCreds class
 /// </summary>
 /// <param name="subscriptionId">subscription id</param>
 /// <param name="resourceType">resource type</param>
 /// <param name="resourceName">resource name</param>
 /// <param name="managementCert">management cert</param>
 /// <param name="acsNamespace">acs namespace</param>
 public VaultCreds(string subscriptionId, string resourceType, string resourceName, string managementCert, AcsNamespace acsNamespace)
 {
     SubscriptionId = subscriptionId;
     ResourceType = resourceType;
     ResourceName = resourceName;
     ManagementCert = managementCert;
     AcsNamespace = acsNamespace;
 }
        /// <summary>
        /// Generates vault creds file content for backup Vault
        /// </summary>
        /// <param name="cert">management certificate</param>
        /// <param name="subscriptionId">subscription Id</param>
        /// <param name="resourceType">resource type</param>
        /// <param name="displayName">display name</param>
        /// <param name="acsNamespace">acs namespace</param>
        /// <returns>xml file in string format</returns>
        private string GenerateVaultCredsForBackup(X509Certificate2 cert, string subscriptionId, string resourceType, AcsNamespace acsNamespace)
        {
            using (var output = new MemoryStream())
            {
                using (var writer = XmlWriter.Create(output, GetXmlWriterSettings()))
                {
                    BackupVaultCreds backupVaultCreds = new BackupVaultCreds(subscriptionId,
                                                                             resourceType,
                                                                             Vault.Name,
                                                                             CertUtils.SerializeCert(cert, X509ContentType.Pfx),
                                                                             acsNamespace,
                                                                             GetAgentLinks());
                    DataContractSerializer serializer = new DataContractSerializer(typeof(BackupVaultCreds));
                    serializer.WriteObject(writer, backupVaultCreds);

                    WriteDebug(string.Format(CultureInfo.InvariantCulture, "RecoveryService - Backup Vault - Successfully serialized the file content"));
                }

                return Encoding.UTF8.GetString(output.ToArray());
            }
        }
 /// <summary>
 /// Generates vault creds file
 /// </summary>
 /// <param name="cert">management certificate</param>
 /// <param name="subscriptionId">subscription Id</param>
 /// <param name="resourceType">resource type</param>
 /// <param name="displayName">display name</param>
 /// <param name="acsNamespace">acs namespace</param>
 /// <returns>xml file in string format</returns>
 private string GenerateVaultCreds(X509Certificate2 cert, string subscriptionId, string resourceType, AcsNamespace acsNamespace)
 {
     try
     {
         return GenerateVaultCredsForBackup(cert, subscriptionId, resourceType, acsNamespace);
     }
     catch (Exception exception)
     {
         throw exception;
     }
 }
 /// <summary>
 /// Initializes a new instance of the BackupVaultCreds class
 /// </summary>
 /// <param name="subscriptionId">subscription Id</param>
 /// <param name="resourceType">resource type</param>
 /// <param name="resourceName">resource name</param>
 /// <param name="managementCert">management cert</param>
 /// <param name="acsNamespace">acs namespace</param>
 /// <param name="agentLinks">agent links</param>
 public BackupVaultCreds(string subscriptionId, string resourceType, string resourceName, string managementCert, AcsNamespace acsNamespace, string agentLinks)
     : this(subscriptionId, resourceType, resourceName, managementCert, acsNamespace)
 {
     AgentLinks = agentLinks;
 }
 /// <summary>
 /// Initializes a new instance of the BackupVaultCreds class
 /// </summary>
 /// <param name="subscriptionId">subscription Id</param>
 /// <param name="resourceType">resource type</param>
 /// <param name="resourceName">resource name</param>
 /// <param name="managementCert">management cert</param>
 /// <param name="acsNamespace">acs namespace</param>
 public BackupVaultCreds(string subscriptionId, string resourceType, string resourceName, string managementCert, AcsNamespace acsNamespace)
     : base(subscriptionId, resourceType, resourceName, managementCert, acsNamespace)
 {
 }
 /// <summary>
 /// Initializes a new instance of the BackupVaultCreds class
 /// </summary>
 /// <param name="subscriptionId">subscription Id</param>
 /// <param name="resourceType">resource type</param>
 /// <param name="resourceName">resource name</param>
 /// <param name="managementCert">management cert</param>
 /// <param name="acsNamespace">acs namespace</param>
 /// <param name="agentLinks">agent links</param>
 public BackupVaultCreds(string subscriptionId, string resourceType, string resourceName, string managementCert, AcsNamespace acsNamespace, string agentLinks)
     : this(subscriptionId, resourceType, resourceName, managementCert, acsNamespace)
 {
     AgentLinks = agentLinks;
 }
 /// <summary>
 /// Initializes a new instance of the BackupVaultCreds class
 /// </summary>
 /// <param name="subscriptionId">subscription Id</param>
 /// <param name="resourceType">resource type</param>
 /// <param name="resourceName">resource name</param>
 /// <param name="managementCert">management cert</param>
 /// <param name="acsNamespace">acs namespace</param>
 public BackupVaultCreds(string subscriptionId, string resourceType, string resourceName, string managementCert, AcsNamespace acsNamespace)
     : base(subscriptionId, resourceType, resourceName, managementCert, acsNamespace)
 { }