【问题标题】:Running Connect-EXOPSSession in Powershell ISE在 Powershell ISE 中运行 Connect-EXOPSSession
【发布时间】:2018-08-13 07:17:05
【问题描述】:

我正在努力获取用于创建邮箱、分配许可证等以使用 MFA 的 PowerShell 脚本。

如果我使用这个脚本,只有;

$modules = @(Get-ChildItem -Path "$($env:LOCALAPPDATA)\Apps\2.0" -Filter "Microsoft.Exchange.Management.ExoPowershellModule.manifest" -Recurse )
$moduleName =  Join-Path $modules[0].Directory.FullName "Microsoft.Exchange.Management.ExoPowershellModule.dll"
Import-Module -FullyQualifiedName $moduleName -Force
$scriptName =  Join-Path $modules[0].Directory.FullName "CreateExoPSSession.ps1"
. $scriptName
$null = Connect-EXOPSSession
$exchangeOnlineSession = (Get-PSSession | Where-Object { ($_.ConfigurationName -eq 'Microsoft.Exchange') -and ($_.State -eq 'Opened') })[0]

我可以使用“获取邮箱”

但是,当我运行与脚本中的函数相同的脚本时;

function O365Logon
{

$modules = @(Get-ChildItem -Path "$($env:LOCALAPPDATA)\Apps\2.0" -Filter "Microsoft.Exchange.Management.ExoPowershellModule.manifest" -Recurse )
$moduleName =  Join-Path $modules[0].Directory.FullName "Microsoft.Exchange.Management.ExoPowershellModule.dll"
Import-Module -FullyQualifiedName $moduleName -Force
$scriptName =  Join-Path $modules[0].Directory.FullName "CreateExoPSSession.ps1"
. $scriptName
$null = Connect-EXOPSSession
$exchangeOnlineSession = (Get-PSSession | Where-Object { ($_.ConfigurationName -eq 'Microsoft.Exchange') -and ($_.State -eq 'Opened') })[0]

}

我没有收到任何错误,我可以毫无问题地使用 MFA 登录...但是,无法识别 Get-Mailbox。

这是我们脚本中唯一真正调用 O365Logon 的东西;

function Main
{
    write-host "`nThis script will create a mailbox in Office 365 for an AD user (with correct attributes), continue? (y/n)."

    $response = read-host
    if ($response.ToLower() -ne "y"){ 
        Quit
    }   

    O365Logon

    return MainMenu
}

非常感谢我能得到的所有帮助。

【问题讨论】:

    标签: powershell authentication exchange-server


    【解决方案1】:
    function connect-365 {
        Import-Module CreateExoPsSession
        $null = Connect-EXOPSSession -ConnectionUri "https://ps.outlook.com/powershell/" | Out-Null
        $global:exchangeOnlineSession = (Get-PSSession | Where-Object { ($_.ConfigurationName -eq 'Microsoft.Exchange') -and ($_.State -eq 'Opened') })[0]
        Import-Module -AsCustomObject (Import-PSSession $exchangeOnlineSession -AllowClobber) -Global
    }
    

    原来固定here

    上面的代码将帮助您将代码包装在正确导入所有命令的函数中。 它使用来自Import-Module 的“全局”参数,不仅可以导入模块,还可以导入自定义对象。

    【讨论】:

      【解决方案2】:

      这是使用 MFA 在 ISE 控制台中成功使用 Connect-EXOPsSession 或 Connect-IPPsSession 的正确代码(即使在 VSCode、Powershell Manager 等中 - 确保您的默认 shell 设置为 5.1,不要使用核心 6, 7等):

      $MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA + "\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse).FullName | Select-Object -Last 1)
      
      . "$MFAExchangeModule"
      
      Connect-EXOPSSession -UserPrincipalName "insert UPN here"
      

      然后,您可以像往常一样使用 Exchange Online cmdlet(Get-Mailbox 等)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-09
        • 2013-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-26
        • 2020-03-08
        相关资源
        最近更新 更多