1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.DirectoryServices;
6: using Microsoft.EnterpriseManagement;
7: using Microsoft.EnterpriseManagement.Administration;
8: using Microsoft.EnterpriseManagement.Common;
9: using System.ServiceProcess;
10:
11: namespace MomSynchTask
12: {
13:
14: class ComputerAccount{
15: private string _name= "";
16: public string Name {
17: get {return _name;}
18: set{ _name=value;}
19: }
20: public ComputerAccount(string name){
21: Name = name;
22: }
23: public override string ToString()
24: {
25: return Name;
26: }
27: }
28:
29: class Task
30: {
31:
32: List<ComputerAccount> AdComputerAccounts = new List<ComputerAccount>();
33: List<AgentManagedComputer> MomAccountsToDelete = new List<AgentManagedComputer>();
34:
35: // uses SDK to connect to Sce using current credentials
36: protected ManagementGroup ConnectToSce(string hostname)
37: {
38: Console.WriteLine("Attempting to connect to the SDK Service by using the current user's credentials.");
39: ManagementGroup mg = null;
40: try
41: {
42: mg = ManagementGroup.Connect(hostname);
43: if (mg.IsConnected)
44: Console.WriteLine("Connection succeeded.");
45: else
46: throw new InvalidOperationException("Not connected to an SDK Service.");
47: }
48: catch (ServerDisconnectedException sde)
49: {
50: Console.WriteLine("\nConnection failed. " + sde.Message);
51: if (sde.InnerException != null)
52: Console.WriteLine(sde.InnerException.Message);
53:
54: // Call custom method to prompt the user for credentials if needed.
55:
56: }
57: catch (ServiceNotRunningException snr)
58: {
59: Console.WriteLine(snr.Message);
60:
61: // Make one attempt to start the service.
62: System.ServiceProcess.ServiceController sc = new
63: ServiceController("MOMSDK", hostname);
64:
65: Console.WriteLine("Attempting to start the SDK Service on " + hostname + ".");
66: sc.Start();
67:
68: // Wait 20 seconds for it to enter the Running state.
69: sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 20));
70:
71: if (sc.Status == ServiceControllerStatus.Running)
72: {
73: mg = new ManagementGroup(hostname);
74: Console.WriteLine("Connection succeeded.");
75: }
76: else
77: {
78: throw new InvalidOperationException("Unable to restart and connect to the SDK Service.");
79: }
80: }
81: return mg;
82: }
83:
84: public void GetAdComputerAccounts()
85: {
86: try
87: {
88: DirectoryEntry root = new DirectoryEntry();
89: DirectorySearcher searcher = new DirectorySearcher();
90: searcher.Filter = "(objectCategory=Computer)";
91: searcher.PropertiesToLoad.Add("Name");
92: searcher.PropertiesToLoad.Add("dnsHostName");
93: searcher.PropertiesToLoad.Add("sAMAccountName");
94: searcher.PageSize = 1000;
95: searcher.SearchScope = SearchScope.Subtree;
96: Console.WriteLine("Enumerating computer accounts in Active Directory...");
97: foreach (SearchResult result in searcher.FindAll())
98: {
99: DirectoryEntry entry = result.GetDirectoryEntry();
100:
101: ComputerAccount computer = new ComputerAccount((string)entry.Properties["dnsHostName"].Value);
102: AdComputerAccounts.Add(computer);
103:
104: Console.WriteLine("Name=" + (string)entry.Properties["Name"].Value +
105: " dnsHostName=" + (string)entry.Properties["dnsHostName"].Value +
106: " sAMAccountName=" + (string)entry.Properties["sAMAccountName"].Value);
107: }
108: }
109: catch (Exception ex)
110: {
111: Console.WriteLine(ex.Message);
112: }
113: }
114: public ManagementGroupAdministration GetMomComputerAccouts(string hostname)
115: {
116: ManagementGroupAdministration mga=null;
117: ManagementGroup mg = ConnectToSce(hostname);
118: if (mg !=null){
119: mga = mg.GetAdministration();
120: //filter managed computer accounts see http://msdn.microsoft.com/en-us/library/bb437603.aspx
121: AgentManagedComputerCriteria criteria = new AgentManagedComputerCriteria("Name LIKE '%'");
122: foreach (AgentManagedComputer momAccount in mga.GetAgentManagedComputers(criteria))
123: {
124: //BIOS name of computer
125: Console.WriteLine("BIOS Name=" + momAccount.ComputerName + " FQDN Name=" + momAccount.PrincipalName + " Name=" + momAccount.Name);
126: //check if this computer Exists in Active Directory- we compate PrincipalName with dnsHostName
127: if (AdComputerAccounts.Exists(p => p.Name.Equals(momAccount.PrincipalName, System.StringComparison.InvariantCultureIgnoreCase)) == false)
128: {
129: Console.WriteLine("Added Mom Computer Account:" + momAccount.PrincipalName + " to deletion list");
130: MomAccountsToDelete.Add(momAccount);
131: }
132: else
133: {
134: Console.WriteLine("Computer Account:" + momAccount.PrincipalName + " appears to exist in Active Directory.");
135:
136: }
137:
138: }
139: }
140: return mga;
141: }
142: public void RemoveMomComputerAccounts(string[] args, ManagementGroupAdministration mga)
143: {
144:
145: mga.DeleteAgentManagedComputers(MomAccountsToDelete);
146: Console.WriteLine("Removed Mom Computer accounts");
147: return;
148: }
149: }
150:
151: class Program
152: {
153: static void Main(string[] args)
154: {
155: Task t = new Task();
156: //Get list of computer accounts from Active Directory
157: t.GetAdComputerAccounts();
158: //Get list of Ops Manager managed computer accounts to be deleted
159: ManagementGroupAdministration mga = t.GetMomComputerAccouts("localhost");
160: //Remove managed computer accounts that have no corresponding Active Directory account
161: // if /d switch has been specified on command line
162: if (args.Length > 0)
163: {
164: if ((mga != null) && (args[0].Equals("/d")))
165: t.RemoveMomComputerAccounts(args, mga);
166: }
167: }
168:
169: }
170: }