首页 文章

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

提问于
浏览
0

我有一个场景,我需要访问远程计算机以编程方式添加和删除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中的System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName,ServerProperties&properties)中的System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval()中的System.DirectoryServices.AccountManagement.AccountManagement.PrincipalContext . System.DirectoryServices.AccountManagement.PrincipalContext..ctor中的ContextType contextType,String name,String container,ContextOptions options,String userName,String password)(ContextType contextType,String name,String container,String userName,String password)InnerException:System.DirectoryServices .Protocols.LdapException Message = LDAP服务器不可用 . Source = System.DirectoryServices.Protocols ErrorCode = 81 StackTrace:System.DirectoryServices.Protocols中的System.DirectoryServices.Protocols.LdapConnection.Connect(),System.DirectoryServices.Protocols.LdapConnection中的SystemRequestConnection.SendRequestHelper(DirectoryRequest request,Int32&messageID) . System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName,ServerProperties&properties)中的System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest请求)中的SendRequest(DirectoryRequest请求,TimeSpan requestTimeout)InnerException:

  • 尝试#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.Antory中的System.DirectoryServices.Interop.Unsafe服务中的System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo()中的System.DirectoryServices.AccountManagement.PrincipalContext.DoMachineInit(),System.DirectoryServices.AccountManagement.PrincipalContext System.DirectoryServices.AccountManagement中System.DirectoryServices.AccountManagement.Principal.FindBdentIdentityWithTypeHelper(PrincipalContext上下文,Type principalType,Nullable`1 identityType,String identityValue,DateTime refDate)中的System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()中的.Initialize() System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context,String identityValue)中的.Principal.FindByIdentityWithType(PrincipalContext context,Type principalType,String identityValue)InnerException:

我尝试了PrincipalContext对象的不同签名(使用域名而不是IP地址,用户名和密码,......)但我总是在两次尝试中都获得例外 .

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

2 回答

  • -2

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

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

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

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

    似乎使用IP地址找不到您的Active目录

相关问题