{"id":5123,"date":"2012-08-15T00:01:00","date_gmt":"2012-08-15T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/08\/15\/use-powershell-to-query-ad-ds-for-servers-and-then-find-hotfixes\/"},"modified":"2012-08-15T00:01:00","modified_gmt":"2012-08-15T00:01:00","slug":"use-powershell-to-query-ad-ds-for-servers-and-then-find-hotfixes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-query-ad-ds-for-servers-and-then-find-hotfixes\/","title":{"rendered":"Use PowerShell to Query AD DS for Servers and then Find Hotfixes"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, talks about querying the AD DS module via PowerShell and using the results to find hotfixes.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! &nbsp;We have a server running Windows Server&nbsp;2008&nbsp;R2 that is acting as one of our domain controllers. I do not have access to install the Remote Server Administration Tools (RSAT) on my workstation. However, I would like to use the Active Directory Domain Services (AD DS) cmdlets to return a listing of all servers on the network. I need to check the status of the most recent hotfix installed on all the servers. I do not have a list of servers because we are constantly adding and removing virtual servers based on load and demand, so Active Directory is the only place I feel comfortable in obtaining this information. Can you help?<\/p>\n<p>&mdash;BB<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello BB,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. There are over 100 people already signed up for <a href=\"http:\/\/powershellsaturday.com\/002\/\" target=\"_blank\">PowerShell Saturday in Charlotte, North Carolina<\/a> on September 15, 2012. The tickets are fast disappearing, so if you want to attend this event, you should register before the event sells completely out. By the way, there are several sessions that talk about using Windows PowerShell with Active Directory on the agenda. <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=Ashley%20mcglone&amp;sections=7618\" target=\"_blank\">Microsoft PFE, Ashley McGlone<\/a> will be making an excellent presentation for this track.<\/p>\n<h2>Use Windows PowerShell remoting to load the module<\/h2>\n<p>If you are using Windows PowerShell&nbsp;2.0 and you have access to the Active Directory module, there are several ways to load and use the module. In a particular scenario where I only need a list of server names, it is easiest to use the <b>Invoke-Command<\/b> cmdlet and store the returned information in a variable. The command shown here accomplishes this task.<\/p>\n<p style=\"padding-left: 30px\">$cred = Get-Credential iammred\\administrator<\/p>\n<p style=\"padding-left: 30px\">$servers = Invoke-Command -cn dc3 -cred $cred -script {import-module ActiveDirectory;<\/p>\n<p style=\"padding-left: 30px\">Get-ADComputer -LDAPFilter &#8220;(&amp;(objectcategory=computer)(OperatingSystem=*server*))&#8221;}<\/p>\n<p>To verify that the command worked properly and that I have an array of server names, I access the <b>count<\/b><i> <\/i>property. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">$servers.Count<\/p>\n<p>In Windows PowerShell&nbsp;3.0, I can directly access the server names by using the <b>name<\/b><i> <\/i>property as shown here.<\/p>\n<p style=\"padding-left: 30px\">$servers.name<\/p>\n<p>But in Windows PowerShell 2.0, I need to use a <b>ForEach-Object<\/b> loop. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">$servers | foreach {$_.name}<\/p>\n<p>The commands and the output associated with the commands are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1526.HSG-8-15-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1526.HSG-8-15-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Use Invoke-Command to do the query<\/h2>\n<p>When I know that I have my list of servers, it is simple to query to find the latest hotfix. I can use the <b>computer<\/b><i> <\/i>parameter of the <b>Get-Hotfix<\/b> cmdlet to do the query, but that presupposes that a large number of ports on the remote server are open, and that simply might not be the case. In Windows Server 2012, Windows PowerShell remoting is on by default, and if you are running Windows PowerShell in your environment, you should turn it on for your other servers (after the appropriate security review of course). I like using WinRM instead of RPC or DCOM on my network because WinRM uses only a single port, and the <b>Enable-PSRemoting<\/b> command makes it really easy to set up and configure.<\/p>\n<p>Therefore, I use the <b>Invoke-Command<\/b> cmdlet to get the required hotfix information. In the command listed here, I use the <b>Invoke-Command<\/b> cmdlet, and I pass the collection of <b>Server<\/b> objects to the <b>CN<\/b><i> <\/i>parameter. I use the Windows PowerShell&nbsp;3.0 syntax and pick the <b>name<\/b><i> <\/i>property directly from the array of objects. I pass my <b>credential<\/b> object to the <b>credential<\/b><i> <\/i>parameter and the <b>Get-HotFix<\/b> cmdlet in the <b>script<\/b> block<i> <\/i>chooses the last hotfix from the collection. The <b>ErrorAction<\/b> gets set to 0, which means that errors do not halt operation, nor do they display. (See <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/08\/10\/powertip-specifying-powershell-error-actions.aspx\">PowerTip: Specifying PowerShell Error Actions<\/a> for more information about error actions.) The command is shown here.<\/p>\n<p style=\"padding-left: 30px\">Invoke-Command -cn $servers.name -cred $cred -script { get-hotfix | select -Last 1 } -ea 0<\/p>\n<p>It is not necessary to create a new variable to store the server names if you are using Windows PowerShell&nbsp;2.0. The syntax to pick out only the server name is usable in the <b>ComputerName<\/b><i> <\/i>parameter of the <b>Invoke-Command<\/b> cmdlet. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">Invoke-Command -cn ($servers | % {$_.name})&nbsp; -cred $cred -script { get-hotfix | select -Last 1 } -ea 0<\/p>\n<p>The commands and the associated output from the commands are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8484.HSG-8-15-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8484.HSG-8-15-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>BB, that is all there is to using Windows PowerShell to query Active Directory Domain Services when you do not have the RSAT installed on your workstation. Join me tomorrow for more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about querying the AD DS module via PowerShell and using the results to find hotfixes. &nbsp;Hey, Scripting Guy! &nbsp;We have a server running Windows Server&nbsp;2008&nbsp;R2 that is acting as one of our domain controllers. I do not have access to install the Remote Server Administration Tools (RSAT) on [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,3,8,45],"class_list":["post-5123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-searching-active-directory","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about querying the AD DS module via PowerShell and using the results to find hotfixes. &nbsp;Hey, Scripting Guy! &nbsp;We have a server running Windows Server&nbsp;2008&nbsp;R2 that is acting as one of our domain controllers. I do not have access to install the Remote Server Administration Tools (RSAT) on [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5123","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=5123"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5123\/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=5123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=5123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=5123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}