/// <summary>
        /// Write Protection Entity
        /// </summary>
        /// <param name="protectableItem"></param>
        private void WriteProtectionEntity(ProtectableItem protectableItem)
        {
            ReplicationProtectedItemResponse replicationProtectedItemResponse = null;

            if (!String.IsNullOrEmpty(protectableItem.Properties.ReplicationProtectedItemId))
            {
                replicationProtectedItemResponse = RecoveryServicesClient.GetAzureSiteRecoveryReplicationProtectedItem(
                    Utilities.GetValueFromArmId(this.ProtectionContainer.ID, ARMResourceTypeConstants.ReplicationFabrics),
                    this.ProtectionContainer.Name,
                    Utilities.GetValueFromArmId(protectableItem.Properties.ReplicationProtectedItemId, ARMResourceTypeConstants.ReplicationProtectedItems));
            }

            if (replicationProtectedItemResponse != null && replicationProtectedItemResponse.ReplicationProtectedItem != null)
            {
                PolicyResponse policyResponse = RecoveryServicesClient.GetAzureSiteRecoveryPolicy(Utilities.GetValueFromArmId(replicationProtectedItemResponse.ReplicationProtectedItem.Properties.PolicyID, ARMResourceTypeConstants.ReplicationPolicies));
                this.WriteObject(new ASRVirtualMachine(protectableItem, replicationProtectedItemResponse.ReplicationProtectedItem, policyResponse.Policy));
            }
            else
            {
                this.WriteObject(new ASRVirtualMachine(protectableItem));
            }
        }
        /// <summary>
        /// Helper to configure cloud
        /// </summary>
        private void Dissociate(string targetProtectionContainerId)
        {
            RemoveProtectionContainerMappingInputProperties inputProperties = new RemoveProtectionContainerMappingInputProperties()
            {
                ProviderSpecificInput = new ReplicationProviderContainerUnmappingInput()
            };

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

            ProtectionContainerMappingListResponse protectionContainerMappingListResponse = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainerMapping(Utilities.GetValueFromArmId(PrimaryProtectionContainer.ID, ARMResourceTypeConstants.ReplicationFabrics), PrimaryProtectionContainer.Name);
            ProtectionContainerMapping             protectionContainerMapping             = protectionContainerMappingListResponse.ProtectionContainerMappings.SingleOrDefault(t => (t.Properties.PolicyId.CompareTo(this.Policy.ID) == 0 && t.Properties.TargetProtectionContainerId.CompareTo(targetProtectionContainerId) == 0));

            if (protectionContainerMapping == null)
            {
                throw new Exception("Cloud is not paired");
            }

            LongRunningOperationResponse response = RecoveryServicesClient.UnConfigureProtection(Utilities.GetValueFromArmId(this.PrimaryProtectionContainer.ID, ARMResourceTypeConstants.ReplicationFabrics), this.PrimaryProtectionContainer.Name, protectionContainerMapping.Name, input);

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

            this.WriteObject(new ASRJob(jobResponse.Job));
        }
