如何以编程方式更改我的Windows域密码?

时间:2010-06-08 11:23:33

标签: windows dns passwords

换句话说,如何更改我的密码而不通过“ Ctrl + Alt + Del - > 更改密码“界面。

通过编程方式我的意思是通过命令行工具,C#通过.NET库,通过Python进行COM调用,......无论什么都不涉及任何手动步骤,真的。

NET USER命令不合格,因为它要求我以域管理员权限运行。

2 个答案:

答案 0 :(得分:7)

答案 1 :(得分:4)

以下是提供的代码Sjoerd的修改版本,它可以更改密码一次,而不是循环更改多个密码。

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.");
        }
    }
}