RemoveAuthorizationEntry(
            string primaryHostSystem)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            //
            // Call the Msvm_ReplicationService::RemoveAuthorizationEntry method.
            //
            using (ManagementObject replicationService =
                       ReplicaUtilities.GetVirtualMachineReplicationService(scope))
            {
                using (ManagementBaseObject inParams =
                           replicationService.GetMethodParameters("RemoveAuthorizationEntry"))
                {
                    inParams["AllowedPrimaryHostSystem"] = primaryHostSystem;

                    using (ManagementBaseObject outParams =
                               replicationService.InvokeMethod("RemoveAuthorizationEntry", inParams, null))
                    {
                        WmiUtilities.ValidateOutput(outParams, scope);
                    }

                    Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                    "Replication authorization entry removed for \"{0}\".", primaryHostSystem));
                }
            }
        }
        RemoveReplicationRelationshipEx(
            string name,
            UInt16 relationshipType)
        {
            if (relationshipType > 1)
            {
                throw new ArgumentException("Replication relationship should be either 0 or 1");
            }

            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            //
            // Retrieve the Msvm_ComputerSystem.
            //
            using (ManagementObject vm = WmiUtilities.GetVirtualMachine(name, scope))
            {
                string vmPath = vm.Path.Path;

                //
                // Retrieve the specified Msvm_ReplicationRelationship object.
                //
                using (ManagementObject replicationRelationship =
                           ReplicaUtilities.GetReplicationRelationshipObject(vm, relationshipType))
                {
                    if (replicationRelationship == null)
                    {
                        throw new ManagementException(string.Format(CultureInfo.CurrentCulture,
                                                                    "No Msvm_ReplicationRelationship object with relationship type {0} could be found",
                                                                    relationshipType));
                    }

                    string replicationRelationshipEmbedded =
                        replicationRelationship.GetText(TextFormat.WmiDtd20);

                    using (ManagementObject replicationService =
                               ReplicaUtilities.GetVirtualMachineReplicationService(scope))
                    {
                        using (ManagementBaseObject inParams =
                                   replicationService.GetMethodParameters("RemoveReplicationRelationshipEx"))
                        {
                            inParams["ComputerSystem"]          = vmPath;
                            inParams["ReplicationRelationship"] = replicationRelationshipEmbedded;

                            using (ManagementBaseObject outParams =
                                       replicationService.InvokeMethod("RemoveReplicationRelationshipEx",
                                                                       inParams,
                                                                       null))
                            {
                                WmiUtilities.ValidateOutput(outParams, scope);
                            }
                        }
                    }
                }

                Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                "{0} replication is successfully removed for virtual machine \"{1}\"",
                                                relationshipType == 0 ? "Primary" : "Extended",
                                                name));
            }
        }
        CreateReplicationRelationship(
            string name,
            string recoveryServerName)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            //
            // Retrieve the Msvm_ComputerSystem.
            //
            using (ManagementObject vm = WmiUtilities.GetVirtualMachine(name, scope))
            {
                string vmPath = vm.Path.Path;

                using (ManagementObject replicationSettingData =
                           ReplicaUtilities.GetReplicationSettings(vm))
                {
                    replicationSettingData["RecoveryConnectionPoint"]  = recoveryServerName;
                    replicationSettingData["AuthenticationType"]       = 1;
                    replicationSettingData["RecoveryServerPortNumber"] = 80;
                    replicationSettingData["CompressionEnabled"]       = 1;

                    // Keep 24 recovery points.
                    replicationSettingData["RecoveryHistory"] = 24;

                    // Replicate changes after every 300 seconds.
                    replicationSettingData["ReplicationInterval"] = 300;

                    // Take VSS snapshot every one hour.
                    replicationSettingData["ApplicationConsistentSnapshotInterval"] = 1;

                    // Include all disks for replication.
                    replicationSettingData["IncludedDisks"] = WmiUtilities.GetVhdSettings(vm);

                    string settingDataEmbedded =
                        replicationSettingData.GetText(TextFormat.WmiDtd20);

                    using (ManagementObject replicationService =
                               ReplicaUtilities.GetVirtualMachineReplicationService(scope))
                    {
                        using (ManagementBaseObject inParams =
                                   replicationService.GetMethodParameters("CreateReplicationRelationship"))
                        {
                            inParams["ComputerSystem"]         = vmPath;
                            inParams["ReplicationSettingData"] = settingDataEmbedded;

                            using (ManagementBaseObject outParams =
                                       replicationService.InvokeMethod("CreateReplicationRelationship",
                                                                       inParams,
                                                                       null))
                            {
                                WmiUtilities.ValidateOutput(outParams, scope);
                            }
                        }
                    }

                    Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                    "Replication is successfully enabled for virtual machine \"{0}\"", name));
                }
            }
        }
