{"id":69913,"date":"2005-04-29T18:52:00","date_gmt":"2005-04-29T18:52:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/04\/29\/how-can-i-change-the-user-and-computer-account-description-attributes-each-time-a-user-logs-on\/"},"modified":"2005-04-29T18:52:00","modified_gmt":"2005-04-29T18:52:00","slug":"how-can-i-change-the-user-and-computer-account-description-attributes-each-time-a-user-logs-on","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-change-the-user-and-computer-account-description-attributes-each-time-a-user-logs-on\/","title":{"rendered":"How Can I Change the User and Computer Account Description Attributes Each Time a User Logs On?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I change the user and computer account Description attributes each time a user logs on? I\u2019d like the Description to indicate who logged on, and when.<BR><BR>&#8212; GG<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, GG. This is actually a pretty good idea. One question we get asked all the time is this: \u201cHow can I get a list of all my computers and who\u2019s logged on to them?\u201d The truth is, there really isn\u2019t a good way to do that. The answer to today\u2019s question is a nice solution to that problem: all you\u2019d have to do is query Active Directory and return a list of computers (or users) and the <B>Description<\/B> attribute. That would give you your list right there.<\/P>\n<P>A couple caveats before we begin. First, you\u2019ll probably want to configure this as a logon script; you\u2019re on your own for that. Second, you\u2019ll have to make sure that all your users have permissions to change the <B>Description<\/B> attribute for both their own account and for the computer account. That\u2019s likely the case anyway, but you\u2019ll need to double-check it before implementing this solution.<\/P>\n<P>OK, let\u2019s take a look at the script that &#8211; each time a user logs on &#8211; changes the Description attribute for both the user account and the computer account in Active Directory:<\/P><PRE class=\"codeSample\">Set objSysInfo = CreateObject(&#8220;ADSystemInfo&#8221;)<\/p>\n<p>Set objUser = GetObject(&#8220;LDAP:\/\/&#8221; &amp; objSysInfo.UserName)\nSet objComputer = GetObject(&#8220;LDAP:\/\/&#8221; &amp; objSysInfo.ComputerName)<\/p>\n<p>strMessage = objUser.CN &amp; &#8221; logged on to &#8221; &amp; objComputer.CN &amp; &#8221; &#8221; &amp; Now &amp; &#8220;.&#8221;<\/p>\n<p>objUser.Description = strMessage\nobjUser.SetInfo<\/p>\n<p>objComputer.Description = strMessage\nobjComputer.SetInfo\n<\/PRE>\n<P>We begin by creating an instance of <B>ADSystemInfo<\/B>, an Active Directory class that returns a lot of useful information about the current user and computer. (For a script showing all the data that can be returned using ADSystemInfo click <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_srv_gwqy.mspx\" target=\"_blank\"><B>here<\/B><\/A>.) We then use the <B>UserName<\/B> and <B>ComputerName<\/B> properties of this object to create a pair of object references, one (objUser) that binds us to the logged-on user\u2019s Active Directory account, the other (objComputer) that binds us to the computer\u2019s Active Directory account.<\/P>\n<P>Next we create the string that we want to write to the Description attribute. Obviously you can write anything you want to this attribute; we chose to combine the user\u2019s CN, the computer\u2019s CN, and the current date and time (plus a few additional words just to turn the thing into a sentence). That\u2019s what this code is all about:<\/P><PRE class=\"codeSample\">strMessage = objUser.CN &amp; &#8221; logged on to &#8221; &amp; objComputer.CN &amp; &#8221; &#8221; &amp; Now &amp; &#8220;.&#8221;\n<\/PRE>\n<P>Ultimately, that results in a description similar to this:<\/P><PRE class=\"codeSample\">Ken Myer logged on to atl-ws-01 4\/25\/2005 8:04:54 AM\n<\/PRE>\n<P>So how do we actually get the Description set to this string? Well, to set the Description for the logged-on user we use these two lines of code:<\/P><PRE class=\"codeSample\">objUser.Description = strMessage\nobjUser.SetInfo\n<\/PRE>\n<P>All this does is configure the Description on the local cache copy of the user account, and then use the <B>SetInfo<\/B> method to write that information back to Active Directory. (Have no idea what we mean by the local cache copy? See <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_ads_yrig.mspx\" target=\"_blank\"><B>this section<\/B><\/A> of the Microsoft Windows 2000 Scripting Guide for more information.) We then use similar code to set the Description for the computer account:<\/P><PRE class=\"codeSample\">objComputer.Description = strMessage\nobjComputer.SetInfo\n<\/PRE>\n<P>One thing you might want to do as well is include a logoff script that either erases the Description or changes the message (e.g., Ken Myer logged on to atl-ws-01 4\/25\/2005 8:04:54 AM). That way you can not only keep track of which users are logged on to which computers, but you can also keep track of which users are <I>not<\/I> logged on at all (as well as which computers have no current user).<\/P>\n<P>Good idea, GG. We only wish <I>we\u2019d<\/I> thought of it!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I change the user and computer account Description attributes each time a user logs on? I\u2019d like the Description to indicate who logged on, and when.&#8212; GG Hey, GG. This is actually a pretty good idea. One question we get asked all the time is this: \u201cHow can I get [&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,20,5],"class_list":["post-69913","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-computer-accounts","tag-scripting-guy","tag-user-accounts","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I change the user and computer account Description attributes each time a user logs on? I\u2019d like the Description to indicate who logged on, and when.&#8212; GG Hey, GG. This is actually a pretty good idea. One question we get asked all the time is this: \u201cHow can I get [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69913","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=69913"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69913\/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=69913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}