Simple T-SQL stored procedure to add user account with full name on a specified computer.
Manage users with SQL server | |
---|---|
CREATE PROCEDURE sp_AddUser(@UserName VARCHAR(30), @Password VARCHAR(30), @FullName VARCHAR(255) = NULL, @GroupName VARCHAR(255) = NULL) AS DECLARE @result INT, @OLEResult INT, @UserID INT, @ServerID INT, @UsersID INT SELECT @result = 0 --Create UserManager object EXECUTE @OLEResult = sp_OACreate 'UserManager.Server', @ServerID OUT IF @OLEResult <> 0 SELECT @result = @OLEResult IF @OLEResult <> 0 RAISERROR 50000 'CreateObject' --Create Select domain controler to add user --EXEC @OLEResult = sp_OAMethod @ServerID, 'Select', Null, 'DomainControler' IF @OLEResult <> 0 SELECT @result = @OLEResult IF @OLEResult <> 0 RAISERROR 50001 'SelectDC' --Get Users collection EXEC @OLEResult = sp_OAGetProperty @ServerID, 'Users', @UsersID OUT IF @OLEResult <> 0 SELECT @result = @OLEResult IF @OLEResult <> 0 RAISERROR 50002 'Users' --Add the user to Users collection EXEC @OLEResult = sp_OAMethod @UsersID, 'Add', @UserID OUT, @UserName, @Password IF @OLEResult <> 0 SELECT @result = @OLEResult IF @OLEResult <> 0 RAISERROR 50003 'UserAdd' IF ''+@FullName<>'' BEGIN--Set user full name EXEC @OLEResult = sp_OASetProperty @UserID, 'FullName', @FullName IF @OLEResult <> 0 SELECT @result = @OLEResult IF @OLEResult <> 0 RAISERROR 50004 'FullName' END IF ''+@GroupName<>'' BEGIN--Add the user to defined group EXEC @OLEResult = sp_OAMethod @UserID, 'AddToLocalGroup', NULL, @GroupName IF @OLEResult <> 0 SELECT @result = @OLEResult IF @OLEResult <> 0 RAISERROR 50005 'Group' END EXEC @OLEResult = sp_OADestroy @UserID EXEC @OLEResult = sp_OADestroy @UsersID EXEC @OLEResult = sp_OADestroy @ServerID RETURN @result GO DECLARE @result INT EXECUTE @result = sp_AddUser 'JohnS', '123456', 'John Smitko', 'Administrators' |
ActiveX UserManager is easy to use COM component with classes to create, delete and manage local and remote user accounts, groups, servers and domains in the Windows NT (2000/XP) environment. You can simple work with user accounts and groups from VBA, VBS/JS, ASP and T-SQL programming environment.
© 1996 - 2009 Antonin Foller, Motobit Software | About, Contacts | e-mail: info@pstruh.cz