Create Windows User in Local Machine
Posted by wrvishnu on September 27, 2007
Creating windows users in Local Machine using coldfusion 8 & .Net
Steps to be Followed :
Create a .Net Class Library to Create Windows NT Userccount in local machine using VB as Language
Write a Coldfuusion Entry form to Enter the necessary information to Create the user account
Write a Coldfuusion Action page to call the .Net Method to create the user account in local windows machine
Code Snippets
CFM Code
Entry form Code
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Entry Form</title>
</head>
<!—
Password needs to be given as Pasword policy set in that machine
Paramenter to be passed
First Parameter : Username
Second Parameter : Password
Third Parameter : Description
Fourth Parameter : FullName
Fifth Parameter : Machine name like WinNT://machinenameNote : As this is Just a sample there is no Validation or exception Handling
This Example will work only in Windows OS ....---><cfform name="Adsample" method="post" action="Act_ADSample.cfm" >
<strong>Username </strong><cfinput required="yes" message="Username Required" name="LclUsername" size="10"><br />
<strong>Password</strong><cfinput required="yes" message="Password Required" name="LclPassword" size="10"><br />
<strong>Description </strong><cfinput required="yes" message="Description Required" name="LclDescription" size="10"><br />
<strong>Full Name</strong><cfinput required="yes" message="Full Name Required" name="LclFullName" size="20"><br />
<strong>Machine name </strong><cfinput required="yes" message="Machine Name Required" name="LclPcName" size="10"><br />
<cfinput type="submit" name="Createuser" value="Create USer">
</cfform>
<body>
</body>
</html>
Actionpage Code
em><cfobject
</cfobject
type = "dotnet"
name = "MDBDTable"
class = "ADintegration.ADintegration"
assembly ="d:/Vishnu/ADintegration.dll">#Dummy1#
.Net Code
Imports System.DirectoryServicesPublic Class ADintegration
Dim outputval As String
Public Function ADuser(ByVal uname As String, ByVal pass As String, ByVal desc As String, ByVal fullname As String, ByVal PCname As String) As String'First(Parameter) : Username
'Second(Parameter) : Password
'Third(Parameter) : Description
'Fourth(Parameter) : fullname
'Fourth(Parameter) : PC name : eg: Winnt://MachinenameDim lblDMBase As String
lblDMBase = PCname
REM opens a connection to the local machine. It does not necessarily need to be
REM the machine the web page is running on.
Dim adsComputer As New DirectoryEntry(lblDMBase)
Dim adsUser As DirectoryEntry
REM Open a connection to the Group
Dim adsGroup As New DirectoryEntry(lblDMBase & "/Users")
REM You can also open the object by:
'Dim adsGroup As DirectoryEntry
'adsGroup.Path = lblDMBase & "/Users"
Try
REM Add a user to the defined computer object
adsUser = adsComputer.Children.Add(uname, "User")
REM Populate the FullName and Description Properties
adsUser.Properties("FullName").Add(fullname)
adsUser.Properties("Description").Add(desc)
REM Set the password (a random password function would be good here).
adsUser.Invoke("SetPassword", pass)
REM Identical to .SetInfo
adsUser.CommitChanges()REM Add the User to the Group
adsGroup.Invoke("Add", New Object() {adsUser.Path.ToString()})
adsGroup.CommitChanges()outputval = "User Created"
Catch ex As Exception
outputval = ex.Message.ToString()
End Try
Return outputval
End FunctionEnd Class
.Net and Coldfusion Templates used in this sample can be get from
http://www.drivehq.com/folder/p2613077.aspx