Пример #4
0
        RemoveReplicationRelationship(
            string name)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            //
            // Retrieve the Msvm_ComputerSystem.
            //
            using (ManagementObject vm = WmiUtilities.GetVirtualMachine(name, scope))
            {
                string vmPath = vm.Path.Path;

                using (ManagementObject replicationService =
                           ReplicaUtilities.GetVirtualMachineReplicationService(scope))
                {
                    using (ManagementBaseObject inParams =
                               replicationService.GetMethodParameters("RemoveReplicationRelationship"))
                    {
                        inParams["ComputerSystem"] = vmPath;

                        using (ManagementBaseObject outParams =
                                   replicationService.InvokeMethod("RemoveReplicationRelationship",
                                                                   inParams,
                                                                   null))
                        {
                            WmiUtilities.ValidateOutput(outParams, scope);
                        }
                    }
                }

                Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                "Replication is successfully removed for virtual machine \"{0}\"", name));
            }
        }
        AddAuthorizationEntry(
            string primaryHostSystem,
            string trustGroup,
            string replicaStoragePath)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            //
            // Call the Msvm_ReplicationService::AddAuthorizationEntry method. Note that the
            // Msvm_ReplicationAuthorizationSettingData instance must be passed as an embedded instance.
            //
            using (ManagementObject replicationService =
                       ReplicaUtilities.GetVirtualMachineReplicationService(scope))
            {
                //
                // Create an instance of the Msvm_ReplicationAuthorizationSettingData.
                //
                string authSettingDataEmbedded;

                using (ManagementClass authSettingDataClass = new ManagementClass(
                           "Msvm_ReplicationAuthorizationSettingData"))
                {
                    authSettingDataClass.Scope = scope;

                    using (ManagementObject authSettingData = authSettingDataClass.CreateInstance())
                    {
                        authSettingData["AllowedPrimaryHostSystem"] = primaryHostSystem;
                        authSettingData["TrustGroup"]             = trustGroup;
                        authSettingData["ReplicaStorageLocation"] = replicaStoragePath;

                        authSettingDataEmbedded = authSettingData.GetText(TextFormat.WmiDtd20);

                        using (ManagementBaseObject inParams =
                                   replicationService.GetMethodParameters("AddAuthorizationEntry"))
                        {
                            inParams["AuthorizationEntry"] = authSettingDataEmbedded;

                            using (ManagementBaseObject outParams =
                                       replicationService.InvokeMethod("AddAuthorizationEntry", inParams, null))
                            {
                                WmiUtilities.ValidateOutput(outParams, scope);
                            }

                            Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                            "Replication is authorized for \"{0}\".", primaryHostSystem));
                        }
                    }
                }
            }
        }
        ReverseReplicationRelationship(
            string name)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            //
            // Retrieve the Msvm_ComputerSystem.
            //
            using (ManagementObject vm = WmiUtilities.GetVirtualMachine(name, scope))
            {
                string vmPath = vm.Path.Path;

                using (ManagementObject replicationSettingData = ReplicaUtilities.GetReplicationSettings(vm))
                {
                    //
                    // Simply reverse the recovery server name with that of primary, other
                    // properties are already populated.
                    //
                    replicationSettingData["RecoveryConnectionPoint"] =
                        replicationSettingData["PrimaryConnectionPoint"];

                    string settingDataEmbedded = replicationSettingData.GetText(TextFormat.WmiDtd20);

                    using (ManagementObject replicationService =
                               ReplicaUtilities.GetVirtualMachineReplicationService(scope))
                    {
                        using (ManagementBaseObject inParams =
                                   replicationService.GetMethodParameters("ReverseReplicationRelationship"))
                        {
                            inParams["ComputerSystem"]         = vmPath;
                            inParams["ReplicationSettingData"] = settingDataEmbedded;

                            using (ManagementBaseObject outParams =
                                       replicationService.InvokeMethod("ReverseReplicationRelationship",
                                                                       inParams,
                                                                       null))
                            {
                                WmiUtilities.ValidateOutput(outParams, scope);
                            }
                        }
                    }

                    Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                    "Replication is successfully reversed for virtual machine \"{0}\"", name));
                }
            }
        }
        StartReplication(
            string name)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            //
            // Retrieve the Msvm_ComputerSystem.
            //
            using (ManagementObject vm = WmiUtilities.GetVirtualMachine(name, scope))
            {
                //
                // Call the Msvm_ReplicationService::StartReplication method.
                // Note the input parameters values are as below -
                // InitialReplicationType - 1 for transfer over network
                //                          2 for exporting the initial replication to the location specified
                //                            in InitialReplicationExportLocation parameter.
                //                          3 for replication with a restored copy on recovery.
                // InitialReplicationExportLocation - null or export location path when InitialReplicationType is 2.
                // StartTime - null or scheduled start time in UTC.
                //
                string vmPath = vm.Path.Path;

                using (ManagementObject replicationService =
                           ReplicaUtilities.GetVirtualMachineReplicationService(scope))
                {
                    using (ManagementBaseObject inParams =
                               replicationService.GetMethodParameters("StartReplication"))
                    {
                        inParams["ComputerSystem"]                   = vmPath;
                        inParams["InitialReplicationType"]           = 1;
                        inParams["InitialReplicationExportLocation"] = null;
                        inParams["StartTime"] = null;

                        using (ManagementBaseObject outParams =
                                   replicationService.InvokeMethod("StartReplication", inParams, null))
                        {
                            WmiUtilities.ValidateOutput(outParams, scope);
                        }
                    }
                }

                Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                "Replication is successfully started for virtual machine \"{0}\"", name));
            }
        }
        ModifyServiceSettings(
            bool enableReplicaServer)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");
            string          serviceSettingDataEmbedded;

            //
            // Call the Msvm_ReplicationService::ModifyServiceSettings method. Note that the
            // Msvm_ReplicationServiceSettingData instance must be passed as an embedded instance.
            //
            using (ManagementObject replicationService =
                       ReplicaUtilities.GetVirtualMachineReplicationService(scope))
            {
                //
                // Gets an instance of the Msvm_ReplicationServiceSettingData class and set the
                // RecoveryServerEnabled field. Note that this sample enables the replication server
                // for integrated authentication.
                //
                using (ManagementObject serviceSettingData =
                           ReplicaUtilities.GetReplicationServiceSettings(replicationService))
                {
                    serviceSettingData["RecoveryServerEnabled"]     = enableReplicaServer;
                    serviceSettingData["AllowedAuthenticationType"] = 1;
                    serviceSettingDataEmbedded = serviceSettingData.GetText(TextFormat.WmiDtd20);

                    using (ManagementBaseObject inParams =
                               replicationService.GetMethodParameters("ModifyServiceSettings"))
                    {
                        inParams["SettingData"] = serviceSettingDataEmbedded;

                        using (ManagementBaseObject outParams =
                                   replicationService.InvokeMethod("ModifyServiceSettings", inParams, null))
                        {
                            WmiUtilities.ValidateOutput(outParams, scope);
                        }

                        Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                        "The replication service was successfully {0}.",
                                                        enableReplicaServer ? "enabled" : "disabled"));
                    }
                }
            }
        }
        SetAuthorizationEntry(
            string name,
            string primaryHostSystem)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            //
            // Retrieve the Msvm_ComputerSystem.
            //
            using (ManagementObject vm = WmiUtilities.GetVirtualMachine(name, scope))
            {
                string vmPath = vm.Path.Path;

                using (ManagementObject authEntry =
                           ReplicaUtilities.GetAuthorizationEntry(primaryHostSystem))
                {
                    string authEntryEmbedded = authEntry.GetText(TextFormat.WmiDtd20);

                    using (ManagementObject replicationService =
                               ReplicaUtilities.GetVirtualMachineReplicationService(scope))
                    {
                        using (ManagementBaseObject inParams =
                                   replicationService.GetMethodParameters("SetAuthorizationEntry"))
                        {
                            inParams["ComputerSystem"]     = vmPath;
                            inParams["AuthorizationEntry"] = authEntryEmbedded;

                            using (ManagementBaseObject outParams =
                                       replicationService.InvokeMethod("SetAuthorizationEntry",
                                                                       inParams,
                                                                       null))
                            {
                                WmiUtilities.ValidateOutput(outParams, scope);
                            }
                        }
                    }

                    Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                    "Replication for virtual machine \"{0}\" is successfully associated with \"{1}\"",
                                                    name, primaryHostSystem));
                }
            }
        }
        InitiateFailover(
            string name)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            //
            // Retrieve the Msvm_ComputerSystem.
            //
            using (ManagementObject vm = WmiUtilities.GetVirtualMachine(name, scope))
            {
                //
                // Call the Msvm_ReplicationService::InitiateFailover method.
                // Note the input paramters values are as below -
                // SnapshotSettingData - null for latest recovery point.
                //                       OR Embedded instance of CIM_VirtualSystemSettingData
                //                       pointing to recovery snapshot.
                //
                string vmPath = vm.Path.Path;

                using (ManagementObject replicationService =
                           ReplicaUtilities.GetVirtualMachineReplicationService(scope))
                {
                    using (ManagementBaseObject inParams =
                               replicationService.GetMethodParameters("InitiateFailover"))
                    {
                        inParams["ComputerSystem"]      = vmPath;
                        inParams["SnapshotSettingData"] = null;

                        using (ManagementBaseObject outParams =
                                   replicationService.InvokeMethod("InitiateFailover", inParams, null))
                        {
                            WmiUtilities.ValidateOutput(outParams, scope);
                        }
                    }
                }

                Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                "Failover is successfully completed for virtual machine \"{0}\"", name));
            }
        }
        GetAuthorizationEntry(
            string primaryHostSystem)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            using (ManagementObject replicationService =
                       ReplicaUtilities.GetVirtualMachineReplicationService(scope))
            {
                ManagementObject serviceSetting = null;
                using (ManagementObjectCollection settingCollection =
                           replicationService.GetRelated("Msvm_ReplicationAuthorizationSettingData"))
                {
                    foreach (ManagementObject mgmtObj in settingCollection)
                    {
                        if (String.Equals(mgmtObj["AllowedPrimaryHostSystem"].ToString(),
                                          primaryHostSystem,
                                          StringComparison.OrdinalIgnoreCase))
                        {
                            serviceSetting = mgmtObj;
                            break;
                        }
                        else
                        {
                            mgmtObj.Dispose();
                        }
                    }

                    if (serviceSetting == null)
                    {
                        Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                        "Msvm_ReplicationAuthorizationSettingData not found for \"{0}\"",
                                                        primaryHostSystem));
                    }
                }

                return(serviceSetting);
            }
        }