Exemple #1
0
        public static IEnumerable <DomainGroup> EnumLocalGroups(bool sid = false)
        {
            int    read;
            int    total;
            int    resume;
            IntPtr buf;

            int ret = netapi.NetLocalGroupEnum(null, 1, out buf, util.MAX_PREFERRED_LENGTH, out read, out total, out resume);

            if (ret != 0)
            {
                throw new Win32Exception(ret);
            }
            if (read > 0)
            {
                DomainGroup group = new DomainGroup();
                var         g     = new LOCALGROUP_INFO_1();
                IntPtr      pItem = buf;
                for (int i = 0; i < read; i++)
                {
                    Marshal.PtrToStructure(pItem, g);
                    pItem = new IntPtr(pItem.ToInt64() + Marshal.SizeOf(typeof(LOCALGROUP_INFO_1)));

                    if (sid)
                    {
                        group.SID = GetAccountSID(g.name);
                    }
                    group.Name    = g.name;
                    group.Comment = g.comment;
                    yield return(group);
                }
                netapi.NetApiBufferFree(buf);
            }
        }
Exemple #2
0
        public static void UpdateLocalGroup(string groupname, DomainGroup group, string[] members = null)
        {
            int ret = 0;

            // rename
            if (!string.Equals(groupname, group.Name, StringComparison.OrdinalIgnoreCase))
            {
                ret = netapi.NetLocalGroupSetInfo(null, groupname, 0, ref group.Name, 0);
                if (ret != 0)
                {
                    throw new Win32Exception(ret);
                }
            }

            // attributes
            var g = new LOCALGROUP_INFO_1()
            {
                name = group.Name, comment = group.Comment
            };

            ret = netapi.NetLocalGroupSetInfo(null, g.name, 1, g, 0);
            if (ret != 0)
            {
                throw new Win32Exception(ret);
            }

            // members
            if (members != null)
            {
                ret = netapi.NetLocalGroupSetMembers(null, group.Name, 3, members, members.Length);
                if (ret != 0)
                {
                    throw new Win32Exception(ret);
                }
            }
            else
            {
                ret = netapi.NetLocalGroupSetMembers(null, group.Name, 3, new string[0], 0);
            }
        }
Exemple #3
0
        public static void AddLocalGroup(DomainGroup group, string[] members = null)
        {
            int ret = 0;
            var g   = new LOCALGROUP_INFO_1()
            {
                name = group.Name, comment = group.Comment
            };

            ret = netapi.NetLocalGroupAdd(null, 1, g, 0);
            if (ret != 0)
            {
                throw new Win32Exception(ret);
            }

            if (members != null)
            {
                ret = netapi.NetLocalGroupSetMembers(null, group.Name, 3, members, members.Length);
                if (ret != 0)
                {
                    throw new Win32Exception(ret);
                }
            }
        }