1

我有一个场景,我需要访问远程机器以编程方式添加和删除 Windows 用户帐户。远程机器是我需要远程配置的“备用工作站”,以防万一需要更换主工作站 - 所以这里没有安全绕过或恶意软件:)

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

  • 尝试#1:

    PrincipalContext context = new PrincipalContext(ContextType.Domain, "xxx.xxx.xxx.xxx" /*remote IP Address*/);
    UserPrincipal user = (UserPrincipal.FindByIdentity(context, "userName"));
    // Do something with user, like user.Delete();
    

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

    System.DirectoryServices.AccountManagement.PrincipalServerDownException 被捕获 Message=无法联系服务器。
    Source=System.DirectoryServices.AccountManagement StackTrace:在 System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties& properties) 在 System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval() 在 System.DirectoryServices.AccountManagement.PrincipalContext..ctor( System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, String userName, String password) InnerException: System.DirectoryServices .Protocols.LdapException Message=LDAP 服务器不可用。源 = System.DirectoryServices。

  • 尝试#2:

    PrincipalContext context = new PrincipalContext(ContextType.Machine, "xxx.xxx.xxx.xxx" /*remote IP Address*/);
    UserPrincipal user = (UserPrincipal.FindByIdentity(context, "userName"));
    // Do something with user, like user.Delete();
    

    在这种情况下,我总是在第二行出现异常:

    System.IO.FileNotFoundException 被捕获 Message=找不到网络路径。

    Source=Active Directory StackTrace:在 System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo() 中 System.DirectoryServices.DirectoryEntry.RefreshCache() 在 System.DirectoryServices.AccountManagement.PrincipalContext.DoMachineInit() 在 System.DirectoryServices.AccountManagement.PrincipalContext System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate) in System.DirectoryServices.AccountManagement 中的 System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() 中的 .Initialize() System.DirectoryServices 中的 .Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue)。AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue) InnerException:

我尝试了 PrincipalContext 对象的不同签名(使用域名而不是 IP 地址,使用用户名和密码,...),但我总是在两次尝试中都遇到异常。

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

4

2 回答 2

3

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

using System.DirectoryServices;

DirectoryEntry deParent = new DirectoryEntry("WinNT://[computername]", 
         @"[domain\adminname", "password");            
DirectoryEntry deToRemove = deParent.Children.Find("usernametoremove");
deParent.Children.Remove(deToRemove);
于 2013-05-22T14:36:03.187 回答
-2

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

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

使用 IP 地址似乎无法找到您的 Active Directory

于 2013-05-22T13:06:33.857 回答