{"id":68123,"date":"2006-01-23T16:48:00","date_gmt":"2006-01-23T16:48:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/01\/23\/how-can-i-add-a-user-to-a-group-but-only-if-that-user-is-a-member-of-the-it-department\/"},"modified":"2006-01-23T16:48:00","modified_gmt":"2006-01-23T16:48:00","slug":"how-can-i-add-a-user-to-a-group-but-only-if-that-user-is-a-member-of-the-it-department","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-add-a-user-to-a-group-but-only-if-that-user-is-a-member-of-the-it-department\/","title":{"rendered":"How Can I Add a User to a Group, but Only if that User is a Member of the IT Department?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I add a user to a group, but only if that user is a member of the IT department?<BR><BR>&#8212; JV<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, JV. You know, this is your lucky day: not only are we going to show you how you can add a specified user to a group (assuming, of course, that this user is a member of the IT department), but we\u2019re also going to show you a way to automatically add <I>all<\/I> members of the IT department to a group. Talk about a deal, huh?<\/P>\n<P>And, no, your thanks are enough for us. Well, that is, your thanks and our standard consulting fee of $12,343.50. We\u2019ll send you a bill.<\/P>\n<P>So what do you get in return for that $12,343.50? (And no, you don\u2019t <I>have<\/I> to send us $12,343.50. Not unless you <I>want<\/I> to \u2026.) Well, for starters, you get a script like this one, which adds the user Jack Richins to the group IT Staff \u2026 provided, of course, that Jack is a member of the IT department:<\/P><PRE class=\"codeSample\">Set objUser = GetObject(&#8220;LDAP:\/\/cn=Jack Richins,ou=canada,dc=fabrikam,dc=com&#8221;)<\/p>\n<p>If objUser.Department = &#8220;IT&#8221; Then\n    Set objGroup = GetObject _\n        (&#8220;LDAP:\/\/cn=IT Staff,ou=support,dc=fabrikam,dc=com&#8221;)\n    objGroup.Add(objUser.ADsPath)\nEnd If\n<\/PRE>\n<P>As you can see, this is a simple little script. We begin by using this line of code to bind directly to Jack Richins\u2019 user account in Active Directory:<\/P><PRE class=\"codeSample\">Set objUser = GetObject(&#8220;LDAP:\/\/cn=Jack Richins,ou=canada,dc=fabrikam,dc=com&#8221;)\n<\/PRE>\n<P>We then check to see whether or not Jack\u2019s <B>Department<\/B> attribute is equal to IT:<\/P><PRE class=\"codeSample\">If objUser.Department = &#8220;IT&#8221; Then\n<\/PRE>\n<P>Let\u2019s assume that it is. In that case, we then create a second object reference, one that connects us to the IT Staff group account:<\/P><PRE class=\"codeSample\">Set objGroup = GetObject _\n    (&#8220;LDAP:\/\/cn=IT Staff,ou=support,dc=fabrikam,dc=com&#8221;)\n<\/PRE>\n<P>Once we\u2019ve made that connection we can then call the <B>Add<\/B> method (passing the value of Jack\u2019s <B>ADsPath<\/B> attribute as the sole parameter) and add Jack to the group. If Jack <I>isn\u2019t<\/I> part of the IT department then we don\u2019t do anything at all.<\/P>\n<P>Not bad, huh? Now here\u2019s the bonus script. This script searches Active Directory and returns a list of all the users (<B>objectCategory=&#8217;user&#8217;<\/B>) who happen to be members of the IT department (<B>Department<\/B>=<B>\u2019IT\u2019<\/B>). For each user meeting those criteria (that is, each user in the IT department), the script adds the user to the IT Staff group:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Const ADS_SCOPE_SUBTREE = 2<\/p>\n<p>Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)\nSet objCommand =   CreateObject(&#8220;ADODB.Command&#8221;)\nobjConnection.Provider = &#8220;ADsDSOObject&#8221;\nobjConnection.Open &#8220;Active Directory Provider&#8221;\nSet objCommand.ActiveConnection = objConnection<\/p>\n<p>objCommand.Properties(&#8220;Page Size&#8221;) = 1000\nobjCommand.Properties(&#8220;Searchscope&#8221;) = ADS_SCOPE_SUBTREE <\/p>\n<p>objCommand.CommandText = _\n    &#8220;SELECT ADsPath FROM &#8216;LDAP:\/\/dc=fabrikam,dc=com&#8217; WHERE objectCategory=&#8217;user&#8217; &#8221; &amp; _\n         &#8220;AND Department=&#8217;IT'&#8221;\nSet objRecordSet = objCommand.Execute<\/p>\n<p>objRecordSet.MoveFirst<\/p>\n<p>Set objGroup = GetObject _\n    (&#8220;LDAP:\/\/cn=IT Staff,ou=support,dc=fabrikam,dc=com&#8221;)<\/p>\n<p>Do Until objRecordSet.EOF\n    objGroup.Add(objRecordSet.Fields(&#8220;ADsPath&#8221;).Value)\n    objRecordSet.MoveNext\nLoop\n<\/PRE>\n<P>We\u2019re not going to talk about the bonus script in any detail; if you aren\u2019t sure how Active Directory scripts work we recommend you take a peek at the two-part <I>Tales from the Script<\/I> series <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/tales\/sg0405.mspx\"><B>Dude, Where\u2019s My Printer?<\/B><\/A> About all we <I>will<\/I> do is mention that the <B>On Error Resume Next<\/B> statement is very important in this particular script. Why? Well, suppose Jack Richins is already a member of the IT Staff group and you try adding him (again) to the group. That\u2019s going to generate an error and the script will blow up. If you add the On Error Resume Next statement, however, the script won\u2019t blow up; instead, it will simply skip Jack and instead try adding the next user in the IT department.<\/P>\n<TABLE id=\"EYE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. If you\u2019re wondering how the Scripting Guys came up with a standard consulting fee of $12,343.50, well, it\u2019s purely coincidental that one of the Scripting Guys (with great reluctance) recently purchased a car for his son and the total bill came to $12,343.50. Like we said, purely coincidental \u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I add a user to a group, but only if that user is a member of the IT department?&#8212; JV Hey, JV. You know, this is your lucky day: not only are we going to show you how you can add a specified user to a group (assuming, of course, [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,19,3,8,5],"class_list":["post-68123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-activex-data-objects-ado","tag-scripting-guy","tag-searching-active-directory","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I add a user to a group, but only if that user is a member of the IT department?&#8212; JV Hey, JV. You know, this is your lucky day: not only are we going to show you how you can add a specified user to a group (assuming, of course, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=68123"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68123\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=68123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}