{"id":158,"date":"2011-07-02T17:00:36","date_gmt":"2011-07-02T23:00:36","guid":{"rendered":"http:\/\/ryan.witschger.net\/?p=158"},"modified":"2011-07-02T17:00:36","modified_gmt":"2011-07-02T23:00:36","slug":"ad-without-quest-listing-servers-and-users","status":"publish","type":"post","link":"http:\/\/www.Get-Blog.com\/?p=158","title":{"rendered":"AD Without Quest: Listing Servers and Users"},"content":{"rendered":"<p>I\u2019ve decided to start an Active Directory series.  I have discovered that when you search online for any AD related code everything points back to the Quest tools.  I prefer to do everything natively and not rely on a third party tool kits that have to be installed.  As an admin, I understand that we do rely on our workstations, but when you start needing to install third party tools on server you are entering dangerous ground.  I wouldn\u2019t want to explain that Exchange went down because I installed some tool that locked the server.<\/p>\n<p>I have been using PowerShell since its inception and I still haven\u2019t found an instance where I need to use the Quest tools.  Sometimes it might be easier, but once you\u2019ve created a well written function, it\u2019s easy to copy and paste it into your next script.  So I am going to base this series off of individual functions that can be added to scripts to work with AD.<\/p>\n<p>We\u2019ll start with the basic ones.  The following functions can be used to pull all the server names and user name out of Active Directory.  This can be modified to pull whatever property you\u2019d like, but I find that either account name or ADSPath (the LDAP string) are the most useful.  I use some variation of these in almost every script I make.<\/p>\n<p>First, let\u2019s look at pulling all AD servers.  Now these are only for the domain that you are running it from.  There is some extra work to do to change domains, but I\u2019ll cover that in a later blog post.  The basic idea is to create a directory searcher, then set a filter (or you will return every object in AD).  Then you need to set the page size.  That, basically, tells the searcher how many objects it can return in a single search, so if you set it to 1000 and you have 8000 total objects, it will run a total of 8 searches.  This step can be skipped if you have a very small domain, but AD has a built in limit to how many objects can be returned in a single search.  This value can be set by the admin, but 1000 works with default domains.  Finally, you run the search and then return the property you want from the function.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\">&nbsp;<span class=\"kw3\">Function<\/span> Get<span class=\"sy0\">-<\/span>ServerList<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">$Searcher<\/span><span class=\"sy0\">=<\/span><span class=\"kw1\">New-Object<\/span> System.DirectoryServices.DirectorySearcher<br \/>\n&nbsp; &nbsp; <span class=\"re0\">$Searcher<\/span>.<span class=\"kw3\">Filter<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;(&amp;(objectcategory=computer)(operatingSystem=*Server*))&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">$Searcher<\/span>.PageSize <span class=\"sy0\">=<\/span> <span class=\"nu0\">1000<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">$Results<\/span><span class=\"sy0\">=<\/span><span class=\"re0\">$Searcher<\/span>.FindAll<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"re0\">$Results<\/span> <span class=\"sy0\">|<\/span> <span class=\"sy0\">%<\/span> <span class=\"br0\">&#123;<\/span><a href=\"about:blank\"><span class=\"kw6\">$_<\/span><\/a>.Properties.name<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>So, that\u2019s it.  You can drop this into your script and then run it with <code class=\"codecolorer powershell default\"><span class=\"powershell\"><span class=\"re0\">$servers<\/span> <span class=\"sy0\">=<\/span> Get<span class=\"sy0\">-<\/span>ServerList<\/span><\/code> to get a list of all your server names.  This function runs very quickly and can be put into any script to make things really easily.<\/p>\n<p>However, part of what I am trying to get across is that these functions can be quickly changed to suite your needs.  So this is the function very slightly changed to pull all the users instead.  In this case the PageSize parameter really comes into use.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\">&nbsp;<span class=\"kw3\">Function<\/span> Get<span class=\"sy0\">-<\/span>UserList<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">$Searcher<\/span><span class=\"sy0\">=<\/span><span class=\"kw1\">New-Object<\/span> System.DirectoryServices.DirectorySearcher<br \/>\n&nbsp; &nbsp; <span class=\"re0\">$Searcher<\/span>.<span class=\"kw3\">Filter<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;(objectcategory=user)&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">$Searcher<\/span>.PageSize <span class=\"sy0\">=<\/span> <span class=\"nu0\">1000<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">$Results<\/span><span class=\"sy0\">=<\/span><span class=\"re0\">$Searcher<\/span>.FindAll<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"re0\">$Results<\/span> <span class=\"sy0\">|<\/span> <span class=\"sy0\">%<\/span> <span class=\"br0\">&#123;<\/span><a href=\"about:blank\"><span class=\"kw6\">$_<\/span><\/a>.Properties.samaccountname<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>More AD entries to come!  You can find them via my blogs tags to the right.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve decided to start an Active Directory series. I have discovered that when you search online for any AD related code everything points back to the Quest tools. I prefer to do everything natively and not rely on a third party tool kits that have to be installed. As an admin, I understand that we <a href='http:\/\/www.Get-Blog.com\/?p=158' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[6,4],"tags":[],"_links":{"self":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts\/158"}],"collection":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=158"}],"version-history":[{"count":0,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts\/158\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}