Files
Public_Scripts/office_365/connect_exchange
T
2026-11-04 00:25:50 +00:00

29 lines
1.2 KiB
Plaintext

function Connect-EXO {
try {
Write-Host "Connecting to Exchange Online (ISE Compatibility Mode)..." -ForegroundColor Cyan
# Inject C# code to allocate a temporary console footprint for ISE
$consoleCode = @'
using System;
using System.Runtime.InteropServices;
public class ISEConsoleFix {
[DllImport("kernel32.dll", SetLastError = true)] public static extern int AllocConsole();
[DllImport("kernel32.dll", SetLastError = true)] public static extern int FreeConsole();
}
'@
Add-Type -TypeDefinition $consoleCode -ErrorAction SilentlyContinue
# Allocate the handle, connect using your working syntax, then free it
[ISEConsoleFix]::AllocConsole() | Out-Null
Connect-ExchangeOnline -UserPrincipalName "[email protected]" -DisableWAM -ShowBanner:$false
[ISEConsoleFix]::FreeConsole() | Out-Null
$global:Connected = $true
Write-Log "Connected to Exchange Online"
} catch {
# Ensure the handle is cleaned up if the login fails or is cancelled
try { [ISEConsoleFix]::FreeConsole() | Out-Null } catch {}
Write-Log "Connection failed: $_"
}
}