public static void DeleteReplicationConnection(string server, string connectionName) { try { DirectoryContext context = new DirectoryContext( DirectoryContextType.DirectoryServer, server); ReplicationConnection connection = ReplicationConnection.FindByName( context, connectionName); // delete the replication connection connection.Delete(); Console.WriteLine("\nReplication connection {0} deleted", connectionName); } catch (Exception e) { Console.WriteLine("\r\nUnexpected exception occured:\n{0}:{1}", e.GetType().Name, e.Message); } }
static void Main() { try { string targetDomainName = "fabrikam.com"; string targetServer = "server1.fabrikam.com"; string sourceServer = "server2.fabrikam.com"; string partitionName = "CN=Configuration,DC=fabrikam,DC=com"; // get to the domain controller DomainController dc = DomainController.FindOne( new DirectoryContext( DirectoryContextType.Domain, targetDomainName)); // to use alternate credentials use the below code // DomainController dc = DomainController.FindOne( // new DirectoryContext( // DirectoryContextType.Domain, // targetDomainName, // "alt-username", // "alt-password")); // // // invoke kcc to check replication consistency dc.CheckReplicationConsistency(); Console.WriteLine("\nCheck replication consistency succeed\n"); // sync replica from a source server dc.SyncReplicaFromServer(partitionName, sourceServer); Console.WriteLine("\nSynchronize naming context \"{0}\" " + "with server {1} succeed", partitionName, sourceServer); // sync replica from all neighbors dc.TriggerSyncReplicaFromNeighbors(partitionName); Console.WriteLine("\nSynchronize naming context \"{0}\" " + "with all neighbors succeed", partitionName); // sync replica from all servers dc.SyncFromAllServersCallback = SyncFromAllServersCallbackRoutine; Console.WriteLine("\nStart sync with all servers:"); dc.SyncReplicaFromAllServers(partitionName, SyncFromAllServersOptions.AbortIfServerUnavailable); Console.WriteLine("\nSynchronize naming context \"{0}\" " + "with all servers succeed", partitionName); // replication connection // create new replication connections DirectoryServer sourceDC = DomainController.GetDomainController( new DirectoryContext( DirectoryContextType.DirectoryServer, sourceServer)); // to use alternate credentials use the below code // DirectoryServer sourceDC = DomainController.GetDomainController( // new DirectoryContext( // DirectoryContextType.DirectoryServer, // sourceServer, // "alt-username", // "alt-password")); // ReplicationConnection connection = new ReplicationConnection( new DirectoryContext( DirectoryContextType.DirectoryServer, targetServer), "myconnection", sourceDC); // to use alternate credentials use the below code // ReplicationConnection connection = // new ReplicationConnection( // new DirectoryContext( // DirectoryContextType.DirectoryServer, // targetServer, // "alt-username", // "alt-password"), // "myconnection", // sourceDC); // // set change notification status connection.ChangeNotificationStatus = NotificationStatus.IntraSiteOnly; // create customized replication schedule ActiveDirectorySchedule schedule = new ActiveDirectorySchedule(); schedule.SetDailySchedule(HourOfDay.Twelve, MinuteOfHour.Zero, HourOfDay.Fifteen, MinuteOfHour.Zero); schedule.SetSchedule(DayOfWeek.Sunday, HourOfDay.Eight, MinuteOfHour.Zero, HourOfDay.Eleven, MinuteOfHour.Zero); schedule.SetSchedule(DayOfWeek.Saturday, HourOfDay.Seven, MinuteOfHour.Zero, HourOfDay.Ten, MinuteOfHour.Zero); connection.ReplicationSchedule = schedule; connection.ReplicationScheduleOwnedByUser = true; connection.Save(); Console.WriteLine("\nNew replication connection is created successfully"); connection = ReplicationConnection.FindByName( new DirectoryContext( DirectoryContextType.DirectoryServer, targetServer), "myconnection"); // to use alternate credentials use the below code // connection = ReplicationConnection.FindByName( // new DirectoryContext( // DirectoryContextType.DirectoryServer, // targetServer, // "alt-username", // "alt-password"), // "myconnection"); // Console.WriteLine("\nGet replication connection \"{0}\" information:", connection.Name); Console.WriteLine("ChangeNotificationStatus is {0}", connection.ChangeNotificationStatus); Console.WriteLine("ReplicationSpan is {0}", connection.ReplicationSpan); Console.WriteLine("ReplicationScheduleOwnedByUser is {0}", connection.ReplicationScheduleOwnedByUser); // delete the replication connection connection.Delete(); Console.WriteLine("\nReplication connection is deleted\n"); } catch (Exception e) { Console.WriteLine("\r\nUnexpected exception occured:\r\n\t" + e.GetType().Name + ":" + e.Message); } }