Пример #1
0
        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);
            });
        }