473,549 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

adding to local

hey all,

what's the best way to automate adding a user to local group on the computer
and have it be part of administrators?

thanks,
rodchar
Nov 23 '05 #1
4 2894
Hi!

You're in luck, as I wrote this function just over a week ago

Add a reference to the System.Director yServices (DLL)

Imports System.Director yServices

Private Enum ADSI_Flags
ADS_UF_SCRIPTAD S_UF_SCRIPT = 1
ADS_UF_ACCOUNTD ISABLEADS_UF_AC COUNTDISABLE = 2
ADS_UF_HOMEDIR_ REQUIREDADS_UF_ HOMEDIR_REQUIRE D = 8
ADS_UF_LOCKOUTA DS_UF_LOCKOUT = 16
ADS_UF_PASSWD_N OTREQDADS_UF_PA SSWD_NOTREQD = 32
ADS_UF_PASSWD_C ANT_CHANGEADS_U F_PASSWD_CANT_C HANGE = 64

ADS_UF_ENCRYPTE D_TEXT_PASSWORD _ALLOWEDADS_UF_ ENCRYPTED_TEXT_ PASSWORD_ALLOWE D
= 128
ADS_UF_TEMP_DUP LICATE_ACCOUNTA DS_UF_TEMP_DUPL ICATE_ACCOUNT = 256
ADS_UF_NORMAL_A CCOUNTADS_UF_NO RMAL_ACCOUNT = 512
ADS_UF_INTERDOM AIN_TRUST_ACCOU NTADS_UF_INTERD OMAIN_TRUST_ACC OUNT =
2048
ADS_UF_WORKSTAT ION_TRUST_ACCOU NTADS_UF_WORKST ATION_TRUST_ACC OUNT =
4096
ADS_UF_SERVER_T RUST_ACCOUNTADS _UF_SERVER_TRUS T_ACCOUNT = 8192
ADS_UF_DONT_EXP IRE_PASSWDADS_U F_DONT_EXPIRE_P ASSWD = 65536
ADS_UF_MNS_LOGO N_ACCOUNTADS_UF _MNS_LOGON_ACCO UNT = 131072
ADS_UF_SMARTCAR D_REQUIREDADS_U F_SMARTCARD_REQ UIRED = 262144
ADS_UF_TRUSTED_ FOR_DELEGATIONA DS_UF_TRUSTED_F OR_DELEGATION = 524288
ADS_UF_NOT_DELE GATEDADS_UF_NOT _DELEGATED = 1048576
ADS_UF_USE_DES_ KEY_ONLYADS_UF_ USE_DES_KEY_ONL Y = 2097152
ADS_UF_DONT_REQ UIRE_PREAUTHADS _UF_DONT_REQUIR E_PREAUTH = 4194304
ADS_UF_PASSWORD _EXPIREDADS_UF_ PASSWORD_EXPIRE D = 8388608

ADS_UF_TRUSTED_ TO_AUTHENTICATE _FOR_DELEGATION ADS_UF_TRUSTED_ TO_AUTHENTICATE _
FOR_DELEGATION = 16777216
End Enum

Private Function AddUser(ByVal sLogin As String, ByVal sPassword As
String) As Boolean ', ByVal sGroup As String) As Boolean
Dim dirEntry As DirectoryEntry
dirEntry = New DirectoryEntry( "WinNT://" + Environment.Mac hineName +
",computer" )

Dim entries As DirectoryEntrie s = dirEntry.Childr en

