{"id":11391,"date":"2012-01-23T00:01:00","date_gmt":"2012-01-23T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/01\/23\/learn-how-to-use-powershell-to-run-exchange-commands-remotely\/"},"modified":"2012-01-23T00:01:00","modified_gmt":"2012-01-23T00:01:00","slug":"learn-how-to-use-powershell-to-run-exchange-commands-remotely","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/learn-how-to-use-powershell-to-run-exchange-commands-remotely\/","title":{"rendered":"Learn How to Use PowerShell to Run Exchange Commands Remotely"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell to run Exchange Server&nbsp;2010 commands remotely by using implicit remoting.<\/p>\n<p><span><span><span><span><span><span><span><span><img decoding=\"async\" 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\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>&nbsp;Hey, Scripting Guy! I am having a problem with Exchange Server&nbsp;2010. I have Windows PowerShell&nbsp;2.0 installed on the server, but I am unable to connect and to do anything with it using Windows PowerShell remoting. I am forced to use RDP to make a remote desktop session, and then to launch the Exchange Server Management Shell to be able to do anything. I think this is rather stupid, especially when I can use Windows PowerShell remoting to establish a remote Windows PowerShell session on any other server in the domain, import whatever modules I need to use, and complete my work. I know that Exchange Server&nbsp;2010 uses snap-ins, not modules; but still, it should work. I hate to rebuild my server, but I am about to do so because I am unable to fix whatever is broken. Can you help me? It will save me an entire weekend if you can.<\/p>\n<p>&mdash;SW<\/p>\n<p><span><span><span><span><span><span><span><span><img decoding=\"async\" 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\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>Hello SW,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Please do not FDISK your server! I say again, please do not FDISK your server. It will really be a waste of time. What you are no doubt experiencing is, in fact, the intended experience. You have no doubt discovered that the Exchange Server&nbsp;2010 cmdlets are a little different&mdash;in fact, they are all functions. The great thing about this is that you can look at the content of every one of the functions to discover how they work.<\/p>\n<p>One thing you need to know is that there is a difference between implicit remoting and explicit remoting. With explicit remoting, you create a remote session, enter a remote session, and you are dropped onto a Windows PowerShell console prompt on the remote computer. The Windows PowerShell prompt that you see is remote&mdash;it resides on the remote computer. Typing <b>dir<\/b><i> <\/i>into the prompt displays the file system structure of the remote computer.<\/p>\n<p>With implicit remoting, the commands from the remote session come to the local computer. Therefore, the Windows PowerShell prompt you see is local&mdash;it remains on your computer. Typing <b>dir<\/b><i> <\/i>into the prompt displays the file system structure of the local computer.<\/p>\n<p>If I make a remote connection to a server running Exchange Server&nbsp;2010, and I add the three management snap-ins for Exchange Server, Windows PowerShell displays no errors. However, when I attempt to run a common Exchange Server command, such as <b>Get-ExchangeServer<\/b>, an error appears. The commands, and associated errors are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3414.HSG-01-23-12-01.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3414.HSG-01-23-12-01.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The secret to using Windows PowerShell remoting to remotely manage a server running Exchange Server&nbsp;2010 is to use implicit remoting instead of explicitly connecting to a remote Windows PowerShell session. Here are the steps required to create an implicit remote Windows PowerShell session.<\/p>\n<ol>\n<li>Use the <b>Get-Credential <\/b>cmdlet to obtain credentials for the server running Exchange Server&nbsp;2010. Store the returned credential object in a variable.<\/li>\n<li>Use the <b>New-PSSession <\/b>cmdlet to create a new session on the server. Specify the <i>ConnectionUri <\/i>in the form of http:\/\/<i>servername<\/i>\/powershell and supply the credential object from step 1 to the <i>Credential <\/i>parameter. Store the returned session object in a variable.<\/li>\n<li>Use the <b>Import-PSSession <\/b>cmdlet to connect to the session created in step 2.<\/li>\n<\/ol>\n<p>The code that follows illustrates connection to a remote server running Exchange Server&nbsp;2010 (named EX1) as the administrator from the iammred domain.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $cred = Get-Credential iammred\\administrator<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri<\/p>\n<p style=\"padding-left: 30px\">&nbsp;http:\/\/ex1\/powershell -Credential $cred<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Import-PSSession $session<\/p>\n<p>When the <b>Import-PSSession <\/b>command runs, a warning appears that states some of the imported commands use unapproved verbs. This is a normal warning for the Exchange Server commands, and can be safely ignored.<\/p>\n<p>Note: The warning message is also why when one is writing Windows PowerShell modules, one should always use approved verbs. This avoids confusing users with the warning message. Display approved verbs by using the <b>Get-Verb <\/b>cmdlet.<\/p>\n<p>When connected, the Exchange Server cmdlets appear in the local Windows PowerShell console and they work as if they locally installed. To obtain information about servers running Exchange Server, use the <b>Get-ExchangeServer <\/b>cmdlet. This command appears here.<\/p>\n<p style=\"padding-left: 30px\">Get-ExchangeServer<\/p>\n<p>Because there are several steps involved in making an implicit remoting session to a remote server running Exchange Server&nbsp;2010, it becomes a good candidate for a function. Because the function is a bit long (with the comment-based Help), I also uploaded it to the <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Create-a-new-implicit-5fdafe45\">Scripting Guys Script Repository<\/a>. Here is the complete <b>New-ExchangeSession<\/b> function.<\/p>\n<p style=\"padding-left: 30px\"><b>Function New-ExchangeSession<\/b><\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp; &lt;#<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Synopsis<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; This function creates an implicit remoting connection to an Exchange Server<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Description<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; This function creates an implicit remoting session to a remote Exchange<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Server. It has been tested on Exchange 2010. The Exchange commands are<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; brought into the local PowerShell environment. This works in both the<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Windows PowerShell console as well as the Windows PowerShell ISE. It requires<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; two parameters: the computername and the user name with rights on the remote<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Exchange server.<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Example<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; New-ExchangeSession -computername ex1 -user iammred\\administrator<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Makes an implicit remoting connection to a remote Exchange 2010 server<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; named ex1 using the administrator account from the iammred domain. The user<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; is prompted for the administrator password.<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Parameter ComputerName<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; The name of the remote Exchange server<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Parameter User<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; The user account with rights on the remote Exchange server. The user<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; account is specified as domain\\username<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Notes<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; NAME:&nbsp; New-ExchangeSession<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; AUTHOR: ed wilson, msft<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; LASTEDIT: 01\/13\/2012 17:05:32<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; KEYWORDS: Messaging &amp; Communication, Microsoft Exchange 2010, Remoting<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; HSG: HSG-1-23-12<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Link<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; Http:\/\/www.ScriptingGuys.com<\/p>\n<p style=\"padding-left: 30px\">&nbsp;#Requires -Version 2.0<\/p>\n<p style=\"padding-left: 30px\">&nbsp;#&gt;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;Param(<\/p>\n<p style=\"padding-left: 30px\">&nbsp; [Parameter(Mandatory=$true,Position=0)]<\/p>\n<p style=\"padding-left: 30px\">&nbsp; [String]<\/p>\n<p style=\"padding-left: 30px\">&nbsp; $computername,<\/p>\n<p style=\"padding-left: 30px\">&nbsp; [Parameter(Mandatory=$true,Position=1)]<\/p>\n<p style=\"padding-left: 30px\">&nbsp; [String]<\/p>\n<p style=\"padding-left: 30px\">&nbsp; $user<\/p>\n<p style=\"padding-left: 30px\">&nbsp; )<\/p>\n<p style=\"padding-left: 30px\">&nbsp; $cred = Get-Credential -Credential $user<\/p>\n<p style=\"padding-left: 30px\">&nbsp; $session = New-PSSession -ConfigurationName Microsoft.Exchange `<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; -ConnectionUri http:\/\/$computername\/powershell -Credential $cred<\/p>\n<p style=\"padding-left: 30px\">&nbsp; Import-PSSession $session<\/p>\n<p style=\"padding-left: 30px\">} #end function New-ExchangeSession<\/p>\n<p>To gain access to the <b>New-ExchangeSession<\/b> function, I dot-source the script that contains the <b>New-ExchangeSession<\/b> function into my current Windows PowerShell session. When I run the function, a credential dialog box appears. This dialog box is shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8741.HSG-01-23-12-02.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8741.HSG-01-23-12-02.jpg\" border=\"0\" alt=\"\" \/><\/a><\/p>\n<p>When I enter the credentials, the implicit remoting session starts, and I can use cmdlets for Exchange Server as if they were installed on the local computer. In the following command, I retrieve information about Microsoft Exchange Server mailbox databases.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-MailboxDatabase<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Server&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Recovery&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ReplicationType<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p style=\"padding-left: 30px\">Mailbox Database 1301642447&nbsp;&nbsp;&nbsp; EX1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; False&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; None<\/p>\n<p>One command that is not available is the <b>Get-ExCommand<\/b>. I will talk about that tomorrow.<\/p>\n<p>SW, that is all there is to using commands for Exchange Server&nbsp;2010 from a remote computer. Join me tomorrow when I will talk about discovering imported commands for Exchange Server. I will also create a function to display the commands.<\/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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to run Exchange Server&nbsp;2010 commands remotely by using implicit remoting. &nbsp;Hey, Scripting Guy! I am having a problem with Exchange Server&nbsp;2010. I have Windows PowerShell&nbsp;2.0 installed on the server, but I am unable to connect and to do anything with it using Windows PowerShell remoting. I am forced [&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":[28,180,57,3,4,45],"class_list":["post-11391","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-messaging-and-communication","tag-microsoft-exchange-2010","tag-remoting","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to run Exchange Server&nbsp;2010 commands remotely by using implicit remoting. &nbsp;Hey, Scripting Guy! I am having a problem with Exchange Server&nbsp;2010. I have Windows PowerShell&nbsp;2.0 installed on the server, but I am unable to connect and to do anything with it using Windows PowerShell remoting. I am forced [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11391","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=11391"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11391\/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=11391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=11391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=11391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}