{"id":303,"date":"2014-09-27T15:17:21","date_gmt":"2014-09-27T19:17:21","guid":{"rendered":"http:\/\/adsecurity.org\/?p=303"},"modified":"2015-01-25T18:00:20","modified_gmt":"2015-01-25T23:00:20","slug":"powershell-remote-use-of-module-commandlets-remoting-import-module","status":"publish","type":"post","link":"https:\/\/adsecurity.org\/?p=303","title":{"rendered":"Powershell Remote Use of Module Commandlets (Remoting Import-Module)"},"content":{"rendered":"<p>Practically all of my Powershell scripts use an Active Directory commandlet. Ok, so they use several.\u00a0 I like to query AD to get environmental information so when I run the script, I know what I am working with from an AD perspective. I can\u2019t help it, I\u2019m an AD Guy.<\/p>\n<p>In order to run the AD commandlets, a Domain Controller in the domain has to be running ADWS (<a title=\"ADWS Reference\" href=\"http:\/\/technet.microsoft.com\/en-us\/library\/dd391908%28WS.10%29.aspx\" target=\"_blank\">Active Directory Web Services<\/a> aka Active Directory Management Gateway Service). Windows 2008 (and Windows 2008 R2) Domain Controllers run this by default and <a title=\"Download ADWS for Windows 2003 servers\" href=\"http:\/\/www.microsoft.com\/download\/en\/details.aspx?id=2852\" target=\"_blank\">it is available for Windows 2003 DCs<\/a>.<\/p>\n<p>Sometimes, the server I need to run a script on doesn\u2019t have the AD commandlets installed.<br \/>\nThe quick solution to this is to run the following:<\/p>\n<blockquote><p>powershell set-executionpolicy unrestricted<br \/>\npowershell import-module servermanager\u00a0 ;\u00a0 add-windowsfeature rsat-ad-powershell<\/p><\/blockquote>\n<p>Assuming this isn\u2019t possible, there is a way to import-modules available on another server.\u00a0 I often use this with Exchange commandlets since I rarely have them installed on servers I use for running scripts.<\/p>\n<p>Here\u2019s how this works\u2026<\/p>\n<blockquote><p># Create a Powershell remote session to a server with the #commandlets installed.<br \/>\n$Session = New-PSsession -Computername Server1<br \/>\n# Use the newly created remote Powershell session to send a #command to that session<br \/>\nInvoke-Command -Command {Import-Module ActiveDirectory} -Session $Session<br \/>\n# Use that session with the modules to add the available # commandlets to your existing Powershell command shell with a #new command name prefix.<br \/>\nImport-PSSession -Session $Session -Module ActiveDirectory -Prefix RM<\/p><\/blockquote>\n<p>The code above enables the use of Active Directory commandlets on a server that doesn\u2019t have them installed.<\/p>\n<p>Use AD commandlets in the Powershell command shell with modified names based on the -Prefix set above:<\/p>\n<p>Get-RMAdUser\u00a0 instead of the standard Get-ADUser<br \/>\nGet-RMAdComputer instead of the standard Get-ADComputer<\/p>\n<p>You can also drop the \u201c-Prefix RM\u201d and it will use the native commandlets using their standard names.<\/p>\n<p>Don Jones calls this Powershell implicit remoting and it is very cool.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Practically all of my Powershell scripts use an Active Directory commandlet. Ok, so they use several.\u00a0 I like to query AD to get environmental information so when I run the script, I know what I am working with from an AD perspective. I can\u2019t help it, I\u2019m an AD Guy. In order to run the &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"https:\/\/adsecurity.org\/?p=303\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[20,101,123,102,575],"class_list":["post-303","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-activedirectory","tag-domaincontroller","tag-exchange","tag-microsoftwindows","tag-powershell","item-wrap"],"_links":{"self":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=303"}],"version-history":[{"count":2,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/303\/revisions"}],"predecessor-version":[{"id":1357,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/303\/revisions\/1357"}],"wp:attachment":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}