' Set login name and full name.
Dim newUser As DirectoryEntry = entries.Add(sLo gin, "User")
Try
'newUser.Proper ties("FullName" ).Add(fullName)
'newUser.Proper ties("HomeDirec tory").Add("C:\ TestDirectory")
newUser.Propert ies("Descriptio n").Add("My User's Description
Here")

' User must change password at next logon (1 - true, 0 - false)
newUser.Propert ies("PasswordEx pired").Add(0)

' Password never expires.
'newUser.Proper ties("PasswordA ge").Add(0)

' Set flags - User Cannot change password | Password never
expires.

newUser.Propert ies("Userflags" ).Add(ADSI_Flag s.ADS_UF_PASSWD _CANT_CHANGEADS _
UF_PASSWD_CANT_ CHANGE Or
ADSI_Flags.ADS_ UF_DONT_EXPIRE_ PASSWDADS_UF_DO NT_EXPIRE_PASSW D)
'newUser.Proper ties("Userflags ").Add(&H40 Or &H10000)
'newUser.Proper ties("Userflags ").Add(64 Or 65536)

' Set the password.
Dim result As Object = newUser.Invoke( "SetPasswor d", sPassword)

newUser.CommitC hanges()

' Add user to the group "Members"
Dim grp As DirectoryEntry =
dirEntry.Childr en.Find("Admini strators", "group")
If (Not grp Is Nothing) Then
grp.Invoke("Add ", New Object() {newUser.Path.T oString()})
End If

' Dim usrGroup As DirectoryEntry =
dirEntry.Childr en.Find("Users" , "group")
' If (Not usrGroup Is Nothing) Then
' usrGroup.Invoke ("Add", New Object()
{newUser.Path.T oString()})
' End If

Return True
Catch ex As Exception
Return False
End Try
End Function

Example:
---------

AddUser("My USername Here", "My Password Here")

That will add a user if not already there, otherwise, it will fail

If you remove the commit changes & a few lines above & then use the 'If'
statement to check to see if the user is in the 'Administrators ' group, if
not the line between the 'If' statement will add the user to the
'Administrators ' group.

I hope this helps,

Crouchie1998
BA (HONS) MCP MCSE
Nov 23 '05 #2
how much different would it be if active directory isn't present?

"Crouchie19 98" wrote:
Hi!

You're in luck, as I wrote this function just over a week ago

Add a reference to the System.Director yServices (DLL)

Imports System.Director yServices

Private Enum ADSI_Flags
ADS_UF_SCRIPTAD S_UF_SCRIPT = 1
ADS_UF_ACCOUNTD ISABLEADS_UF_AC COUNTDISABLE = 2
ADS_UF_HOMEDIR_ REQUIREDADS_UF_ HOMEDIR_REQUIRE D = 8
ADS_UF_LOCKOUTA DS_UF_LOCKOUT = 16
ADS_UF_PASSWD_N OTREQDADS_UF_PA SSWD_NOTREQD = 32
ADS_UF_PASSWD_C ANT_CHANGEADS_U F_PASSWD_CANT_C HANGE = 64

ADS_UF_ENCRYPTE D_TEXT_PASSWORD _ALLOWEDADS_UF_ ENCRYPTED_TEXT_ PASSWORD_ALLOWE D
= 128
ADS_UF_TEMP_DUP LICATE_ACCOUNTA DS_UF_TEMP_DUPL ICATE_ACCOUNT = 256
ADS_UF_NORMAL_A CCOUNTADS_UF_NO RMAL_ACCOUNT = 512
ADS_UF_INTERDOM AIN_TRUST_ACCOU NTADS_UF_INTERD OMAIN_TRUST_ACC OUNT =
2048
ADS_UF_WORKSTAT ION_TRUST_ACCOU NTADS_UF_WORKST ATION_TRUST_ACC OUNT =
4096
ADS_UF_SERVER_T RUST_ACCOUNTADS _UF_SERVER_TRUS T_ACCOUNT = 8192
ADS_UF_DONT_EXP IRE_PASSWDADS_U F_DONT_EXPIRE_P ASSWD = 65536
ADS_UF_MNS_LOGO N_ACCOUNTADS_UF _MNS_LOGON_ACCO UNT = 131072
ADS_UF_SMARTCAR D_REQUIREDADS_U F_SMARTCARD_REQ UIRED = 262144
ADS_UF_TRUSTED_ FOR_DELEGATIONA DS_UF_TRUSTED_F OR_DELEGATION = 524288
ADS_UF_NOT_DELE GATEDADS_UF_NOT _DELEGATED = 1048576
ADS_UF_USE_DES_ KEY_ONLYADS_UF_ USE_DES_KEY_ONL Y = 2097152
ADS_UF_DONT_REQ UIRE_PREAUTHADS _UF_DONT_REQUIR E_PREAUTH = 4194304
ADS_UF_PASSWORD _EXPIREDADS_UF_ PASSWORD_EXPIRE D = 8388608

