/// <summary>
        /// ProcessRecord of the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            switch (this.ParameterSetName)
            {
            case ASRParameterSets.ByPEObject:
                this.targetNameOrId = this.ProtectionEntity.FriendlyName;
                break;
            }

            this.ConfirmAction(
                this.Force.IsPresent || 0 != string.CompareOrdinal(this.Protection, Constants.DisableProtection),
                string.Format(Properties.Resources.DisableProtectionWarning, this.targetNameOrId),
                string.Format(Properties.Resources.DisableProtectionWhatIfMessage, this.Protection),
                this.targetNameOrId,
                () =>
            {
                try
                {
                    // ADD SOME VALIDATIONS

                    if (this.Protection == Constants.EnableProtection)
                    {
                        EnableProtectionInput input = new EnableProtectionInput();

                        this.response =
                            RecoveryServicesClient.EnableProtection(
                                this.ProtectionEntity.ProtectionContainerId,
                                this.ProtectionEntity.Name,
                                input);
                    }
                    else
                    {
                        DisableProtectionInput input = new DisableProtectionInput();

                        this.response =
                            RecoveryServicesClient.DisableProtection(
                                this.ProtectionEntity.ProtectionContainerId,
                                this.ProtectionEntity.Name,
                                input);
                    }

                    jobResponse =
                        RecoveryServicesClient
                        .GetAzureSiteRecoveryJobDetails(PSRecoveryServicesClient.GetJobIdFromReponseLocation(response.Location));

                    WriteObject(new ASRJob(jobResponse.Job));

                    if (this.WaitForCompletion.IsPresent)
                    {
                        this.WaitForJobCompletion(this.jobResponse.Job.Name);
                    }
                }
                catch (Exception exception)
                {
                    this.HandleException(exception);
                }
            });
        }
        /// <summary>
        /// ProcessRecord of the command.
        /// </summary>
        public override void ExecuteSiteRecoveryCmdlet()
        {
            base.ExecuteSiteRecoveryCmdlet();

            this.targetNameOrId = this.ProtectionEntity.FriendlyName;
            this.ConfirmAction(
                this.Force.IsPresent || 0 != string.CompareOrdinal(this.Protection, Constants.DisableProtection),
                string.Format(Properties.Resources.DisableProtectionWarning, this.targetNameOrId),
                string.Format(Properties.Resources.DisableProtectionWhatIfMessage, this.Protection),
                this.targetNameOrId,
                () =>
            {
                if (this.Protection == Constants.EnableProtection)
                {
                    if (string.Compare(this.ParameterSetName, ASRParameterSets.DisableDR, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        throw new PSArgumentException(Properties.Resources.PassingPolicyMandatoryForEnablingDR);
                    }

                    EnableProtectionProviderSpecificInput enableProtectionProviderSpecificInput = new EnableProtectionProviderSpecificInput();

                    EnableProtectionInputProperties inputProperties = new EnableProtectionInputProperties()
                    {
                        PolicyId                = this.Policy.ID,
                        ProtectableItemId       = this.ProtectionEntity.ID,
                        ProviderSpecificDetails = enableProtectionProviderSpecificInput
                    };

                    EnableProtectionInput input = new EnableProtectionInput()
                    {
                        Properties = inputProperties
                    };

                    // Process if block only if policy is not null, policy is created for E2A or B2A and parameter set is for enable DR of E2A or B2A
                    if (this.Policy != null &&
                        0 == string.Compare(this.Policy.ReplicationProvider, Constants.HyperVReplicaAzure, StringComparison.OrdinalIgnoreCase) &&
                        (0 == string.Compare(this.ParameterSetName, ASRParameterSets.EnterpriseToAzure, StringComparison.OrdinalIgnoreCase) ||
                         0 == string.Compare(this.ParameterSetName, ASRParameterSets.HyperVSiteToAzure, StringComparison.OrdinalIgnoreCase)))
                    {
                        HyperVReplicaAzureEnableProtectionInput providerSettings = new HyperVReplicaAzureEnableProtectionInput();
                        providerSettings.HvHostVmId = this.ProtectionEntity.FabricObjectId;
                        providerSettings.VmName     = this.ProtectionEntity.FriendlyName;

                        // Id disk details are missing in input PE object, get the latest PE.
                        if (string.IsNullOrEmpty(this.ProtectionEntity.OS))
                        {
                            // Just checked for OS to see whether the disk details got filled up or not
                            ProtectableItemResponse protectableItemResponse =
                                RecoveryServicesClient.GetAzureSiteRecoveryProtectableItem(
                                    Utilities.GetValueFromArmId(this.ProtectionEntity.ID, ARMResourceTypeConstants.ReplicationFabrics),
                                    this.ProtectionEntity.ProtectionContainerId,
                                    this.ProtectionEntity.Name);

                            this.ProtectionEntity = new ASRProtectionEntity(protectableItemResponse.ProtectableItem);
                        }

                        if (string.IsNullOrWhiteSpace(this.OS))
                        {
                            providerSettings.OSType = ((string.Compare(this.ProtectionEntity.OS, Constants.OSWindows) == 0) || (string.Compare(this.ProtectionEntity.OS, Constants.OSLinux) == 0)) ? this.ProtectionEntity.OS : Constants.OSWindows;
                        }
                        else
                        {
                            providerSettings.OSType = this.OS;
                        }

                        if (string.IsNullOrWhiteSpace(this.OSDiskName))
                        {
                            providerSettings.VhdId = this.ProtectionEntity.OSDiskId;
                        }
                        else
                        {
                            foreach (var disk in this.ProtectionEntity.Disks)
                            {
                                if (0 == string.Compare(disk.Name, this.OSDiskName, true))
                                {
                                    providerSettings.VhdId = disk.Id;
                                    break;
                                }
                            }
                        }

                        if (RecoveryAzureStorageAccountId != null)
                        {
                            providerSettings.TargetStorageAccountId = RecoveryAzureStorageAccountId;
                        }

                        input.Properties.ProviderSpecificDetails = providerSettings;
                    }
                    else if (this.Policy != null &&
                             0 == string.Compare(this.Policy.ReplicationProvider, Constants.HyperVReplicaAzure, StringComparison.OrdinalIgnoreCase) &&
                             0 == string.Compare(this.ParameterSetName, ASRParameterSets.EnterpriseToEnterprise, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new PSArgumentException(Properties.Resources.PassingStorageMandatoryForEnablingDRInAzureScenarios);
                    }

                    this.response =
                        RecoveryServicesClient.EnableProtection(
                            Utilities.GetValueFromArmId(this.ProtectionEntity.ID, ARMResourceTypeConstants.ReplicationFabrics),
                            Utilities.GetValueFromArmId(this.ProtectionEntity.ID, ARMResourceTypeConstants.ReplicationProtectionContainers),
                            this.ProtectionEntity.Name,
                            input);
                }
                else
                {
                    // fetch the latest PE object
                    ProtectableItemResponse protectableItemResponse =
                        RecoveryServicesClient.GetAzureSiteRecoveryProtectableItem(Utilities.GetValueFromArmId(this.ProtectionEntity.ID, ARMResourceTypeConstants.ReplicationFabrics),
                                                                                   this.ProtectionEntity.ProtectionContainerId, this.ProtectionEntity.Name);
                    ProtectableItem protectableItem = protectableItemResponse.ProtectableItem;

                    if (!this.Force.IsPresent)
                    {
                        DisableProtectionInput input = new DisableProtectionInput();
                        input.Properties             = new DisableProtectionInputProperties()
                        {
                            ProviderSettings = new DisableProtectionProviderSpecificInput()
                        };

                        this.response =
                            RecoveryServicesClient.DisableProtection(
                                Utilities.GetValueFromArmId(this.ProtectionEntity.ID, ARMResourceTypeConstants.ReplicationFabrics),
                                Utilities.GetValueFromArmId(this.ProtectionEntity.ID, ARMResourceTypeConstants.ReplicationProtectionContainers),
                                Utilities.GetValueFromArmId(protectableItem.Properties.ReplicationProtectedItemId, ARMResourceTypeConstants.ReplicationProtectedItems),
                                input);
                    }
                    else
                    {
                        this.response =
                            RecoveryServicesClient.PurgeProtection(
                                Utilities.GetValueFromArmId(this.ProtectionEntity.ID, ARMResourceTypeConstants.ReplicationFabrics),
                                Utilities.GetValueFromArmId(this.ProtectionEntity.ID, ARMResourceTypeConstants.ReplicationProtectionContainers),
                                Utilities.GetValueFromArmId(protectableItem.Properties.ReplicationProtectedItemId, ARMResourceTypeConstants.ReplicationProtectedItems)
                                );
                    }
                }

                jobResponse =
                    RecoveryServicesClient
                    .GetAzureSiteRecoveryJobDetails(PSRecoveryServicesClient.GetJobIdFromReponseLocation(response.Location));

                WriteObject(new ASRJob(jobResponse.Job));

                if (this.WaitForCompletion.IsPresent)
                {
                    this.WaitForJobCompletion(this.jobResponse.Job.Name);

                    jobResponse =
                        RecoveryServicesClient
                        .GetAzureSiteRecoveryJobDetails(PSRecoveryServicesClient.GetJobIdFromReponseLocation(response.Location));

                    WriteObject(new ASRJob(jobResponse.Job));
                }
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// ProcessRecord of the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            this.targetNameOrId = this.ProtectionEntity.FriendlyName;
            this.ConfirmAction(
                this.Force.IsPresent || 0 != string.CompareOrdinal(this.Protection, Constants.DisableProtection),
                string.Format(Properties.Resources.DisableProtectionWarning, this.targetNameOrId),
                string.Format(Properties.Resources.DisableProtectionWhatIfMessage, this.Protection),
                this.targetNameOrId,
                () =>
            {
                try
                {
                    if (this.Protection == Constants.EnableProtection)
                    {
                        EnableProtectionInput input = new EnableProtectionInput();

                        // Considering no PP input for E2E, if needed, change below logic
                        // for E2E, consider the profile created PS & a profile created in service
                        if (this.ProtectionProfile != null &&
                            0 == string.Compare(
                                this.ProtectionProfile.ReplicationProvider,
                                Constants.HyperVReplicaAzure,
                                StringComparison.OrdinalIgnoreCase))
                        {
                            input.ProtectionProfileId = this.ProtectionProfile.Name;
                            AzureEnableProtectionInput providerSettings = new AzureEnableProtectionInput();
                            providerSettings.HvHostVmId = this.ProtectionEntity.FabricObjectId;
                            providerSettings.VmName     = this.ProtectionEntity.FriendlyName;

                            // Id disk details are missing in input PE object, get the latest PE.
                            // As get PE by name is failing before protection, get all & filter.
                            // Once after we fix get pe by name, change the logic to use the same.
                            if (string.IsNullOrEmpty(this.ProtectionEntity.OS))
                            {
                                // Just checked for OS to see whether the disk details got filled up or not
                                ProtectionEntityListResponse protectionEntityListResponse =
                                    RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity(
                                        this.ProtectionEntity.ProtectionContainerId);

                                foreach (ProtectionEntity pe in protectionEntityListResponse.ProtectionEntities)
                                {
                                    if (0 == string.Compare(this.ProtectionEntity.FriendlyName, pe.Properties.FriendlyName, true))
                                    {
                                        this.ProtectionEntity = new ASRProtectionEntity(pe);
                                        break;
                                    }
                                }
                            }
                            providerSettings.OSType = this.ProtectionEntity.OS;

                            if (string.IsNullOrWhiteSpace(this.OSDiskName))
                            {
                                providerSettings.VHDId = this.ProtectionEntity.OSDiskId;
                            }
                            else
                            {
                                foreach (var disk in this.ProtectionEntity.Disks)
                                {
                                    if (0 == string.Compare(disk.Name, this.OSDiskName, true))
                                    {
                                        providerSettings.VHDId = disk.Id;
                                        break;
                                    }
                                }
                            }

                            input.ProviderSettings = providerSettings;
                        }

                        this.response =
                            RecoveryServicesClient.EnableProtection(
                                this.ProtectionEntity.ProtectionContainerId,
                                this.ProtectionEntity.Name,
                                input);
                    }
                    else
                    {
                        DisableProtectionInput input = new DisableProtectionInput();

                        this.response =
                            RecoveryServicesClient.DisableProtection(
                                this.ProtectionEntity.ProtectionContainerId,
                                this.ProtectionEntity.Name,
                                input);
                    }

                    jobResponse =
                        RecoveryServicesClient
                        .GetAzureSiteRecoveryJobDetails(PSRecoveryServicesClient.GetJobIdFromReponseLocation(response.Location));

                    WriteObject(new ASRJob(jobResponse.Job));

                    if (this.WaitForCompletion.IsPresent)
                    {
                        this.WaitForJobCompletion(this.jobResponse.Job.Name);
                    }
                }
                catch (Exception exception)
                {
                    this.HandleException(exception);
                }
            });
        }
        /// <summary>
        /// ProcessRecord of the command.
        /// </summary>
        public override void ExecuteSiteRecoveryCmdlet()
        {
            base.ExecuteSiteRecoveryCmdlet();

            var policy = RecoveryServicesClient.GetAzureSiteRecoveryPolicy(
                Utilities.GetValueFromArmId(this.ProtectionContainerMapping.PolicyId, ARMResourceTypeConstants.Policies)).Policy;
            var policyInstanceType = policy.Properties.ProviderSpecificDetails.InstanceType;

            switch (this.ParameterSetName)
            {
            case ASRParameterSets.EnterpriseToEnterprise:
                if (policyInstanceType != Constants.HyperVReplica2012 &&
                    policyInstanceType != Constants.HyperVReplica2012R2)
                {
                    throw new PSArgumentException(
                              string.Format(
                                  Properties.Resources.ContainerMappingParameterSetMismatch,
                                  this.ProtectionContainerMapping.Name,
                                  policyInstanceType));
                }
                break;

            case ASRParameterSets.EnterpriseToAzure:
            case ASRParameterSets.HyperVSiteToAzure:
                if (policyInstanceType != Constants.HyperVReplicaAzure)
                {
                    throw new PSArgumentException(
                              string.Format(
                                  Properties.Resources.ContainerMappingParameterSetMismatch,
                                  this.ProtectionContainerMapping.Name,
                                  policyInstanceType));
                }
                break;

            default:
                break;
            }

            EnableProtectionProviderSpecificInput enableProtectionProviderSpecificInput = new EnableProtectionProviderSpecificInput();
            EnableProtectionInputProperties       inputProperties = new EnableProtectionInputProperties()
            {
                PolicyId                = this.ProtectionContainerMapping.PolicyId,
                ProtectableItemId       = this.ProtectableItem.ID,
                ProviderSpecificDetails = enableProtectionProviderSpecificInput
            };

            EnableProtectionInput input = new EnableProtectionInput()
            {
                Properties = inputProperties
            };

            // E2A and B2A.
            if (0 == string.Compare(this.ParameterSetName, ASRParameterSets.EnterpriseToAzure, StringComparison.OrdinalIgnoreCase) ||
                0 == string.Compare(this.ParameterSetName, ASRParameterSets.HyperVSiteToAzure, StringComparison.OrdinalIgnoreCase))
            {
                HyperVReplicaAzureEnableProtectionInput providerSettings = new HyperVReplicaAzureEnableProtectionInput();
                providerSettings.HvHostVmId = this.ProtectableItem.FabricObjectId;
                providerSettings.VmName     = this.ProtectableItem.FriendlyName;

                // Id disk details are missing in input PE object, get the latest PE.
                if (string.IsNullOrEmpty(this.ProtectableItem.OS))
                {
                    // Just checked for OS to see whether the disk details got filled up or not
                    ProtectableItemResponse protectableItemResponse =
                        RecoveryServicesClient.GetAzureSiteRecoveryProtectableItem(
                            Utilities.GetValueFromArmId(this.ProtectableItem.ID, ARMResourceTypeConstants.ReplicationFabrics),
                            this.ProtectableItem.ProtectionContainerId,
                            this.ProtectableItem.Name);

                    this.ProtectableItem = new ASRProtectableItem(protectableItemResponse.ProtectableItem);
                }

                if (string.IsNullOrWhiteSpace(this.OS))
                {
                    providerSettings.OSType = ((string.Compare(this.ProtectableItem.OS, Constants.OSWindows, StringComparison.OrdinalIgnoreCase) == 0) ||
                                               (string.Compare(this.ProtectableItem.OS, Constants.OSLinux) == 0)) ? this.ProtectableItem.OS : Constants.OSWindows;
                }
                else
                {
                    providerSettings.OSType = this.OS;
                }

                if (string.IsNullOrWhiteSpace(this.OSDiskName))
                {
                    providerSettings.VhdId = this.ProtectableItem.OSDiskId;
                }
                else
                {
                    foreach (var disk in this.ProtectableItem.Disks)
                    {
                        if (0 == string.Compare(disk.Name, this.OSDiskName, true))
                        {
                            providerSettings.VhdId = disk.Id;
                            break;
                        }
                    }
                }

                if (RecoveryAzureStorageAccountId != null)
                {
                    providerSettings.TargetStorageAccountId = RecoveryAzureStorageAccountId;
                }

                input.Properties.ProviderSpecificDetails = providerSettings;
            }

            this.response =
                RecoveryServicesClient.EnableProtection(
                    Utilities.GetValueFromArmId(this.ProtectableItem.ID, ARMResourceTypeConstants.ReplicationFabrics),
                    Utilities.GetValueFromArmId(this.ProtectableItem.ID, ARMResourceTypeConstants.ReplicationProtectionContainers),
                    Name,
                    input);

            jobResponse =
                RecoveryServicesClient
                .GetAzureSiteRecoveryJobDetails(PSRecoveryServicesClient.GetJobIdFromReponseLocation(response.Location));

            WriteObject(new ASRJob(jobResponse.Job));

            if (this.WaitForCompletion.IsPresent)
            {
                this.WaitForJobCompletion(this.jobResponse.Job.Name);

                jobResponse =
                    RecoveryServicesClient
                    .GetAzureSiteRecoveryJobDetails(PSRecoveryServicesClient.GetJobIdFromReponseLocation(response.Location));

                WriteObject(new ASRJob(jobResponse.Job));
            }
        }
        /// <summary>
        /// ProcessRecord of the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            switch (this.ParameterSetName)
            {
            case ASRParameterSets.ByPEObject:
                this.Id = this.ProtectionEntity.ID;
                this.ProtectionContainerId = this.ProtectionEntity.ProtectionContainerId;
                this.targetNameOrId        = this.ProtectionEntity.Name;
                this.alreadyEnabled        = this.ProtectionEntity.Protected;

                break;

            case ASRParameterSets.ByIDs:
                this.targetNameOrId = this.Id;
                ProtectionEntityResponse protectionEntityResponse =
                    RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity(
                        this.ProtectionContainerId,
                        this.Id);
                this.alreadyEnabled = protectionEntityResponse.ProtectionEntity.Protected;
                this.targetNameOrId = protectionEntityResponse.ProtectionEntity.Name;

                break;
            }

            if (this.alreadyEnabled &&
                this.Protection.Equals(Constants.EnableProtection, StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException(
                          string.Format(
                              Properties.Resources.ProtectionEntityAlreadyEnabled,
                              this.targetNameOrId));
            }
            else if (!this.alreadyEnabled &&
                     this.Protection.Equals(Constants.DisableProtection, StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException(
                          Properties.Resources.ProtectionEntityAlreadyDisabled,
                          this.targetNameOrId);
            }

            this.ConfirmAction(
                this.Force.IsPresent || 0 != string.CompareOrdinal(this.Protection, Constants.DisableProtection),
                string.Format(Properties.Resources.DisableProtectionWarning, this.targetNameOrId),
                string.Format(Properties.Resources.DisableProtectionWhatIfMessage, this.Protection),
                this.targetNameOrId,
                () =>
            {
                try
                {
                    if (this.Protection == Constants.EnableProtection)
                    {
                        string profileId = string.Empty;
                        var input        = new EnableProtectionInput();

                        if (this.ProtectionEntity == null)
                        {
                            var pe = RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity(
                                this.ProtectionContainerId,
                                this.Id);
                            this.ProtectionEntity = new ASRProtectionEntity(pe.ProtectionEntity);
                        }

                        // Get the replciation provider from profile object otherwise assume its E2E.
                        // Let the call go without profileId set.
                        string replicationProvider = null;

                        if (this.ProtectionProfile != null)
                        {
                            profileId           = this.ProtectionProfile.ID;
                            replicationProvider = this.ProtectionProfile.ReplicationProvider;
                        }
                        else
                        {
                            string pcId = this.ProtectionContainerId ?? this.ProtectionEntity.ProtectionContainerId;
                            var pc      = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainer(
                                pcId);

                            // PC will have all profiles associated with same replciation providers only.
                            replicationProvider =
                                pc.ProtectionContainer.AvailableProtectionProfiles.Count < 1 ?
                                null :
                                pc.ProtectionContainer.AvailableProtectionProfiles[0].ReplicationProvider;

                            if (replicationProvider != Constants.HyperVReplica)
                            {
                                throw new Exception("Please provide the protection profile object. It can be chosen from available protection profiles of the protection container.");
                            }
                        }

                        if (this.ParameterSetName == ASRParameterSets.ByIDs)
                        {
                            this.ValidateUsageById(replicationProvider, "Id");
                        }

                        if (replicationProvider == Constants.HyperVReplicaAzure)
                        {
                            input.ProtectionProfileId             = this.ProtectionProfile.ID;
                            AzureEnableProtectionInput azureInput = new AzureEnableProtectionInput();
                            azureInput.HvHostVmId = this.ProtectionEntity.FabricObjectId;
                            azureInput.VmName     = this.ProtectionEntity.Name;

                            azureInput.OSType = this.OS;
                            if (string.IsNullOrWhiteSpace(this.OS))
                            {
                                azureInput.OSType = this.ProtectionEntity.OS;
                            }

                            if (string.IsNullOrWhiteSpace(this.OSDiskName))
                            {
                                azureInput.VHDId = this.ProtectionEntity.OSDiskId;
                            }
                            else
                            {
                                foreach (var disk in this.ProtectionEntity.Disks)
                                {
                                    if (disk.Name == this.OSDiskName)
                                    {
                                        azureInput.VHDId = disk.Id;
                                        break;
                                    }
                                }
                            }

                            input.ReplicationProviderInput = DataContractUtils.Serialize <AzureEnableProtectionInput>(azureInput);
                        }
                        else if (string.IsNullOrWhiteSpace(profileId))
                        {
                            input = null;
                        }
                        else
                        {
                            input.ReplicationProviderInput = string.Empty;
                            input.ProtectionProfileId      = profileId;
                        }

                        this.jobResponse =
                            RecoveryServicesClient.EnableProtection(
                                this.ProtectionContainerId,
                                this.Id,
                                input);
                    }
                    else
                    {
                        this.jobResponse =
                            RecoveryServicesClient.DisbleProtection(
                                this.ProtectionContainerId,
                                this.Id);
                    }

                    this.WriteJob(this.jobResponse.Job);

                    if (this.WaitForCompletion.IsPresent)
                    {
                        this.WaitForJobCompletion(this.jobResponse.Job.ID);
                    }
                }
                catch (Exception exception)
                {
                    this.HandleException(exception);
                }
            });
        }