473,651 Members | 2,518 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 2899
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
1635
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
1302
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 URL discovery. I then searched through the MSDN help to discover that this can be configured through...
2
6148
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
13087
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 /// Summary description for Form1 /// </summary
1
1774
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 complained that the path did not correspond to c:\inetpub\wwwroot or something like that. Does that...
0
1241
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 DLLs built also with VS .NET 2003. This references are needed because the web methods use them in
47
3346
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
4001
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 want to use vb.net as the programming language.
3
13164
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 group in C#? TIA
2
3770
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
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8795
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8695
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8460
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7296
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4143
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2696
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1906
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.