ADS_UF_TRUSTED_ TO_AUTHENTICATE _FOR_DELEGATION ADS_UF_TRUSTED_ TO_AUTHENTICATE _
FOR_DELEGATION = 16777216
End Enum

Private Function AddUser(ByVal sLogin As String, ByVal sPassword As
String) As Boolean ', ByVal sGroup As String) As Boolean
Dim dirEntry As DirectoryEntry
dirEntry = New DirectoryEntry( "WinNT://" + Environment.Mac hineName +
",computer" )

Dim entries As DirectoryEntrie s = dirEntry.Childr en

' Set login name and full name.
Dim newUser As DirectoryEntry = entries.Add(sLo gin, "User")
Try
'newUser.Proper ties("FullName" ).Add(fullName)
'newUser.Proper ties("HomeDirec tory").Add("C:\ TestDirectory")
newUser.Propert ies("Descriptio n").Add("My User's Description
Here")

' User must change password at next logon (1 - true, 0 - false)
newUser.Propert ies("PasswordEx pired").Add(0)

' Password never expires.
'newUser.Proper ties("PasswordA ge").Add(0)

' Set flags - User Cannot change password | Password never
expires.

newUser.Propert ies("Userflags" ).Add(ADSI_Flag s.ADS_UF_PASSWD _CANT_CHANGEADS _
UF_PASSWD_CANT_ CHANGE Or
ADSI_Flags.ADS_ UF_DONT_EXPIRE_ PASSWDADS_UF_DO NT_EXPIRE_PASSW D)
'newUser.Proper ties("Userflags ").Add(&H40 Or &H10000)
'newUser.Proper ties("Userflags ").Add(64 Or 65536)

' Set the password.
Dim result As Object = newUser.Invoke( "SetPasswor d", sPassword)

newUser.CommitC hanges()

' Add user to the group "Members"
Dim grp As DirectoryEntry =
dirEntry.Childr en.Find("Admini strators", "group")
If (Not grp Is Nothing) Then
grp.Invoke("Add ", New Object() {newUser.Path.T oString()})
End If

' Dim usrGroup As DirectoryEntry =
dirEntry.Childr en.Find("Users" , "group")
' If (Not usrGroup Is Nothing) Then
' usrGroup.Invoke ("Add", New Object()
{newUser.Path.T oString()})
' End If

Return True
Catch ex As Exception
Return False
End Try
End Function

Example:
---------

AddUser("My USername Here", "My Password Here")

That will add a user if not already there, otherwise, it will fail

If you remove the commit changes & a few lines above & then use the 'If'
statement to check to see if the user is in the 'Administrators ' group, if
not the line between the 'If' statement will add the user to the
'Administrators ' group.

I hope this helps,

Crouchie1998
BA (HONS) MCP MCSE

Nov 23 '05 #3
thanks, this helped.

"Crouchie19 98" wrote:
Hi!

You're in luck, as I wrote this function just over a week ago

Add a reference to the System.Director yServices (DLL)

Imports System.Director yServices

Private Enum ADSI_Flags
ADS_UF_SCRIPTAD S_UF_SCRIPT = 1
ADS_UF_ACCOUNTD ISABLEADS_UF_AC COUNTDISABLE = 2
ADS_UF_HOMEDIR_ REQUIREDADS_UF_ HOMEDIR_REQUIRE D = 8
ADS_UF_LOCKOUTA DS_UF_LOCKOUT = 16
ADS_UF_PASSWD_N OTREQDADS_UF_PA SSWD_NOTREQD = 32
ADS_UF_PASSWD_C ANT_CHANGEADS_U F_PASSWD_CANT_C HANGE = 64

