-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAddAdministrator.cs
40 lines (36 loc) · 1.35 KB
/
AddAdministrator.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
### Update username and password fields
###
### Compile using:
### csc /r:System.DirectoryServices.AccountManagement.dll /out:AddAdministrator.exe AddAdministrator.cs
###
using System.DirectoryServices.AccountManagement;
using System.Security.Principal;
namespace AddAdministrator
{
class Program
{
static void Main(string[] args)
{
string username = "<compile error here>"
string password = "<compile error here>"
using (var context = new PrincipalContext(ContextType.Machine))
{
using (var user = new UserPrincipal(context, username, password, true))
{
user.DisplayName = "IT Backup Account";
user.Description = "";
user.PasswordNeverExpires = true;
user.UserCannotChangePassword = false;
user.Save();
var adminSID = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null)
.Translate(typeof(NTAccount)).Value;
using (var group = GroupPrincipal.FindByIdentity(context, IdentityType.Name, adminSID.ToString()))
{
group.Members.Add(user);
group.Save();
}
}
}
}
}
}