{"id":68743,"date":"2005-10-14T21:41:00","date_gmt":"2005-10-14T21:41:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/10\/14\/how-can-i-add-a-user-to-a-group-if-that-user-belongs-to-two-other-groups\/"},"modified":"2005-10-14T21:41:00","modified_gmt":"2005-10-14T21:41:00","slug":"how-can-i-add-a-user-to-a-group-if-that-user-belongs-to-two-other-groups","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-add-a-user-to-a-group-if-that-user-belongs-to-two-other-groups\/","title":{"rendered":"How Can I Add a User to a Group if That User Belongs to Two Other Groups?"},"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 check to see if a user is in both group A and group B and, if so, add that user to Group C?<BR><BR>&#8212; DH<\/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, DH. Good heavens, how are we supposed to know <I>that<\/I>?!? Sheesh.<\/P>\n<P>Sorry; we just always wanted to say that. Now that we have that out of our system, here\u2019s a sample script that checks to see if Ken Myer belongs to both the Finance Users and the Fabrikam Managers groups in Active Directory. If he <I>does<\/I> belong to both groups, then the script adds him to a third group, Finance Managers (if A and B, then C):<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Set objUser = GetObject(&#8220;LDAP:\/\/cn=Ken Myer,ou=Finance,dc=fabrikam,dc=com&#8221;)<\/p>\n<p>i = 0<\/p>\n<p>For Each strGroup in objUser.memberOf\n    Set objGroup = GetObject(&#8220;LDAP:\/\/&#8221; &amp;  strGroup)\n    If objGroup.CN = &#8220;Finance Users&#8221; Then\n        i = i + 1\n    End If\n    If objGroup.CN = &#8220;Fabrikam Managers&#8221; Then\n        i = i + 1\n    End If\nNext<\/p>\n<p>If i = 2 Then\n    Set objGroup = GetObject(&#8220;LDAP:\/\/cn=Finance Managers,ou=Finance,dc=fabrikam,dc=com&#8221;)\n    objGroup.Add(objUser.ADsPath)\nEnd If\n<\/PRE>\n<P>The script begins by creating an object reference to the Ken Myer user account in Active Directory. We then assign the value 0 to a counter variable named <I>i<\/I>; we\u2019ll use this variable to keep track of how many of our two target groups Ken belongs to.<\/P>\n<P>As it turns out, group membership in Active Directory is stored in a multi-valued attribute named <B>memberOf<\/B>. With that in mind, we use this line of code to walk through the collection of groups that Ken Myer is a member of:<\/P><PRE class=\"codeSample\">For Each strGroup in objUser.memberOf\n<\/PRE>\n<P>As it <I>further<\/I> turns out, the memberOf attribute returns the distinguished name of each group Ken Myer belongs to. That\u2019s nice, but the distinguished name looks something like this:<\/P><PRE class=\"codeSample\">cn=Finance Users,ou=Finance,dc=fabrikam,dc=com\n<\/PRE>\n<P>Needless to say, we\u2019re used to dealing with groups by name (e.g., Finance Users); most likely we have no idea where the group account is stored in Active Directory. Therefore, we don\u2019t even bother checking the distinguished name; instead, we use that value to connect to the group account itself. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">Set objGroup = GetObject(&#8220;LDAP:\/\/&#8221; &amp;  strGroup)\n<\/PRE>\n<P>Once we connect to the group account we can then use code like this to check the name (CN) of the group, something a bit easier and a bit more intuitive:<\/P><PRE class=\"codeSample\">If objGroup.CN = &#8220;Finance Users&#8221; Then\n<\/PRE>\n<P>As we loop through the groups, we\u2019re checking to see if any of those groups have a CN equal to <I>Finance Users<\/I> (one of our two target groups). What if one of those groups <I>does<\/I> have a CN equal to <I>Finance Users<\/I>? Well, in that case we increment the value of <I>i<\/I> by 1:<\/P><PRE class=\"codeSample\">i = i + 1\n<\/PRE>\n<P>Pretty fancy coding, huh?<\/P>\n<P>Meanwhile, we have a similar block of code that checks to see if the group has a CN equal to <I>Fabrikam Managers<\/I>. If it does, then we again increment the value of our counter variable by 1:<\/P><PRE class=\"codeSample\">If objGroup.CN = &#8220;Fabrikam Managers&#8221; Then\n    i = i + 1\nEnd If\n<\/PRE>\n<P>We then loop around and check the next group in the collection.<\/P>\n<P>Why do we increment the value of our counter variable? Well, if Ken doesn\u2019t belong to either of our target groups then i will never be changed and will thus equal 0. If Ken belongs to one group, but not the other, then i will be changed one time, and thus be equal to 1. So what does it mean if i is equal to 2? You got it: Ken must be a member of both Finance Users and Fabrikam Managers (because the value if i was incremented twice). Consequently, we want to add Ken to our third group, Finance Managers:<\/P><PRE class=\"codeSample\">If i = 2 Then\n    Set objGroup = GetObject(&#8220;LDAP:\/\/cn=Finance Managers,ou=Finance,dc=fabrikam,dc=com&#8221;)\n    objGroup.Add(objUser.ADsPath)\nEnd If\n<\/PRE>\n<P>Nothing too fancy here: we create an object reference to the Finance Managers group, then use the <B>Add<\/B> method to add Ken Myer. Note that when calling the Add method we pass the value <B>objUser.ADsPath<\/B>: that\u2019s the ADsPath to the Ken Myer user account in Active Directory.<\/P>\n<P>Two quick notes regarding this script. First, it\u2019s possible that you could have multiple groups with the same CN; if that\u2019s the case, then examining the value of the CN attribute won\u2019t do you much good. Instead, you\u2019ll need to look at the <B>sAMAccountName<\/B> attribute, which must be unique within the domain. (Of course, the sAMAccountName is going to be something along the lines of <I>fabmgrs<\/I>, another value you typically don\u2019t know off the top of your head.)<\/P>\n<P>Second, this script doesn\u2019t deal with nested groups: if Ken is a member of a group which is a member of a group which is a member of Fabrikam Managers, well, then Ken is also a member of Fabrikam Managers. However, this script doesn\u2019t deal with situations like that. Why? Because nested groups can get a bit messy, and they require a level of explanation that lies outside the scope of this column. However, you can find a sample script (and an accompanying explanation) for dealing with nested groups in this <A href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=24419\" target=\"_blank\"><B>Scripting Guys Webcast<\/B><\/A>.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I check to see if a user is in both group A and group B and, if so, add that user to Group C?&#8212; DH Hey, DH. Good heavens, how are we supposed to know that?!? Sheesh. Sorry; we just always wanted to say that. Now that we have that [&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,9,44,3,20,198,5],"class_list":["post-68743","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-adsi","tag-groups","tag-scripting-guy","tag-user-accounts","tag-users","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I check to see if a user is in both group A and group B and, if so, add that user to Group C?&#8212; DH Hey, DH. Good heavens, how are we supposed to know that?!? Sheesh. Sorry; we just always wanted to say that. Now that we have that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68743","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=68743"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68743\/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=68743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}