Add Domain user to local group

M

Mike

I am trying to create a small admin toolset using visual studio 2005 (vb).
I want to be able to supply a domain username and have it add to the local
workstation Remote desktop user's group. This is the code I have so far.

Dim LCL As New DirectoryEntry
Dim DOM As New DirectoryEntry
Dim DOMUSR As DirectoryEntry
Dim LCLGRP As DirectoryEntry

LCL = "WinNT://" & "PCNAME" & ",computer"
DOM = "WinNT://domain"
DOMUSR = DOM.Children.Find("USERID", "user")
LCLGRP = LCL.Children.Find("LOCAL_GROUP", "group")

LCLGRP.Invoke("Add", New Object() {DOMUSR.Path.ToString})

but I keep getting the following error, this is happening on all four dim
statements.

Error 1 Type 'DirectoryEntry' is not defined.

The directory entry does not appear to be in the classes.
 
G

Guest

Mike said:
No I haven't, how would you go about doing that.

At the very top of your page where you have your code include this line:

Imports System.DirectoryServices

In other words it should be the first line (or one of the first) in the page.

e.g.

Imports System.DirectoryServices

Public Class MyDirectoryClass
...
End Class
 
M

Mike

I tried the import statement

Imports System.DirectoryServices

But I keep getting the following error.

Error 3 'Imports' statements must precede any declarations.


I modified my code a bit to get rid of the error but now I am receiving a
new error


Dim LCL As System.DirectoryServices.DirectoryEntries(Of String)

Dim DOM As System.DirectoryServices.DirectoryEntries(Of String)

Dim DOMUSR As System.DirectoryServices.DirectoryEntry

Dim LCLGRP As System.DirectoryServices.DirectoryEntry

LCL = "WinNT://" & "PCNAME" & ",computer"

DOM = "WinNT://domain"

DOMUSR = DOM.Find("USERID", "user")

LCLGRP = LCL.Find("LOCAL_GROUP", "group")

LCLGRP.Invoke("Add", New Object() {DOMUSR.Path.ToString})



Error 1 'System.DirectoryServices.DirectoryEntries' has no type parameters
and so cannot have type arguments.
 
G

Guest

Mike said:
Dim LCL As System.DirectoryServices.DirectoryEntries(Of String)
Dim DOM As System.DirectoryServices.DirectoryEntries(Of String)

Remove the Of String from these two lines.

HTH
 
M

Mike

I tried that, and when I removed that statement, I recieved the following

Dim LCL As System.DirectoryServices.DirectoryEntries
Dim DOM As System.DirectoryServices.DirectoryEntries
Dim DOMUSR As System.DirectoryServices.DirectoryEntry
Dim LCLGRP As System.DirectoryServices.DirectoryEntry

LCL = "WinNT://" & "PCNAME" & ",computer"
DOM = "WinNT://domain"
DOMUSR = DOM.Find("USERID", "user")
LCLGRP = LCL.Find("LOCAL_GROUP", "group")

LCLGRP.Invoke("Add", New Object() {DOMUSR.Path.ToString})

Error 1 Value of type 'String' cannot be converted to
'System.DirectoryServices.DirectoryEntries'. and this error occurs on these
two lines:

LCL = "WinNT://" & "PCNAME" & ",computer"
DOM = "WinNT://domain"
 
G

Guest

Mike said:
I tried that, and when I removed that statement, I recieved the following

Dim LCL As System.DirectoryServices.DirectoryEntries
Dim DOM As System.DirectoryServices.DirectoryEntries
Dim DOMUSR As System.DirectoryServices.DirectoryEntry
Dim LCLGRP As System.DirectoryServices.DirectoryEntry

LCL = "WinNT://" & "PCNAME" & ",computer"
DOM = "WinNT://domain"
DOMUSR = DOM.Find("USERID", "user")
LCLGRP = LCL.Find("LOCAL_GROUP", "group")

LCLGRP.Invoke("Add", New Object() {DOMUSR.Path.ToString})

Error 1 Value of type 'String' cannot be converted to
'System.DirectoryServices.DirectoryEntries'. and this error occurs on these
two lines:

LCL = "WinNT://" & "PCNAME" & ",computer"
DOM = "WinNT://domain"

This is correct as this is the DirectoryEntry syntax not the
DirectoryEntries. A DirectoryEntries object contains one or more
DirectoryEntry objects. The code you have above is more applicable to your
DomUsr and LclGrp variables.

I'd suggest that you refer to the MSDN documentation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top