使用c#

时间:2017-06-05 11:38:06

标签: c# windows winforms active-directory wmic

我尝试重命名一些Windows本地帐户,我使用了3种方法: (所有这些都在提升的管理员帐户中运行) 第一个工作,但它需要7-9秒。 这是我的第一个问题:
为什么这么慢?

public bool RenameWinUser(winUser winUser, string newname) {
  log.writeToLog(null, "[renameWinUser]");
  DirectoryEntry localDirectory = new DirectoryEntry("WinNT://" + Environment.MachineName.ToString());
  DirectoryEntries localUsers = localDirectory.Children;
    try {
      DirectoryEntry toRename = localUsers.Find(winUser.getName());
      try {
        log.writeToLog(null, String.Format("[renameWinUser] {0} is found, try to rename", winUser.getName()));
        try {
          toRename.Rename(newname);
          log.writeToLog(null, String.Format("[renameWinUser] {0} is renamed", winUser.getName()));
          return true;
        }
        catch (Exception e) {
         log.writeToLog(null, String.Format("[renameWinUser] Rename user exception: ", e.ToString()));
        }
      }
      catch (Exception e) {
        log.writeToLog(null, String.Format("[renameWinUser] Rename user exception: ", e.ToString()));
        return false;
      }
    }
    catch (Exception e) {
      log.writeToLog(null, String.Format("[removeWinUser] Remove user exception while searching: ", e.ToString()));
      return false;
    }
  return false;
}

第二个是快速的,但它不会保存用户名/ SamAccountName / UserPrincipalName,只是FullName。
所以,这是我的第二个问题:我做错了什么?

public bool RenameWinUser(winUser winUser, string newname) {
  log.writeToLog(null, "[renameWinUser]");
  PrincipalContext context = new PrincipalContext(ContextType.Machine, Environment.MachineName);
  UserPrincipal user = new UserPrincipal(context);
  PrincipalSearcher usrSearch = new PrincipalSearcher(user);
  foreach (UserPrincipal usrResult in usrSearch.FindAll()) {
    if (usrResult.SamAccountName.ToLower() == winUser.getName().ToLower()) {
      log.writeToLog(null, String.Format("[renameWinUser] {0} is found, try to rename", winUser.getName()));
      try {
        usrResult.SamAccountName = newname;
        usrResult.Name = newname;
        usrResult.DisplayName = newname;
        //usrResult.UserPrincipalName = newname + "@" + Domain.GetCurrentDomain();
        //this throws:Current security context is not associated with an Active Directory domain or forest
        //usrResult.UserPrincipalName = newname + "@" + Environment.MachineName;
        //this throws:This property is invalid for this storage type
      }
      catch (Exception e) {
        log.writeToLog(null, String.Format("[renameWinUser] Failed to set properties: {0}", e.Message.ToString()));
        return false;
        }
      try {
        usrResult.Save();
        log.writeToLog(null, String.Format("[renameWinUser] Rename was success", winUser.getName()));
        return true;
      }
      catch (Exception e) {
        log.writeToLog(null, String.Format("[renameWinUser] Failed to save: {0}", e.Message.ToString()));
        return false;
      }
    }
  }
  return false;
}

我的第三个选项是UserPrincipal.FindByIdentity(),但是只需要7-9秒来保存全名,而不是用户名/ SamAccountName / UserPrincipalName。 我的最后一个问题:
为什么UserPrincipal.FindByIdentity()需要这么长时间
我又做错了什么?

public bool RenameWinUser(winUser winUser, string newname) {
  log.writeToLog(null, "[renameWinUser]");
  PrincipalContext context = new PrincipalContext(ContextType.Machine, Environment.MachineName);
  UserPrincipal user = UserPrincipal.FindByIdentity(context, winUser.getName());
  log.writeToLog(null, String.Format("[renameWinUser] {0} is found, try to rename", winUser.getName()));
  try {
    user.SamAccountName = newname;
    user.Name = newname;
    user.DisplayName = newname;
    //user.UserPrincipalName = newname + "@" + Domain.GetCurrentDomain();
  }
  catch (Exception e) {
    log.writeToLog(null, String.Format("[renameWinUser] Failed to set properties: {0}", e.Message.ToString()));
    return false;
  }
  try {
    user.Save();
    log.writeToLog(null, String.Format("[renameWinUser] Rename was success", winUser.getName()));
    return true;
  }
  catch (Exception e) {
    log.writeToLog(null, String.Format("[renameWinUser] Failed to save: {0}", e.Message.ToString()));
    return false;
  }
}

我不想使用cmd / wmic / System.Management.SelectQuery,但我知道,那个wmic

wmic useraccount where name='apple' rename grape

只需0.1-2秒。

我真的需要你的帮助,伙计......

0 个答案:

没有答案