ADS_UF_ENCRYPTE D_TEXT_PASSWORD _ALLOWEDADS_UF_ ENCRYPTED_TEXT_ PASSWORD_ALLOWE D
= 128
ADS_UF_TEMP_DUP LICATE_ACCOUNTA DS_UF_TEMP_DUPL ICATE_ACCOUNT = 256
ADS_UF_NORMAL_A CCOUNTADS_UF_NO RMAL_ACCOUNT = 512
ADS_UF_INTERDOM AIN_TRUST_ACCOU NTADS_UF_INTERD OMAIN_TRUST_ACC OUNT =
2048
ADS_UF_WORKSTAT ION_TRUST_ACCOU NTADS_UF_WORKST ATION_TRUST_ACC OUNT =
4096
ADS_UF_SERVER_T RUST_ACCOUNTADS _UF_SERVER_TRUS T_ACCOUNT = 8192
ADS_UF_DONT_EXP IRE_PASSWDADS_U F_DONT_EXPIRE_P ASSWD = 65536
ADS_UF_MNS_LOGO N_ACCOUNTADS_UF _MNS_LOGON_ACCO UNT = 131072
ADS_UF_SMARTCAR D_REQUIREDADS_U F_SMARTCARD_REQ UIRED = 262144
ADS_UF_TRUSTED_ FOR_DELEGATIONA DS_UF_TRUSTED_F OR_DELEGATION = 524288
ADS_UF_NOT_DELE GATEDADS_UF_NOT _DELEGATED = 1048576
ADS_UF_USE_DES_ KEY_ONLYADS_UF_ USE_DES_KEY_ONL Y = 2097152
ADS_UF_DONT_REQ UIRE_PREAUTHADS _UF_DONT_REQUIR E_PREAUTH = 4194304
ADS_UF_PASSWORD _EXPIREDADS_UF_ PASSWORD_EXPIRE D = 8388608

ADS_UF_TRUSTED_ TO_AUTHENTICATE _FOR_DELEGATION ADS_UF_TRUSTED_ TO_AUTHENTICATE _
FOR_DELEGATION = 16777216
End Enum

Private Function AddUser(ByVal sLogin As String, ByVal sPassword As
String) As Boolean ', ByVal sGroup As String) As Boolean
Dim dirEntry As DirectoryEntry
dirEntry = New DirectoryEntry( "WinNT://" + Environment.Mac hineName +
",computer" )

Dim entries As DirectoryEntrie s = dirEntry.Childr en

' Set login name and full name.
Dim newUser As DirectoryEntry = entries.Add(sLo gin, "User")
Try
'newUser.Proper ties("FullName" ).Add(fullName)
'newUser.Proper ties("HomeDirec tory").Add("C:\ TestDirectory")
newUser.Propert ies("Descriptio n").Add("My User's Description
Here")

' User must change password at next logon (1 - true, 0 - false)
newUser.Propert ies("PasswordEx pired").Add(0)

' Password never expires.
'newUser.Proper ties("PasswordA ge").Add(0)

' Set flags - User Cannot change password | Password never
expires.

newUser.Propert ies("Userflags" ).Add(ADSI_Flag s.ADS_UF_PASSWD _CANT_CHANGEADS _
UF_PASSWD_CANT_ CHANGE Or
ADSI_Flags.ADS_ UF_DONT_EXPIRE_ PASSWDADS_UF_DO NT_EXPIRE_PASSW D)
'newUser.Proper ties("Userflags ").Add(&H40 Or &H10000)
'newUser.Proper ties("Userflags ").Add(64 Or 65536)

' Set the password.
Dim result As Object = newUser.Invoke( "SetPasswor d", sPassword)

newUser.CommitC hanges()

' Add user to the group "Members"
Dim grp As DirectoryEntry =
dirEntry.Childr en.Find("Admini strators", "group")
If (Not grp Is Nothing) Then
grp.Invoke("Add ", New Object() {newUser.Path.T oString()})
End If

' Dim usrGroup As DirectoryEntry =
dirEntry.Childr en.Find("Users" , "group")
' If (Not usrGroup Is Nothing) Then
' usrGroup.Invoke ("Add", New Object()
{newUser.Path.T oString()})
' End If

