{"id":69503,"date":"2005-06-28T21:12:00","date_gmt":"2005-06-28T21:12:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/06\/28\/how-can-i-list-all-the-attributes-used-by-the-computer-class-in-active-directory\/"},"modified":"2005-06-28T21:12:00","modified_gmt":"2005-06-28T21:12:00","slug":"how-can-i-list-all-the-attributes-used-by-the-computer-class-in-active-directory","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-list-all-the-attributes-used-by-the-computer-class-in-active-directory\/","title":{"rendered":"How Can I List All the Attributes Used by the Computer Class in Active Directory?"},"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 list all the attributes used by the Computer class in Active Directory?<BR><BR>&#8212; KP<\/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, KP. Thanks for your question. In response, yes, it <I>is<\/I> true that the Kirkland Fire, the Colt League baseball team coached by one of the Scripting Guys, won the city championship this past weekend, nicely bookending the regular-season championship which the team had already clinched. And, yes, the final score of the title game really <I>was<\/I> 13-0. Anything else we can help you with?<\/P>\n<P>Oh, right: your <I>scripting<\/I> question. (For those of you unfamiliar with scripting, it\u2019s something we do to pass the time between baseball games.) Obviously one of the Scripting Guys isn\u2019t exactly focused on doing his job today (though we don\u2019t know why today should be an exception) but fortunately this is an easy question to answer: after all, one of the really cool things about ADSI (the scripting technology used to access Active Directory) is that you can write an ADSI script that will tell you what you can script using ADSI. For example, this script will return the names of all the attributes (properties) used by the Computer class:<\/P><PRE class=\"codeSample\">Set objSchema = GetObject(&#8220;LDAP:\/\/schema\/computer&#8221;)<\/p>\n<p>Wscript.Echo &#8220;Mandatory attributes&#8221;<\/p>\n<p>For Each strAttribute in objSchema.MandatoryProperties\n    Wscript.Echo strAttribute\nNext<\/p>\n<p>Wscript.Echo<\/p>\n<p>Wscript.Echo &#8220;Optional attributes&#8221;<\/p>\n<p>For Each strAttribute in objSchema.OptionalProperties\n    Wscript.Echo strAttribute\nNext\n<\/PRE>\n<P>The script begins by connecting to the schema for the local domain and then binding to the Computer class. (The schema, of course, is a master list that details all the classes and other objects that can be stored in Active Directory, along with all the properties and methods of those items.) When we do this, we get back an instance of the IADsClass object; as the name implies, IADsClass contains information about a particular Active Directory class. What if we wanted information about the User class? Then we would bind to that class instead:<\/P><PRE class=\"codeSample\">Set objSchema = GetObject(&#8220;LDAP:\/\/schema\/user&#8221;)\n<\/PRE>\n<P>And if we wanted information about the organizationalUnit class we\u2019d use this code:<\/P><PRE class=\"codeSample\">Set objSchema = GetObject(&#8220;LDAP:\/\/schema\/organizationalUnit&#8221;)\n<\/PRE>\n<P>Yes, very cool.<\/P>\n<P>Because IADsClass is a class, it has its own set of attributes, including these two: <B>MandatoryProperties<\/B> and <B>OptionalProperties<\/B>. MandatoryProperties represents the properties that each instance of the class (in this case, the Computer class) must have. When we run the script against Active Directory we get back the following list of mandatory properties:<\/P><PRE class=\"codeSample\">cn\ninstanceType\nnTSecurityDescriptor\nobjectCategory\nobjectClass\nobjectSid\nsAMAccountName\n<\/PRE>\n<P>Most of these properties are automatically assigned by Active Directory, but a couple of them &#8211; like the <B>cn<\/B> and the <B>sAMAccountName<\/B> are not. That means you have to assign these yourself when creating a new Computer object; if you leave out, say, the sAMAccountName, then your script will fail.<\/P>\n<P>As you might expect, MandatoryProperties are stored as an array (because there can be &#8211; and will be &#8211; more than one mandatory property); therefore we use a For Each loop to cycle through all the properties and echo back each property name:<\/P><PRE class=\"codeSample\">For Each strAttribute in objSchema.MandatoryProperties\n    Wscript.Echo strAttribute\nNext\n<\/PRE>\n<P>That\u2019s pretty much it. We do the same sort of thing for OptionalProperties (properties that a Computer object <I>can<\/I> have but doesn\u2019t <I>have<\/I> to have) and we\u2019re done.<\/P>\n<P>Incidentally, when you look through the list of optional properties for a computer you\u2019ll see all sorts of oddball items. Pager? Home phone number? How many computers have their own pager or home phone number? Believe it or not, that\u2019s perfectly normal: as it turns out, the Computer class is derived from the User class. That means the Computer class has all the properties of the User class plus a few other properties &#8211; such as operating system version &#8211; specific to computers. If you see some crazy attributes associated with a computer &#8211; initials? secretary? &#8211; well, don\u2019t worry about them. After all, as optional properties you don\u2019t have to do anything with them if you don\u2019t want to.<\/P>\n<P>Hope that helps, KP. Now, who wants to hear more about the Kirkland Fire? Anybody? Hello? Hello?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I list all the attributes used by the Computer class in Active Directory?&#8212; KP Hey, KP. Thanks for your question. In response, yes, it is true that the Kirkland Fire, the Colt League baseball team coached by one of the Scripting Guys, won the city championship this past weekend, nicely [&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,46,3,5],"class_list":["post-69503","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-computer-accounts","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I list all the attributes used by the Computer class in Active Directory?&#8212; KP Hey, KP. Thanks for your question. In response, yes, it is true that the Kirkland Fire, the Colt League baseball team coached by one of the Scripting Guys, won the city championship this past weekend, nicely [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69503","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=69503"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69503\/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=69503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}