-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAD.asmx.cs
184 lines (127 loc) · 6.8 KB
/
AD.asmx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Web.Services;
using System.Diagnostics;
using System.Configuration;
using System.Web.Configuration;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
namespace Frontend
{
/// <summary>
/// Summary description for AD Webservice
/// </summary>
[WebService(Name = "AD Web Service", Description = "AD Web Service developed by Johan Arwidmark", Namespace = "http://www.deploymentresearch.com")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// [System.Web.Script.Services.ScriptService]
public class ConfigMgr : System.Web.Services.WebService
{
#region Instance variables
String FQDNDomainName;
#endregion
#region Constructor
public ConfigMgr()
{
// Read the Frontend parameters from web.config
Trace.WriteLine(DateTime.Now + ": ConfigMgr: Read the Frontend parameters from web.config");
Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(null);
FQDNDomainName = WebConfigurationManager.AppSettings["FQDNDomainName"];
Trace.WriteLine(DateTime.Now + ": ConfigMgr: FQDNDomainName value from web.config is: " + FQDNDomainName);
}
#endregion
#region Web methods
[WebMethod]
public Boolean AddComputerToGroup(String ADGroupName, String OSDComputerName)
{
Trace.WriteLine(DateTime.Now + ": AddComputerToGroup: Starting Web Service");
Trace.WriteLine(DateTime.Now + ": AddComputerToGroup: ADGroupName received was: " + ADGroupName);
Trace.WriteLine(DateTime.Now + ": AddComputerToGroup: OSDComputerName received was: " + OSDComputerName);
Trace.WriteLine(DateTime.Now + ": AddComputerToGroup: Connecting to " + FQDNDomainName + ".");
try
{
// Connect to Active Directory
PrincipalContext AD = new PrincipalContext(ContextType.Domain, FQDNDomainName);
string controller = AD.ConnectedServer;
Trace.WriteLine(DateTime.Now + ": AddComputerToGroup: Connected to " + string.Format("Domain Controller: {0}", controller));
ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(AD, OSDComputerName);
if (computer != null)
{
Trace.WriteLine(DateTime.Now + ": AddComputerToGroup: " + OSDComputerName + " computer found in AD, continue.");
GroupPrincipal group = GroupPrincipal.FindByIdentity(AD, ADGroupName);
if (group != null)
{
Trace.WriteLine(DateTime.Now + ": AddComputerToGroup: " + ADGroupName + " group found in AD, continue.");
group.Members.Add(computer);
group.Save();
Trace.WriteLine(DateTime.Now + ": AddComputerToGroup: " + OSDComputerName + " computer added to " + ADGroupName + " group");
return true;
}
else
{
Trace.WriteLine(DateTime.Now + ": AddComputerToGroup: " + ADGroupName + " group not found in AD");
return false;
}
}
else
{
Trace.WriteLine(DateTime.Now + ": AddComputerToGroup: " + OSDComputerName + "Machine not found in AD");
return false;
}
}
catch (Exception e)
{
Trace.WriteLine(DateTime.Now + ": AddComputerToGroup: Unhandled exception finding provider namespace on server " + e.ToString());
return false;
}
}
[WebMethod]
public Boolean MoveComputerToOU(String MACHINEOBJECTOU, String OSDComputerName)
{
Trace.WriteLine(DateTime.Now + ": MoveComputerToOU: Starting Web Service");
Trace.WriteLine(DateTime.Now + ": MoveComputerToOU: MACHINEOBJECTOU received was: " + MACHINEOBJECTOU);
Trace.WriteLine(DateTime.Now + ": MoveComputerToOU: OSDComputerName received was: " + OSDComputerName);
String CurrentOU = string.Empty;
Trace.WriteLine(DateTime.Now + ": MoveComputerToOU: Connecting to " + FQDNDomainName + ".");
try
{
// Connect to AD
PrincipalContext AD = new PrincipalContext(ContextType.Domain, FQDNDomainName);
string controller = AD.ConnectedServer;
Trace.WriteLine(DateTime.Now + ": MoveComputerToOU: Connected to " + string.Format("Domain Controller: {0}", controller));
ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(AD, OSDComputerName);
if (computer != null)
{
Trace.WriteLine(DateTime.Now + ": MoveComputerToOU: Machine found in AD, continue.");
// Get Parent OU
DirectoryEntry deComputer = computer.GetUnderlyingObject() as DirectoryEntry;
DirectoryEntry deComputerContainer = deComputer.Parent;
CurrentOU = string.Format("{0}".Trim(), deComputerContainer.Properties["distinguishedName"].Value);
Trace.WriteLine(DateTime.Now + ": MoveComputerToOU: CurrentOU is " + CurrentOU);
// Verify if the selected OU is the same as the current OU
if (String.Equals(MACHINEOBJECTOU, CurrentOU, StringComparison.OrdinalIgnoreCase))
{
Trace.WriteLine(DateTime.Now + ": MoveComputerToOU: Selected OU is the same as current OU, or machine does not exist in AD, do nothing ");
}
else
{
Trace.WriteLine(DateTime.Now + ": MoveComputerToOU: Selected OU is not the same as currentOU, move computer to selected OU ");
// Move the computer object
DirectoryEntry NewParent = new DirectoryEntry("LDAP://" + MACHINEOBJECTOU);
deComputer.MoveTo(NewParent);
}
}
else
{
Trace.WriteLine(DateTime.Now + ": MoveComputerToOU: Machine not found in AD, assuming new machine, skipping move operation.");
}
}
catch (Exception e)
{
Trace.WriteLine(DateTime.Now + ": MoveComputerToOU: Unhandled exception finding provider namespace on server " + e.ToString());
return false;
}
return true;
}
#endregion
}
}