将组添加到本地管理员

将组添加到本地管理员

本文介绍了将组添加到本地管理员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在本地管理员中添加现有组.组"ABC\Some Active Group"存在.我可以通过Windows GUI添加它,但是我需要通过代码添加它.到目前为止,这是我尝试过的:

I am trying to add an existing group in Local Administrators. The group "ABC\Some Active Group" exists. I can add that through Windows GUI but I need to add it through code. Here is what I have tried so far:

public static bool AddGroup(string machineName, string groupName)
{
    bool ifSuccessful = false;
    try
    {
        DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + machineName);
        DirectoryEntry admGroup = localMachine.Children.Find("administrators", "group");
        //admGroup.Children.Add(groupName, "Group");
        admGroup.Invoke("Add", groupName);
        admGroup.CommitChanges();

        ifSuccessful = true;

    }
    catch (Exception ex)
    {
        ifSuccessful = false;
        //logging
        Console.WriteLine(machineName + " ----------" + ex.Message);
    }
    return ifSuccessful;
}

我这样称呼它:

AddGroup(Environment.MachineName, @"ABC\Some Active Group");

我得到了异常,(它是一个内部异常)

我也尝试添加它,例如:

I also tried adding it like:

admGroup.Children.Add(groupName, "Group");

但是后来我得到了例外:

But then I got the exception:

我已经能够通过admGroup成功获得所有用户和组,我不能只添加一个.有人可以告诉我我在做什么错吗?

I have been able to successfully get all the users and groups with admGroup, I can't just add one. Can someone please tell me what am I doing wrong ?

推荐答案

您需要像这样调用AddGroup

You need to call AddGroup like this

这篇关于将组添加到本地管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 14:43