Return True
Catch ex As Exception
Return False
End Try
End Function

Example:
---------

AddUser("My USername Here", "My Password Here")

That will add a user if not already there, otherwise, it will fail

If you remove the commit changes & a few lines above & then use the 'If'
statement to check to see if the user is in the 'Administrators ' group, if
not the line between the 'If' statement will add the user to the
'Administrators ' group.

I hope this helps,

Crouchie1998
BA (HONS) MCP MCSE

Nov 23 '05 #4
Pleasure

Crouchie1998
BA (HONS) MCP MCSE
Nov 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1628
by: Carlo | last post by:
I was wondering if someone could help me with this or point me to a good resource. I'm trying to add some attributes to docbook to be used later in effectivity. It was recommended I change the dbpoolx.mod: <!ENTITY % local.common.attrib ""> to <!ENTITY % local.common.attrib
1
1298
by: asp.net NUT!!! | last post by:
Hi I've been trying to add a web refernece to a asp.net application project, from a web service within the same solution. The site is hosted on my local host & in a virtual directory. when I go through the process of adding the web refernce to the application layer I get an error message stating that my LAN services are configured for web...
2
6147
by: David Williams | last post by:
I have a simple XML file that I have been using: <component name="test"> <class name="class"/> <component> Up till now, the Xpath of "//component/class" worked well to select the <class> tag. Recently we added a namespace to the <component> tag:
2
13084
by: Lisa Jones | last post by:
Hi I am trying to add web reference to my code to use web services . (I am using VS 2003) so I go to add references/ select web reference. After selecting the right web service from my local machine and rename it to something other than local host (aWebService) and then Adding the cod sing aWebService namespace WebClien /// <summary ///...
1
1771
by: ~~~ .NET Ed ~~~ | last post by:
I have a couple of websites that I develop locally on WinXP Pro and these have their virtual directories mapped to another local drive (i.e. E:\Websites\*) rather than the default C:\inetpub\wwwroot\* location. Using VS.NET 2003 I wanted to add my solution to source control (local VSS database as well) but was unable to do it because it...
0
1237
by: MIGUEL | last post by:
Hi all! Be patient because what I'm going to explain all of you it's more than very strange. I've developed a webservice project that contains two classes. One of them is going to act as a SOAP header, and the other class is the main class containing all the web methods. Besides, the project contains two references pointing at two normal...
47
3321
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols are objects whose representation within the code is more important than their actual value. Two symbols needs only to be
5
3996
by: Rocky | last post by:
Hi, I have a webform, with 2 textboxs and a submit button. In the text box1, i enter a username and in textbox2 I enter the computer name. Both the username and computer name is in active directory. When i click submit, I want to add the username from textbox1 into the local administrators group on the computer name from textbox2. I...
3
13155
by: enilno | last post by:
I'm working on a C# app., and I need to programmatically add a local user to a local group on the computer on which the app. is running. When end-users run this app., they will be logged on as local Administrator (so they should have sufficient permissions). Would any one out there know how to programmatically add a local user to a local...
2
3768
by: silversurfer | last post by:
Hello, I am a little unsure whether this method really makes sense. The goal is to add an element to a vector. This is the struct and method I am using: std::vector<Entry> models; struct Entry{ int index; FeatureVector* value;
0
7550
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7746
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7836
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5116
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3502
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
789
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.