Пример #3
0
        /// <summary>
        /// Starts PE Unplanned failover.
        /// </summary>
        private void StartPEUnplannedFailover()
        {
            UnplannedFailoverInputProperties unplannedFailoverInputProperties = new UnplannedFailoverInputProperties()
            {
                FailoverDirection       = this.Direction,
                SourceSiteOperations    = this.PerformSourceSideActions ? "Required" : "NotRequired", //Required|NotRequired
                ProviderSpecificDetails = new ProviderSpecificFailoverInput()
            };

            UnplannedFailoverInput input = new UnplannedFailoverInput()
            {
                Properties = unplannedFailoverInputProperties
            };

            // fetch the latest PE object
            ProtectableItemResponse protectableItemResponse =
                RecoveryServicesClient.GetAzureSiteRecoveryProtectableItem(this.fabricName,
                                                                           this.ProtectionEntity.ProtectionContainerId, this.ProtectionEntity.Name);

            ReplicationProtectedItemResponse replicationProtectedItemResponse =
                RecoveryServicesClient.GetAzureSiteRecoveryReplicationProtectedItem(this.fabricName,
                                                                                    this.ProtectionEntity.ProtectionContainerId, Utilities.GetValueFromArmId(protectableItemResponse.ProtectableItem.Properties.ReplicationProtectedItemId, ARMResourceTypeConstants.ReplicationProtectedItems));

            PolicyResponse policyResponse = RecoveryServicesClient.GetAzureSiteRecoveryPolicy(Utilities.GetValueFromArmId(replicationProtectedItemResponse.ReplicationProtectedItem.Properties.PolicyID, ARMResourceTypeConstants.ReplicationPolicies));

            this.ProtectionEntity = new ASRProtectionEntity(protectableItemResponse.ProtectableItem, replicationProtectedItemResponse.ReplicationProtectedItem);

            if (0 == string.Compare(
                    this.ProtectionEntity.ReplicationProvider,
                    Constants.HyperVReplicaAzure,
                    StringComparison.OrdinalIgnoreCase))
            {
                if (this.Direction == Constants.PrimaryToRecovery)
                {
                    HyperVReplicaAzureFailoverProviderInput failoverInput = new HyperVReplicaAzureFailoverProviderInput()
                    {
                        PrimaryKekCertificatePfx   = null,
                        SecondaryKekCertificatePfx = null,
                        VaultLocation = this.GetCurrentValutLocation()
                    };
                    input.Properties.ProviderSpecificDetails = failoverInput;
                }
            }

            LongRunningOperationResponse response =
                RecoveryServicesClient.StartAzureSiteRecoveryUnplannedFailover(
                    this.fabricName,
                    this.protectionContainerName,
                    Utilities.GetValueFromArmId(replicationProtectedItemResponse.ReplicationProtectedItem.Id, ARMResourceTypeConstants.ReplicationProtectedItems),
                    input);

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

            WriteObject(new ASRJob(jobResponse.Job));
        }
        /// <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)
                    {
                        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
                    {
                        DisableProtectionInput input = new DisableProtectionInput();
                        input.Properties             = new DisableProtectionInputProperties()
                        {
                            ProviderSettings = new DisableProtectionProviderSpecificInput()
                        };

                        // fetch the latest PE object
                        ProtectableItemListResponse protectableItemListResponse =
                            RecoveryServicesClient.GetAzureSiteRecoveryProtectableItem(Utilities.GetValueFromArmId(this.ProtectionEntity.ID, ARMResourceTypeConstants.ReplicationFabrics),
                                                                                       this.ProtectionEntity.ProtectionContainerId);
                        ProtectableItem protectableItem = protectableItemListResponse.ProtectableItems.SingleOrDefault(t => t.Name.CompareTo(this.ProtectionEntity.Name) == 0);

                        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);
                    }

                    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));
                    }
                }
                catch (Exception exception)
                {
                    this.HandleException(exception);
                }
            });
        }
        /// <summary>
        /// ProcessRecord of the command.
        /// </summary>
        public override void ExecuteSiteRecoveryCmdlet()
        {
            base.ExecuteSiteRecoveryCmdlet();

            ProtectableItemResponse protectableItemResponse =
                RecoveryServicesClient.GetAzureSiteRecoveryProtectableItem(Utilities.GetValueFromArmId(this.VirtualMachine.ID, ARMResourceTypeConstants.ReplicationFabrics),
                                                                           this.VirtualMachine.ProtectionContainerId, this.VirtualMachine.Name);

            if (protectableItemResponse.ProtectableItem.Properties.ReplicationProtectedItemId == null)
            {
                this.WriteWarning(Properties.Resources.ProtectionIsNotEnabledForVM.ToString());
                return;
            }

            ReplicationProtectedItemResponse replicationProtectedItemResponse =
                RecoveryServicesClient.GetAzureSiteRecoveryReplicationProtectedItem(Utilities.GetValueFromArmId(this.VirtualMachine.ID, ARMResourceTypeConstants.ReplicationFabrics),
                                                                                    this.VirtualMachine.ProtectionContainerId, Utilities.GetValueFromArmId(protectableItemResponse.ProtectableItem.Properties.ReplicationProtectedItemId, ARMResourceTypeConstants.ReplicationProtectedItems));

            // Check for Replication Provider type HyperVReplicaAzure
            if (0 != string.Compare(
                    replicationProtectedItemResponse.ReplicationProtectedItem.Properties.ProviderSpecificDetails.InstanceType,
                    Constants.HyperVReplicaAzure,
                    StringComparison.OrdinalIgnoreCase))
            {
                this.WriteWarning(Properties.Resources.UnsupportedReplicationProvidedForUpdateVmProperties.ToString());
                return;
            }

            // Check for at least one option
            if (string.IsNullOrEmpty(this.Name) &&
                string.IsNullOrEmpty(this.Size) &&
                string.IsNullOrEmpty(this.PrimaryNic) &&
                string.IsNullOrEmpty(this.RecoveryNetworkId))
            {
                this.WriteWarning(Properties.Resources.ArgumentsMissingForUpdateVmProperties.ToString());
                return;
            }

            // Both primary & recovery inputs should be present
            if (string.IsNullOrEmpty(this.PrimaryNic) ^
                string.IsNullOrEmpty(this.RecoveryNetworkId))
            {
                this.WriteWarning(Properties.Resources.NetworkArgumentsMissingForUpdateVmProperties.ToString());
                return;
            }

            List <VMNicInputDetails> vMNicInputDetailsList = new List <VMNicInputDetails>();
            VMNicDetails             vMNicDetailsToBeUpdated;

            if (!string.IsNullOrEmpty(this.PrimaryNic))
            {
                HyperVReplicaAzureReplicationDetails providerSpecificDetails =
                    (HyperVReplicaAzureReplicationDetails)replicationProtectedItemResponse.ReplicationProtectedItem.Properties.ProviderSpecificDetails;

                if (providerSpecificDetails.VMNics != null)
                {
                    vMNicDetailsToBeUpdated = providerSpecificDetails.VMNics.SingleOrDefault(n => string.Compare(n.NicId, this.PrimaryNic, StringComparison.OrdinalIgnoreCase) == 0);
                    if (vMNicDetailsToBeUpdated != null)
                    {
                        VMNicInputDetails vMNicInputDetails = new VMNicInputDetails();

                        vMNicInputDetails.NicId = this.PrimaryNic;
                        vMNicInputDetails.RecoveryVMSubnetName      = this.RecoveryNicSubnetName;
                        vMNicInputDetails.ReplicaNicStaticIPAddress = this.RecoveryNicStaticIPAddress;
                        vMNicInputDetails.SelectionType             = string.IsNullOrEmpty(this.NicSelectionType) ? Constants.SelectedByUser : this.NicSelectionType;
                        vMNicInputDetailsList.Add(vMNicInputDetails);

                        IEnumerable <VMNicDetails> vMNicDetailsListRemaining = providerSpecificDetails.VMNics.Where(n => string.Compare(n.NicId, this.PrimaryNic, StringComparison.OrdinalIgnoreCase) != 0);
                        foreach (VMNicDetails nDetails in vMNicDetailsListRemaining)
                        {
                            vMNicInputDetails = new VMNicInputDetails();

                            vMNicInputDetails.NicId = nDetails.NicId;
                            vMNicInputDetails.RecoveryVMSubnetName      = nDetails.RecoveryVMSubnetName;
                            vMNicInputDetails.ReplicaNicStaticIPAddress = nDetails.ReplicaNicStaticIPAddress;
                            vMNicInputDetails.SelectionType             = nDetails.SelectionType;
                            vMNicInputDetailsList.Add(vMNicInputDetails);
                        }
                    }
                    else
                    {
                        throw new PSInvalidOperationException(Properties.Resources.NicNotFoundInVMForUpdateVmProperties);
                    }
                }
            }

            UpdateReplicationProtectedItemInputProperties updateReplicationProtectedItemInputProperties = new UpdateReplicationProtectedItemInputProperties()
            {
                RecoveryAzureVMName            = this.Name,
                RecoveryAzureVMSize            = this.Size,
                SelectedRecoveryAzureNetworkId = this.RecoveryNetworkId,
                VmNics = vMNicInputDetailsList
            };

            UpdateReplicationProtectedItemInput input = new UpdateReplicationProtectedItemInput()
            {
                Properties = updateReplicationProtectedItemInputProperties
            };

            LongRunningOperationResponse response = RecoveryServicesClient.UpdateVmProperties(
                Utilities.GetValueFromArmId(this.VirtualMachine.ID, ARMResourceTypeConstants.ReplicationFabrics),
                Utilities.GetValueFromArmId(this.VirtualMachine.ID, ARMResourceTypeConstants.ReplicationProtectionContainers),
                replicationProtectedItemResponse.ReplicationProtectedItem.Name,
                input);

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

            WriteObject(new ASRJob(jobResponse.Job));
        }
        /// <summary>
        /// Helper to configure cloud
        /// </summary>
        private void Associate(string targetProtectionContainerId)
        {
            CreateProtectionContainerMappingInputProperties inputProperties = new CreateProtectionContainerMappingInputProperties()
            {
                PolicyId = this.Policy.ID,
                ProviderSpecificInput       = new ReplicationProviderContainerMappingInput(),
                TargetProtectionContainerId = targetProtectionContainerId
            };

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

            string mappingName = "ContainerMapping_" + Guid.NewGuid().ToString();
            LongRunningOperationResponse response = RecoveryServicesClient.ConfigureProtection(Utilities.GetValueFromArmId(this.PrimaryProtectionContainer.ID, ARMResourceTypeConstants.ReplicationFabrics), this.PrimaryProtectionContainer.Name, mappingName, input);

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

            this.WriteObject(new ASRJob(jobResponse.Job));
        }