NET将本地用户添加到本地

NET将本地用户添加到本地

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

问题描述

我有一个用于管理本地用户帐户的VB.net类(asp.net项目)。

我支持北美地区超过200台不在域内的服务器(长篇故事)。



我没有添加用户,设置密码甚至创建群组的问题,但我无法弄清楚如何将用户添加到群组中。

这是添加用户功能 - 属性用于收集信息 - 这很有效

I have a VB.net class (asp.net project) for managing local user accounts.
I support more than 200 servers across north america that are not in a domain (long story).

My has no issue adding users, setting passwords and even creating groups, but I cant figure out how to add users to a group.
Here is the add user function - properties are used for gathering information - this works well

Public Function AddUser()
        Dim obDirEntry As DirectoryEntry = Nothing
        Try
            obDirEntry = New DirectoryEntry("WinNT://" & pServer)
            Dim entries As DirectoryEntries = obDirEntry.Children
            Dim obUser As DirectoryEntry = entries.Add(pUserName, "User")
            obUser.Properties("FullName").Add(pFullName)
            obUser.Properties("Description").Add(pDescription)
            Dim obRet As Object = obUser.Invoke("SetPassword", pPassword)
            obUser.CommitChanges()
            obDirEntry.Close()
            Return True
        Catch ex As Exception
            'Trace.Warn(ex.Message)
            Return ex.Message
        End Try
    End Function





这是我将用户添加到群组的功能 - msgboxes用于收集错误信息





Here is my function for adding a user to a group - The msgboxes are there to gather error info

Public Function AddToGroup()
    Try
        Dim obDirEntry As New DirectoryEntry("WinNT://" & pServer & ",computer")
        Dim obUser As DirectoryEntry = obDirEntry.Children.Find(pUserName, "user")
        Dim obGroup As DirectoryEntry = obDirEntry.Children.Find("Administrators", "group")
        obGroup.Invoke("Add", New Object() {obUser.Path.ToString})
        obGroup.CommitChanges()
        obDirEntry.Close()
        Return True

    Catch ex As Exception
        MsgBox(ex.Message)
        MsgBox(ex.InnerException.Message)
        Return False
    End Try
End Function





以下是两个错误



1)异常被调用的目标抛出。



2)由于该成员不存在,因此无法在本地组中添加或删除该成员





我还添加到其他msgbox中查看返回的路径,这是结果



obdirentry = WinNT://10.106.3.220,computer



obUser = WinNT://workgroup/10.106.3.220/test88 ----- -

test88是targ上的有效用户和系统



任何帮助都会很棒...在此先感谢



Here are the two errors

1) Exception has been thrown by the target of an invocation.

2) A memeber could not be added to or removed from the local group because the member does not exist


I have also added to other msgbox's to look at the paths returned and this is the results

obdirentry = WinNT://10.106.3.220,computer

obUser = WinNT://workgroup/10.106.3.220/test88 ------
test88 is a valid user on the target system

Any help would be great... Thanks in advance

推荐答案


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

08-21 00:41