Fredric Carlberg
Fredric Carlberg

Reputation: 1

Running Connect-EXOPSSession in Powershell ISE

I'm struggling to get our PowerShell script for creating mailboxes, assigning licenses etc. to work with MFA.

If I use this script, only;

$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]

I can use "Get-Mailbox"

However, when I run the same script as a function in our script;

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]

}

I do not get any errors, I can log in with MFA without any issues... but, Get-Mailbox is not recognized.

This is the only thing in our script that actually calls the 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
}

All help I can get is extremely appreciated.

Upvotes: 0

Views: 3231

Answers (2)

avdividetace
avdividetace

Reputation: 1

This is the correct code to successfully using Connect-EXOPsSession or Connect-IPPsSession in ISE console using MFA (even within VSCode, Powershell Manager, etc - be sure that your default shell is set to 5.1, do not use core 6, 7, etc):

$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"

You can then use Exchange Online cmdlets as you would normally (Get-Mailbox, etc).

Upvotes: 0

Chris Black
Chris Black

Reputation: 371

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
}

Originally fixed here.

The code above will help you to wrap your code inside a function that correctly imports all the commands. It uses "Global" parameter from Import-Module that can not only import modules but also import Custom Objects.

Upvotes: 1

Related Questions