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

发布于 2024-09-05 11:39:45 字数 245 浏览 2 评论 0原文

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

我所说的以编程方式是指通过命令行工具、通过 .NET 库使用 C#、通过 Python 进行 COM 调用……实际上,任何不涉及任何手动步骤的方式。

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

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, as it requires me to run with domain administrator privileges.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻之岛 2024-09-12 11:39:46

使用 DirectoryEntry 类来获取和更新用户的活动目录条目。

http://linuxonly.nl/docs/21/43_Circumvent_password_expiry_in_Windows.html

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

感性 2024-09-12 11:39:46

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

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.");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文