/// <summary>
        /// ProcessRecord of the command.
        /// </summary>
        public override void ExecuteSiteRecoveryCmdlet()
        {
            base.ExecuteSiteRecoveryCmdlet();

            if (ShouldProcess(this.ReplicationProtectedItem.FriendlyName, VerbsCommon.Remove))
            {
                this.targetNameOrId = this.ReplicationProtectedItem.FriendlyName;

                if (!Force.IsPresent)
                {
                    DisableProtectionInput input = new DisableProtectionInput();
                    input.Properties = new DisableProtectionInputProperties()
                    {
                        ProviderSettings = new DisableProtectionProviderSpecificInput()
                    };
                    this.response =
                        RecoveryServicesClient.DisableProtection(
                        Utilities.GetValueFromArmId(this.ReplicationProtectedItem.ID, ARMResourceTypeConstants.ReplicationFabrics),
                        Utilities.GetValueFromArmId(this.ReplicationProtectedItem.ID, ARMResourceTypeConstants.ReplicationProtectionContainers),
                        ReplicationProtectedItem.Name,
                        input);
                }
                else
                {
                    this.response =
                        RecoveryServicesClient.PurgeProtection(
                        Utilities.GetValueFromArmId(this.ReplicationProtectedItem.ID, ARMResourceTypeConstants.ReplicationFabrics),
                        Utilities.GetValueFromArmId(this.ReplicationProtectedItem.ID, ARMResourceTypeConstants.ReplicationProtectionContainers),
                        ReplicationProtectedItem.Name);
                }

                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>
 /// Disable Protection for the given Replication protected item.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.SiteRecovery.IReplicationProtectedItemOperations.
 /// </param>
 /// <param name='fabricName'>
 /// Required. Fabric name.
 /// </param>
 /// <param name='protectionContainerName'>
 /// Required. Protection container name.
 /// </param>
 /// <param name='replicationProtectedItemName'>
 /// Required. Replication protected item unique name.
 /// </param>
 /// <param name='input'>
 /// Required. Disable protection input.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// A standard service response for long running operations.
 /// </returns>
 public static LongRunningOperationResponse DisableProtection(this IReplicationProtectedItemOperations operations, string fabricName, string protectionContainerName, string replicationProtectedItemName, DisableProtectionInput input, CustomRequestHeaders customRequestHeaders)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IReplicationProtectedItemOperations)s).DisableProtectionAsync(fabricName, protectionContainerName, replicationProtectedItemName, input, customRequestHeaders);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
 /// <summary>
 /// Disable Protection for the given Replication protected item.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.SiteRecovery.IReplicationProtectedItemOperations.
 /// </param>
 /// <param name='fabricName'>
 /// Required. Fabric name.
 /// </param>
 /// <param name='protectionContainerName'>
 /// Required. Protection container name.
 /// </param>
 /// <param name='replicationProtectedItemName'>
 /// Required. Replication protected item unique name.
 /// </param>
 /// <param name='input'>
 /// Required. Disable protection input.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// A standard service response for long running operations.
 /// </returns>
 public static Task<LongRunningOperationResponse> DisableProtectionAsync(this IReplicationProtectedItemOperations operations, string fabricName, string protectionContainerName, string replicationProtectedItemName, DisableProtectionInput input, CustomRequestHeaders customRequestHeaders)
 {
     return operations.DisableProtectionAsync(fabricName, protectionContainerName, replicationProtectedItemName, input, customRequestHeaders, CancellationToken.None);
 }
        /// <summary>
        /// ProcessRecord of the command.
        /// </summary>
        protected override void ProcessRecord()
        {
            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();

            this.WriteWarningWithTimestamp(
                string.Format(Properties.Resources.CmdletWillBeDeprecatedSoon,
                    this.MyInvocation.MyCommand.Name,
                    "New-AzureRmSiteRecoveryReplicationProtectedItem or Remove-AzureRmSiteRecoveryReplicationProtectedItem"));

            if (0 != string.CompareOrdinal(this.Protection, Constants.DisableProtection) || ShouldProcess(this.ProtectionEntity.FriendlyName, "Disable Protection"))
            {
                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));
                }
            }
        }
 /// <summary>
 /// Sets protection on Protection entity.
 /// </summary>
 /// <param name="protectionContainerId">Protection Container ID</param>
 /// <param name="protectionEntityId">Virtual Machine ID or Replication group Id</param>
 /// <param name="input">Disable protection input.</param>
 /// <returns>Job response</returns>
 public LongRunningOperationResponse DisableProtection(
     string protectionContainerId,
     string protectionEntityId,
     DisableProtectionInput input)
 {
     return this.GetSiteRecoveryClient().ProtectionEntity.DisableProtection(
         protectionContainerId,
         protectionEntityId,
         input,
         this.GetRequestHeaders());
 }
 /// <summary>
 /// Disable Protection for the given protection enity.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.SiteRecovery.IProtectionEntityOperations.
 /// </param>
 /// <param name='protectionContainerId'>
 /// Required. Parent Protection Container ID.
 /// </param>
 /// <param name='protectionEntityId'>
 /// Required. Protection entity ID.
 /// </param>
 /// <param name='input'>
 /// Optional. Protection entity ID.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// A standard service response for long running operations.
 /// </returns>
 public static Task<LongRunningOperationResponse> DisableProtectionAsync(this IProtectionEntityOperations operations, string protectionContainerId, string protectionEntityId, DisableProtectionInput input, CustomRequestHeaders customRequestHeaders)
 {
     return operations.DisableProtectionAsync(protectionContainerId, protectionEntityId, input, customRequestHeaders, CancellationToken.None);
 }
 /// <summary>
 /// Disable Protection for the given protection enity.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.SiteRecovery.IProtectionEntityOperations.
 /// </param>
 /// <param name='protectionContainerId'>
 /// Required. Parent Protection Container ID.
 /// </param>
 /// <param name='protectionEntityId'>
 /// Required. Protection entity ID.
 /// </param>
 /// <param name='input'>
 /// Optional. Protection entity ID.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// A standard service response for long running operations.
 /// </returns>
 public static LongRunningOperationResponse DisableProtection(this IProtectionEntityOperations operations, string protectionContainerId, string protectionEntityId, DisableProtectionInput input, CustomRequestHeaders customRequestHeaders)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IProtectionEntityOperations)s).DisableProtectionAsync(protectionContainerId, protectionEntityId, input, customRequestHeaders);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
 /// <summary>
 /// Removes Replicated Protected Item.
 /// </summary>
 /// <param name="protectionContainerName">Protection Container ID</param>
 /// <param name="replicationProtectedItemName">Virtual Machine ID or Replication group Id</param>
 /// <param name="input">Disable protection input.</param>
 /// <returns>Job response</returns>
 public LongRunningOperationResponse DisableProtection(string fabricName,
     string protectionContainerName,
     string replicationProtectedItemName,
     DisableProtectionInput input)
 {
     return this.GetSiteRecoveryClient().ReplicationProtectedItem.BeginDisableProtection(
         fabricName,
         protectionContainerName,
         replicationProtectedItemName,
         input,
         this.GetRequestHeaders());
 }
        /// <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);
                        }
                    });
        }