/
Program.cs
164 lines (146 loc) · 5.77 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Text;
using CommandLine.Utility;
namespace sipchange
{
using System.Management;
using System.Collections;
using System.Collections.Specialized;
using System.DirectoryServices;
using System.Text.RegularExpressions;
class Program
{
static string sQuery;
static string sQueryContacts;
static string SourceDomainURI;
static string TargetDomainURI;
public Program()
{
// query string to return instance ID of user enabled for LCS using WMI
sQuery = "select * from MSFT_SIPESUserSetting where UserDN = '";
// query string to return all contacts given a user's instance ID
sQueryContacts = "select * from MSFT_SIPESUserContactData where UserInstanceID = '";
}
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("\ncommand-line arguments are required.");
return;
}
Arguments CommandLine = new Arguments(args);
if (CommandLine["?"] != null)
{
Console.WriteLine("\ncommand-line arguments:");
Console.WriteLine("\t/source:<name>\t\t- SIP domain to match");
Console.WriteLine("\t/target:<name>\t\t- SIP domain to change");
return;
}
if (CommandLine["source"] != null)
{
SourceDomainURI = CommandLine["source"];
}
if (CommandLine["target"] != null)
{
TargetDomainURI = CommandLine["target"];
}
DirectoryEntry deRoot = null;
try
{
DirectoryEntry deRootDSE = new DirectoryEntry("LDAP://RootDSE");
string sRootDomain = "LDAP://" + deRootDSE.Properties["rootDomainNamingContext"].Value.ToString();
deRoot = new DirectoryEntry(sRootDomain);
}
catch(Exception e)
{
Console.WriteLine("Failed to query Active Directory");
Console.WriteLine(e.Message);
return;
}
// Instantiate class.
Program SipDomain = new Program();
string userDN = null;
try
{
DirectorySearcher Users = new DirectorySearcher(deRoot, "(&(objectCategory=person)(objectCategory=user)(msRTCSIP-UserEnabled=TRUE))");
foreach(SearchResult user in Users.FindAll())
{
// Obtain DN of user by removing the first 7 characters "LDAP://".
userDN = user.Path.Substring(7);
SipDomain.ChangeSIPdomain(userDN);
Console.WriteLine();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return;
}
}
public void ChangeSIPdomain(string userDN)
{
try
{
// Query for user's instance ID
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(sQuery + userDN + "'");
ManagementObjectCollection oCollection = oSearcher.Get();
foreach (ManagementObject user in oCollection)
{
Console.Write(user["PrimaryURI"].ToString());
if(Regex.IsMatch(user["PrimaryURI"].ToString(), SourceDomainURI))
{
// Modify user SIP URI domain portion
string URI = Regex.Replace(user["PrimaryURI"].ToString(), @"@[-\w.]+", "@" + TargetDomainURI);
Console.Write(" -> " + URI);
user["PrimaryURI"] = URI;
// Commit changes
//user.Put();
}
Console.WriteLine();
// Check whether user is unassigned
if (user["HomeServerDN"] == null)
{
Console.WriteLine("WARNING: this user is unassigned");
// If the user is unassigned, then it won't be possible to read their contact list
return;
}
UpdateContactsURI(user["InstanceID"].ToString());
}
}
catch (Exception e)
{
Console.WriteLine("ChangeSIPdomain(): " + e.Message);
}
}
private void UpdateContactsURI(string InstanceID)
{
try
{
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(sQueryContacts + InstanceID + "'");
ManagementObjectCollection oCollection = oSearcher.Get();
// Check whether user has any contacts in their contact list
if (oCollection.Count == 0)
{
return;
}
foreach (ManagementObject contact in oCollection)
{
if (Regex.IsMatch(contact.GetPropertyValue("SIPURI").ToString(), SourceDomainURI))
{
string URI = Regex.Replace(contact.GetPropertyValue("SIPURI").ToString(), @"@[-\w.]+", "@" + TargetDomainURI);
// Display only contacts that are modified
Console.WriteLine("\tcontact: " + contact.GetPropertyValue("SIPURI") + " -> " + URI);
contact.SetPropertyValue("SIPURI", URI);
// Commit change
//contact.Put();
}
}
}
catch (Exception e)
{
Console.WriteLine("WARNING: unable to contact user\'s home server to read contact list");
}
}
}
}