How to add SMS 2003/ConfigMgr 2007 computers in a collection to an Active Directory security group

Received an interesting question recently from myITForum regular Joshua Searles. Was it possible to use SQL Server stored procedure to directly add computer names in a collection to a security group in Active Directory?  I advised that Josh could take advantage of the SQL Server extended stored procedure xp_CmdShell to enumerate the collection, then pass the computer name to a VBScript, PowerShell or possibly the NET GROUP command using xp_CmdShell. 

Josh ran with those ideas and created the following batch solution, which could be adapted to a stored procedure.

(in his own words)

Basically I have a collection of systems that are on slow wan links that I want to add to a Security Group that applies a “BITS Throttling Policy”.

I’ll run it as a stored procedure, so that we’re sure to apply the policy to any system that gets deployed to those subnets.  Next will be a script to remove the machine from the group, when it is no longer a member of the collection.

‘************************************************************************************

create table #tmp_name_list (HostNames varchar(20))

insert into #tmp_name_list

SELECT Name FROM dbo.v_CM_RES_COLL_SMS00001

SELECT * FROM #tmp_name_list

DECLARE @host_name sysname

DECLARE host_list cursor

                for select HostNames

                                                FROM #tmp_name_list

                for read only

declare @COMMAND sysname

open host_list

fetch next from host_list into @host_name

while @@fetch_status=0

                                begin

                                                SET @COMMAND = 'NET GROUP "BITS Throttling" ' + @host_name + '$'+' /ADD /DOMAIN'                         

                                                exec.master.dbo.xp_cmdshell @COMMAND , NO_OUTPUT

                                                fetch next from host_list into @host_name

                                                PRINT @COMMAND

                                                end

close host_list

deallocate host_list

drop table #tmp_name_list                       

‘*****************************************************************

ST Note: Be aware that there are known security concerns with xp_CmdShell, pay particular attention to the account that you give “Exec” permissions to this command.  More can be found here: http://msdn.microsoft.com/en-us/library/aa175398(SQL.80).aspx

Published Thursday, January 28, 2010 9:39 AM by sthompson

Comments

# re: How to add SMS 2003/ConfigMgr 2007 computers in a collection to an Active Directory security group

Thursday, January 28, 2010 12:01 PM by jsearles

Since I'm an SMS guy, rather than create a stored procedure in SQL, I just created a custom SQL Command under site maintenance, and pasted the code in there.  And since I'm in advanced security mode, I believe it will run the command under the site servers computer account. :-)  Thanks for the post!

# re: How to add SMS 2003/ConfigMgr 2007 computers in a collection to an Active Directory security group

Thursday, January 28, 2010 1:49 PM by jsearles

It should be noted that this worked in my test environment because my TEST Lab SQL server is also my domain controller(it's a test vm cut me slack).  The NET GROUP command must be ran from a domain controller.  The concept is there, and I'll have another one that works on NON domain controllers soon.

# re: How to add SMS 2003/ConfigMgr 2007 computers in a collection to an Active Directory security group

Thursday, January 28, 2010 3:09 PM by sthompson

You don't have to run the NET GROUP command on a domain controller. However, the account that invokes it needs to be able to add objects to Active Directory.  By default an account with administrative rights would work. However, you should be able to find the minimum permissions needed to achieve this by setting specific rights to AD here: technet.microsoft.com/.../cc728117(WS.10).aspx

# re: How to add SMS 2003/ConfigMgr 2007 computers in a collection to an Active Directory security group

Thursday, January 28, 2010 7:05 PM by jsearles

Well, isn't that peachy.  When you run the entire command from the command line, it works, but if you just run NET GROUP it produces errors.  I just spent about 6 hours changing the code, so that it uses powershell instead of the NET command.  Oh well!

# re: How to add SMS 2003/ConfigMgr 2007 computers in a collection to an Active Directory security group

Monday, February 01, 2010 2:14 PM by sthompson

See the follow-up article here:

www.myitforum.com/.../Articles.aspx

Powered by Community Server (Commercial Edition), by Telligent Systems