How can I programmatically change my windows domain password?
In other words, how to change my password without going through the "Ctrl+Alt+Del -> Change Password" interface.
By programmatically I mean via a command-line tool, C# via a .NET library, COM-invocation via Python, ... Whatever doesn't involve any manual steps, really.
The NET USER
command is ineligible, a开发者_运维问答s it requires me to run with domain administrator privileges.
Use the DirectoryEntry class to get and update the active directory entry for the user.
http://linuxonly.nl/docs/21/43_Circumvent_password_expiry_in_Windows.html
Here is a modified version of the code Sjoerd provided that changes the password once rather than cycling through multiple password changes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
namespace ChangePassword
{
class Program
{
static void Main(string[] args)
{
string Domain = Environment.UserDomainName;
string User = Environment.UserName;
if (args.Length < 2)
{
System.Console.WriteLine("Usage: ChangePassword OldPassword NewPassword [User]");
System.Console.WriteLine(" -The domain is " + Domain + ".");
System.Console.WriteLine(" -The user is " + User + " unless it is specified.");
System.Environment.Exit(1);
}
string OldPassword = args[0];
string NewPassword = args[1];
if (args.Length == 3)
User = args[2];
DirectoryEntry entry = null;
try {
entry = new DirectoryEntry(@"WinNT://" + Domain + "/" + User + ",User");
}
catch (System.Reflection.TargetInvocationException e)
{
System.Console.WriteLine("Domain/User failed due to:");
Exception cause = e.InnerException;
System.Console.WriteLine(cause.Message);
System.Environment.Exit(1);
}
try {
entry.Invoke("ChangePassword", OldPassword, NewPassword);
}
catch (System.Reflection.TargetInvocationException e)
{
System.Console.WriteLine("Password change failed due to:");
Exception cause = e.InnerException;
System.Console.WriteLine(cause.Message);
System.Environment.Exit(1);
}
System.Console.WriteLine("Ok.");
}
}
}
精彩评论