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 "administrator@jachobitechurch.onmicrosoft.com" -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: $_" } }