首页 > 解决方案 > 从远程计算机调用时交换 cmdlet

问题描述

我有一台交换服务器和一台 Windows 7 机器。

WRT远程执行

服务器 - Exchange Server (Win server 2012) 客户端 - Win 7 机器

我想在远程机器(exchange/win server 2012)上运行客户端机器中存在的脚本。但是这些都失败了,找不到错误 cmdlet。

因此,为了快速检查,我尝试调用普通的 powershell cmdlet 以及交换 cmdlet,发现只有交换 cmdlet 失败。但是,如果我在服务器(交换)上运行相同的 cmdlet,它会给我预期的输出。

问题

  1. 交换 cmdlet 不能在远程 powershell 中工作吗?
  2. 我尝试了不同的会话类型,将交换服务器作为连接 URL,但在那里也遇到了错误。

附在示例测试输出下方。

帮助我如何进一步进行!

在远程客户端(Win 7 机器)

PS C:\Users\Administrator> invoke-command -Session $session -ScriptBlock { ls }

返回:

Directory: C:\Users\Administrator\Documents

Mode        LastWriteTime       Length Name        PSComputerName
----        -------------        ------ ----        --------------
d-----      12/2/2018  12:10 PM  WindowsPowerShell  10.76.68.251

但 Exchange cmdlet 不起作用

PS C:\Users\Administrator> invoke-command -Session $session -ScriptBlock { Get-Mailbox }
The term 'Get-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
    + CategoryInfo          : ObjectNotFound: (Get-Mailbox:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : 10.76.68.251

服务器 - Exchange / 服务器 2012

PS C:\Users\Administrator\Downloads\custom scripts> Get-Mailbox

Name                      Alias                ServerName       ProhibitSendQuota
----                      -----                ----------       -----------------
Administrator             Administrator        win-j1uti0rc7qp  Unlimited
DiscoverySearchMailbox... DiscoverySearchMa... win-j1uti0rc7qp  50 GB (53,687,091,200 bytes)

在连接 URI 中使用 Exchange Server URL 进行测试

测试 1

PS C:\Users\Administrator> $session1 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://10.76.68.251/PowerShell/ -Authentication Kerberos -Credential $credential

错误:

New-PSSession : [10.76.68.251] Connecting to remote server 10.76.68.251 failed with the following error message : The
WinRM client cannot process the request. Kerberos authentication cannot be used when the destination is an IP address.
Specify a DNS or NetBIOS destination or specify Basic or Negotiate authentication. For more information, see the
about_Remote_Troubleshooting Help topic.

At line:1 char:13
+ $session1 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri h ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo: OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
    + FullyQualifiedErrorId : -2144108277,PSSessionOpenFailed

测试 2

PS C:\Users\Administrator> $session1 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://10.76.68.251/PowerShell/  -Credential $credential

错误:

New-PSSession : [10.76.68.251] Connecting to remote server 10.76.68.251 failed with the following error message : The
WinRM client cannot process the request. It cannot determine the content type of the HTTP response from the
destination computer. The content type is absent or invalid. For more information, see the
about_Remote_Troubleshooting Help topic.
At line:1 char:13
+ $session1 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri h ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession],PSRemotingTransportException
    + FullyQualifiedErrorId : -2144108297,PSSessionOpenFailed

标签: powershellexchange-serverexchangewebservicespowershell-remoting

解决方案


是的,他们这样做了,这是一种常见的做法,但你所做的还不够完整。

  1. PSRemoting 必须正确启用。
  2. 您必须传递一个帐户的凭据,该帐户是框上的管理员和 Exchange 中的管理员

您必须使用 PSRemoting 来执行此操作,Microsoft 对此进行了详细记录,不仅适用于 Exchange on prom,而且适用于 Exchange Online。

使用远程 PowerShell 连接到 Exchange 服务器

连接-O365 1.5.4

如果您使用的是 PowerShell ISE,则可以采用这两种方法中的任何一种,只需记住点击“命令”选项卡上的“刷新”按钮即可查看反映的 cmdlet。

如何将 Exchange 命令行管理程序加载到 PowerShell ISE

将 Exchange Shell 项目添加到 PowerShell ISE

$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add(
    "Connect to Exchange @ Contoso", {
        $ExSession= New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exserver.contoso.com/PowerShell/ -Authentication Kerberos
        Import-PSSession $ExSession
    },
    "Control+Alt+1"
)
$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add(
    "Connect to Exchange On-Premise", {
        Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
        . $env:ExchangeInstallPath\bin\RemoteExchange.ps1
        Connect-ExchangeServer –auto
            },
    "Control+Alt+2"
)
$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add(
    "Connect to Exchange Online", {
        $o365Cred= Get-Credential
        $o365Session= New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $o365Cred -Authentication Basic -AllowRedirection
        Import-PSSession $o365Session
    },
    "Control+Alt+3"
)

如果您使用的是控制台主机,只需删除所有 ISE 内容。

$ExSession= New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exserver.contoso.com/PowerShell/ -Authentication Kerberos
Import-PSSession $ExSession

推荐阅读