导入 Microsoft Exchange Online PowerShell 模块

Import Microsoft Exchange Online PowerShell Module

以前,我曾经使用以下 BAT 文件将 PowerShell 连接到 Windows Azure Active Directory 和 Exchange Online:

@echo off 
Powershell -noexit -Command "& {
    $Credentials = Get-Credential;
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -Authentication Basic -ConnectionUri https://ps.outlook.com/powershell -AllowRedirection:$true -Credential $Credentials;
    Import-PSSession $Session;
    Import-Module MSOnline;
    Connect-MsolService -Credential $Credentials;
}"

在我们对全局管理员帐户启用 MFA/TFA 之前,这一直很有效,这会导致上述 BAT 文件失败并出现以下错误:

New-PSSession : [ps.outlook.com] Connecting to remote server ps.outlook.com failed with the following error message :
[ClientAccessServer=VI1PR0701CA0070,BackEndServer=amspr03mb326.eurprd03.prod.outlook.com,RequestId=f875eba9-7066-45df-9d59-67f9daf8b210,TimeStamp=9/7/2017 7:57:14 AM] Access Denied For more information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:66
+ ...  $Session = New-PSSession -ConfigurationName Microsoft.Exchange -Auth ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
    + FullyQualifiedErrorId : -2144108477,PSSessionOpenFailed
Import-PSSession : Cannot validate argument on parameter 'Session'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At line:1 char:256
+ ... ion:$true -Credential $Credentials; Import-PSSession $Session; Import ...
+                                                          ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Import-PSSession], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ImportPSSessionCommand

Connect-MsolService : Authentication Error: Authentication cancelled by user.
At line:1 char:290
+ ... Import-Module MSOnline; Connect-MsolService -Credential $Credentials}
+                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [Connect-MsolService], Exception
    + FullyQualifiedErrorId : System.Exception,Microsoft.Online.Administration.Automation.ConnectMsolService

根据 Microsoft 的 TechNet 文章 "Connect to Exchange Online PowerShell using multi-factor authentication",我需要(仅使用 Microsoft 网络浏览器,不少于)安装“[Microsoft] Exchange Online [远程] PowerShell 模块”。

我已经这样做并确认我现在可以使用启用了 MFA/TFA 的管理员帐户将 PowerShell 连接到 Exchange Online。

但是,我想将新的 PowerShell 模块合并到我原来的 BAT 文件中,但我不确定是否可行,因为:

  1. 研究未发现任何相关信息。
  2. “开始”菜单项 Microsoft Exchange Online Powershell Module 是一个 APPREF-MS / ClickOnce 文件,似乎没有指向任何本地文件。
  3. 命令 Get-Module –ListAvailable 不输出包含 EXOP 的任何内容。

谁能指点一下?

更新 2017/09/08 10:10:

