C# 如何访问远程计算机以添加/删除/管理用户帐户?

C# 如何访问远程计算机以添加/删除/管理用户帐户?,c#,active-directory,impersonation,remote-access,principal,C#,Active Directory,Impersonation,Remote Access,Principal,我有一个场景,需要访问远程计算机以编程方式添加和删除Windows用户帐户。 远程机器是一个“备用工作站”,我需要远程配置,以便在需要更换主工作站时准备就绪-因此这里没有安全旁路或恶意软件:) 我知道远程计算机管理员的用户/密码,并且我能够使用WMI Win32_UserAccount检索现有用户帐户的完整列表。现在,我试图为每个用户获取一个UserPrincipal对象(最终将其删除),但我的所有尝试都会遇到异常 尝试#1: 在这种情况下,我总是在第一行得到一个异常: System.Dire

我有一个场景,需要访问远程计算机以编程方式添加和删除Windows用户帐户。 远程机器是一个“备用工作站”,我需要远程配置,以便在需要更换主工作站时准备就绪-因此这里没有安全旁路或恶意软件:)

我知道远程计算机管理员的用户/密码,并且我能够使用WMI Win32_UserAccount检索现有用户帐户的完整列表。现在,我试图为每个用户获取一个UserPrincipal对象(最终将其删除),但我的所有尝试都会遇到异常

  • 尝试#1:

    在这种情况下,我总是在第一行得到一个异常:

    System.DirectoryServices.AccountManagement.PrincipalServerDownException 已捕获消息=无法联系服务器。
    Source=System.DirectoryServices.AccountManagement StackTrace: 在System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(字符串)中 服务器名称、服务器属性和属性) 在System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval()中 在System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType)中 contextType、字符串名称、字符串容器、ContextOptions选项、, 字符串用户名、字符串密码) 在System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType)中 contextType、字符串名称、字符串容器、字符串用户名、字符串 密码)InnerException: System.DirectoryServices.Protocols.LdapException Message=LDAP服务器不可用。 Source=System.DirectoryServices.Protocols 错误代码=81 堆栈跟踪: 在System.DirectoryServices.Protocols.LdapConnection.Connect()中 在System.DirectoryServices.Protocols.LdapConnection.SendRequestHelper(DirectoryRequest)中 请求、Int32和messageID) 在System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest)中 请求,TimeSpan请求超时) 在System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest)中 请求) 在System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(字符串)中 服务器名称、服务器属性和属性) 内部异常:

  • 尝试#2:

    在这种情况下,我总是在第二行得到一个异常:

    System.IO.FileNotFoundException被捕获消息=网络路径未被捕获 找到了

    Source=Active Directory堆栈跟踪: 在System.DirectoryServices.Interop.UnsafentiveMethods.IAds.GetInfo()中 在System.DirectoryServices.DirectoryEntry.RefreshCache()中 在System.DirectoryServices.AccountManagement.PrincipalContext.DoMachineInit()中 在System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()中 在System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()中 在System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext)中 上下文,类型principalType,可空`1 identityType,字符串 identityValue,DateTime refDate) 在System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext)中 上下文、类型原则类型、字符串标识值) 在System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext)中 上下文、字符串标识值) 内部异常:

我尝试过PrincipalContext对象的不同签名(使用域名而不是IP地址,使用用户名和密码,…),但在两次尝试中都会出现异常

我错过了一些指示吗?在创建PrincipalContext对象之前,是否需要使用模拟来获得对远程计算机的完全访问权限?
还有其他方法来完成我想做的事情吗?(即,访问远程计算机以添加/删除Windows帐户)

如果您在域下,请删除IP地址参数并尝试使用此参数

using(var pc = new PrincipalContext(ContextType.Domain))
{
  using(var up = new UserPrincipal(pc))
  {
   //Do some stuff
  }
}

似乎无法使用Ip地址找到Active directory如果您在域下,请删除Ip地址参数并尝试使用此参数

using(var pc = new PrincipalContext(ContextType.Domain))
{
  using(var up = new UserPrincipal(pc))
  {
   //Do some stuff
  }
}

似乎无法使用Ip地址找到您的Active directory如果您只是想删除帐户,为什么不使用目录服务:

using System.DirectoryServices;

DirectoryEntry deParent = new DirectoryEntry("WinNT://[computername]", 
         @"[domain\adminname", "password");            
DirectoryEntry deToRemove = deParent.Children.Find("usernametoremove");
deParent.Children.Remove(deToRemove);

如果您只是想删除该帐户,为什么不改用目录服务:

using System.DirectoryServices;

DirectoryEntry deParent = new DirectoryEntry("WinNT://[computername]", 
         @"[domain\adminname", "password");            
DirectoryEntry deToRemove = deParent.Children.Find("usernametoremove");
deParent.Children.Remove(deToRemove);

嘿,谢谢!使用您的示例代码,我没有得到任何异常,但pc和up都将其所有属性设置为null,因此我无法对用户对象执行任何操作。其他问题:如果我没有指定IP地址,代码如何“理解”我指的是哪台远程机器?嘿,谢谢!使用您的示例代码,我没有得到任何异常,但pc和up都将其所有属性设置为null,因此我无法对用户对象执行任何操作。其他问题:如果我没有指定IP地址,代码如何“理解”我指的是哪台远程机器?非常感谢!我没有想到
DirectoryServices
来删除帐户。您是否知道使用
DirectoryServices
class I是否也能够以编程方式创建新帐户和更改用户设置?不管怎样,我会深入了解这门课,谢谢。当然。MSDN中的DirecoryEntry页面将显示执行此操作所需的所有方法。不要忘记“提交”您的更改。另外,如果这回答了你的问题,请勾选是的,谢谢,我想这回答了我的问题。我非常关注
System.DirectoryServices.AccountManagement
名称空间,主要是因为引入了,所以我没有考虑长期存在的
DirectoryEntry
类。非常感谢!我没有想到
DirectoryServices
来删除帐户。你知道如果使用
DirectoryServices
class I