我:

  1. 了解到 ClickOnce 应用程序存储在文件夹 %localAppData%\Apps.0\ 中。
  2. 已在文件夹 %localAppData%\Apps.0\ 中搜索 *.ps1
  3. 找到文件 C:\Users\%userName%\AppData\Local\Apps.0\N8Z7NPYM.QVD\BL0EGO2J.G5A\micr..tion_51a5b647dacf4059_0010.0000_5d32306b9385c20a\CreateExoPSSession.ps1
  4. 在文件 C:\Users\%userName%\AppData\Local\Apps.0\N8Z7NPYM.QVD\BL0EGO2J.G5A\micr..tion_51a5b647dacf4059_0010.0000_5d32306b9385c20a\CreateExoPSSession.ps1 中搜索了 Connect-EXOPSSession
  5. 找到以下行:

    $ExoPowershellModule = "Microsoft.Exchange.Management.ExoPowershellModule.dll";
    $ModulePath = [System.IO.Path]::Combine($PSScriptRoot, $ExoPowershellModule);
    
    $global:ConnectionUri = $ConnectionUri;
    $global:AzureADAuthorizationEndpointUri = $AzureADAuthorizationEndpointUri;
    $global:UserPrincipalName = $UserPrincipalName;
    $global:PSSessionOption = $PSSessionOption;
    
    Import-Module $ModulePath;
    $PSSession = New-ExoPSSession -UserPrincipalName $UserPrincipalName -ConnectionUri $ConnectionUri -AzureADAuthorizationEndpointUri $AzureADAuthorizationEndpointUri -PSSessionOption $PSSessionOption
    
    if ($PSSession -ne $null)
            {
                Import-PSSession $PSSession -AllowClobber
                UpdateImplicitRemotingHandler
            }
    
  6. 已在文件夹 %localAppData%\Apps.0\ 中搜索 microsoft.exchange.management.exopowershellmodule.dll
  7. 找到文件C:\Users\%userName%\AppData\Local\Apps.0\N8Z7NPYM.QVD\BL0EGO2J.G5A\micr..tion_51a5b647dacf4059_0010.0000_5d32306b9385c20a\Microsoft.Exchange.Management.ExoPowershellModule.dll
  8. 已将文件夹 C:\Users\%userName%\AppData\Local\Apps.0\N8Z7NPYM.QVD\BL0EGO2J.G5A\micr..tion_51a5b647dacf4059_0010.0000_5d32306b9385c20a 中的所有文件复制到文件夹 C:\windows\system32\WindowsPowerShell\v1.0\modules\Exo
  9. 执行的命令:

    Import-Module "C:\windows\system32\WindowsPowerShell\v1.0\modules\Exo\Microsoft.Exchange.Management.ExoPowershellModule.dll"
    New-ExoPSSession
    
  10. 已通过 MFA 验证。

  11. 发现 PowerShell 输出了以下内容:

     Id Name            ComputerName    ComputerType    State         ConfigurationName     Availability
     -- ----            ------------    ------------    -----         -----------------     ------------
      1 WinRM1          outlook.offi... RemoteMachine   Opened        Microsoft.Exchange       Available
    
  12. 发现 Get-MailboxGet-InboxRule 等命令失败并出现以下错误:

     %cmdlet% : The term '%cmdlet%' 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.
     At line:1 char:1
     + %cmdlet%
     + ~~~~~~~~~~~
         + CategoryInfo          : ObjectNotFound: (%cmdlet%:String) [], CommandNotFoundException
         + FullyQualifiedErrorId : CommandNotFoundException
    

所以,我们正在取得进展。

我通过以下方法解决了这个问题;

来自您的第 7 步:我根本没有复制文件。我把他们留在原地。没有理由为什么他们在复制后不能工作,但我经常重建这台机器所以想减少步骤。

我的第 8 步:在 ISE 中(在我的例子中,目录名称如下)
cd C:\Users\%username%\AppData\Local\Apps.0\V7MK47Q8.0NZ\KLJLETPC.4RJ\micr..tion_d8f8f667ee342b5c_0010.0000_6b4a13fd451b1c00 Import-Module .\CreateExoPSSession.ps1 Connect-EXOPSSession -UserPrincipalName name@domain.com

现在,没有理由认为它应该起作用,因为它不是一个合适的模块,但它确实起作用并且已经存在了一段时间。

我不能 post 图片,因为我没有代表,但足以说明当你使用 "connect-" 时,列表中的前两个是 AzureAD 和 EXOPSSession。

我正想做同样的事情,然后遇到了 this link。只需 运行 普通 PowerShell 中的这两个命令 window 就可以让我连接到 Exchange Online(启用 MFA):

$CreateEXOPSSession = (Get-ChildItem -Path $env:userprofile -Filter CreateExoPSSession.ps1 -Recurse -ErrorAction SilentlyContinue -Force | Select -Last 1).DirectoryName

. "$CreateEXOPSSession\CreateExoPSSession.ps1"

然后我可以使用

连接
Connect-EXOPSSession

您可以使用 Exchange Online PowerShell V2 模块而不是导入 Exchange Online PowerShell 模块。

目前我使用的是EXO V2模块。您可以直接从 PowerShell 库中安装模块。

    Install-Module ExchangeOnlineManagement

您可以使用以下 cmdlet 连接到 Exchange Online PowerShell。

    Connect-ExchangeOnline

更多信息:Connect to Exchange Online PowerShell using EXO V2 module