Home

60 minutes timeout on MFA Session

%3CLINGO-SUB%20id%3D%22lingo-sub-559224%22%20slang%3D%22en-US%22%3E60%20minutes%20timeout%20on%20MFA%20Session%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-559224%22%20slang%3D%22en-US%22%3E%3CP%3EBelow%20is%20the%20script%20for%20a%20non-ending%20MFA%20session%2C%20that%20renews%20it%20self%20%3B%20once%20you%20connect%20to%20ExO%20with%20this%20script%20it%20will%20stay%20open.%3C%2FP%3E%0A%3CP%3Efirst%20Use%20Internet%20Explorer%20to%20download%20and%20install%20this%20module%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcmdletpswmodule.blob.core.windows.net%2Fexopsmodule%2FMicrosoft.Online.CSE.PSModule.Client.application%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fcmdletpswmodule.blob.core.windows.net%2Fexopsmodule%2FMicrosoft.Online.CSE.PSModule.Client.application%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20script%20has%20an%20automated%20firing%20timer%20every%2010%20minutes%20%2C%20but%20you%20can%20either%20force%20it%20by%26nbsp%3B%3C%2FP%3E%0A%3CP%3EInvoke-ExoOnlineConnection%20-RepairPSSession%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Eor%20check%20if%20the%20session%20is%20about%20to%20expire%20and%20repairs%20itself%3C%2FP%3E%0A%3CP%3EInvoke-ExoOnlineConnection%20-Checktimer%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%24Global%3AErrorActionPreference%20%3D%20%22Stop%22%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%24Global%3AVerbosePreference%20%3D%20%22Continue%22%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%24office365UserPrincipalName%20%3D%20%22ADMIN%40o365.com%22%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%24PSExoPowershellModuleRoot%20%3D%20(Get-ChildItem%20-Path%20%24env%3Auserprofile%20-Filter%20CreateExoPSSession.ps1%20-Recurse%20-ErrorAction%20SilentlyContinue%20-Force%20%7C%20Select%20-Last%201).DirectoryName%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%24ExoPowershellModule%20%3D%20%22Microsoft.Exchange.Management.ExoPowershellModule.dll%22%3B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%24ModulePath%20%3D%20%5BSystem.IO.Path%5D%3A%3ACombine(%24PSExoPowershellModuleRoot%2C%20%24ExoPowershellModule)%3B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3EImport-Module%20%24ModulePath%3B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%3CSUP%3Efunction%20Invoke-ExoOnlineConnection%7B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%5Bcmdletbinding()%5D%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BParam%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B(%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%5BParameter(Mandatory%3D%24false)%5D%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%5Bswitch%5D%24Checktimer%2C%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%5BParameter(mandatory%3D%24false%2C%20valuefrompipeline%3D%24false)%5D%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%5Bswitch%5D%24RepairPSSession%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B)%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bbegin%7B%7D%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bprocess%7B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%23determine%20if%26nbsp%3B%26nbsp%3BPsSession%20is%20loaded%20in%20memory%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24ExosessionInfo%20%3D%20Get-PsSession%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%23calculate%20session%20time%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bif%20(%24global%3AExosessionStartTime)%7B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24global%3AExosessionTotalTime%20%3D%20((Get-Date)%20-%20%24global%3AExosessionStartTime)%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%23need%20to%20loop%20through%20each%20session%20a%20user%20might%20have%20opened%20previously%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bforeach%20(%24ExosessionItem%20in%20%24ExosessionInfo)%7B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%23check%20session%20timer%20to%20know%20if%20we%20need%20to%20break%20the%20connection%20in%20advance%20of%20a%20timeout.%20Break%20and%20make%20new%20after%2040%20minutes.%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bif%20(%24ExosessionItem.ComputerName.Contains(%22outlook.office365.com%22)%20-and%20%24ExosessionItem.State%20-eq%20%22Opened%22%20-and%20%24global%3AExosessionTotalTime.TotalSeconds%20-ge%20%222400%22)%7B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BWrite-Verbose%20-Message%20%22The%20PowerShell%20session%20has%20been%20running%20for%20%24(%24global%3AExosessionTotalTime.TotalMinutes)%20minutes.%20We%20need%20to%20shut%20it%20down%20and%20create%20a%20new%20session%20due%20to%20the%20access%20token%20expiration%20at%2060%20minutes.%22%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24ExosessionItem%20%7C%20Remove-PSSession%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BStart-Sleep%20-Seconds%203%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24strSessionFound%20%3D%20%24false%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24global%3AExosessionTotalTime%20%3D%20%24null%20%23reset%20the%20timer%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%20else%20%7B%26nbsp%3BWrite-Verbose%20-Message%20%22The%20PowerShell%20session%20has%20been%20running%20for%20%24(%24global%3AExosessionTotalTime.TotalMinutes)%20minutes.)%22%7D%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%23Force%20repair%20PSSession%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bif%20(%24ExosessionItem.ComputerName.Contains(%22outlook.office365.com%22)%20-and%20%24RepairPSSession)%7B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BWrite-Verbose%20-Message%20%22Attempting%20to%20repair%20broken%20PowerShell%20session%20to%20Exchange%20Online%20using%20cached%20credential.%22%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24ExosessionItem%20%7C%20Remove-PSSession%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BStart-Sleep%20-Seconds%203%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24strSessionFound%20%3D%20%24false%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24global%3AExosessionTotalTime%20%3D%20%24null%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7Delseif%20(%24ExosessionItem.ComputerName.Contains(%22outlook.office365.com%22)%20-and%20%24ExosessionItem.State%20-eq%20%22Opened%22)%7B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24strSessionFound%20%3D%20%24true%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bif%20(!%24strSessionFound)%7B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BWrite-Verbose%20-Message%20%22Creating%20new%20Exchange%20Online%20PowerShell%20session...%22%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Btry%7B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24ExoSession%26nbsp%3B%26nbsp%3B%3D%20New-ExoPSSession%20-UserPrincipalName%20%24office365UserPrincipalName%20-ConnectionUri%20%22%3CA%20href%3D%22https%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%3C%2FA%3E%22%20-ErrorAction%20SilentlyContinue%20-ErrorVariable%20%24newOnlineSessionError%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3Ecatch%7B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%3CSUP%3EWrite-Verbose%20-Message%20%22Throw%20error...%22%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bthrow%3B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%20finally%20%7B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20if%20(%24newOnlineSessionError)%20%7B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Write-Verbose%20-Message%20%22Final%20error...%22%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20throw%20%24newOnlineSessionError%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Write-Verbose%20-Message%20%22Importing%20remote%20PowerShell%20session...%22%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24global%3AExosessionStartTime%20%3D%20(Get-Date)%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BImport-PSSession%20%24ExoSession%26nbsp%3B-AllowClobber%20%7C%20Out-Null%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bend%7B%7D%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%7D%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%23%23%20Create%20an%20Timer%20instance%26nbsp%3B%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%24timer%20%3D%20New-Object%20Timers.Timer%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%23%23%20Now%20setup%20the%20Timer%20instance%20to%20fire%20events%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%24timer.Interval%20%3D%20600000%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%24timer.AutoReset%20%3D%20%24true%20%26nbsp%3B%23%20enable%20the%20event%20again%20after%20its%20been%20fired%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%24timer.Enabled%20%3D%20%24true%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%23%23%20register%20your%20event%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%23%23%20%24args%5B0%5D%20Timer%20object%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%23%23%20%24args%5B1%5D%20Elapsed%20event%20properties%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSUP%3ERegister-ObjectEvent%20-InputObject%20%24timer%20-EventName%20Elapsed%20-SourceIdentifier%20Repair%20%26nbsp%3B-Action%20%7BInvoke-ExoOnlineConnection%20-Checktimer%7D%3C%2FSUP%3E%3C%2FP%3E%0A%3CP%3E%3CSUP%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%3C%2FSUP%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-559224%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExchange%20Online%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EHybrid%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
Mahmoud Badran
Microsoft

Below is the script for a non-ending MFA session, that renews it self ; once you connect to ExO with this script it will stay open.

first Use Internet Explorer to download and install this module https://cmdletpswmodule.blob.core.windows.net/exopsmodule/Microsoft.Online.CSE.PSModule.Client.appli...

 

 

The script has an automated firing timer every 10 minutes , but you can either force it by 

Invoke-ExoOnlineConnection -RepairPSSession

 

or check if the session is about to expire and repairs itself

Invoke-ExoOnlineConnection -Checktimer

 

###############################################################

$Global:ErrorActionPreference = "Stop"

$Global:VerbosePreference = "Continue"

 

 

$office365UserPrincipalName = "ADMIN@o365.com"

 

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

 

$ExoPowershellModule = "Microsoft.Exchange.Management.ExoPowershellModule.dll";

 

$ModulePath = [System.IO.Path]::Combine($PSExoPowershellModuleRoot, $ExoPowershellModule);

 

Import-Module $ModulePath;

  

function Invoke-ExoOnlineConnection{

 

    [cmdletbinding()]

 

    Param

     (

[Parameter(Mandatory=$false)]

        [switch]$Checktimer,

 

    [Parameter(mandatory=$false, valuefrompipeline=$false)]

     [switch]$RepairPSSession

 

    )

 

    begin{}

 

    process{

 

        #determine if  PsSession is loaded in memory

 

        $ExosessionInfo = Get-PsSession

 

 

 

        #calculate session time

 

        if ($global:ExosessionStartTime){

 

             $global:ExosessionTotalTime = ((Get-Date) - $global:ExosessionStartTime)

 

        }

 

        #need to loop through each session a user might have opened previously

 

        foreach ($ExosessionItem in $ExosessionInfo){

 

            #check session timer to know if we need to break the connection in advance of a timeout. Break and make new after 40 minutes.

 

            if ($ExosessionItem.ComputerName.Contains("outlook.office365.com") -and $ExosessionItem.State -eq "Opened" -and $global:ExosessionTotalTime.TotalSeconds -ge "2400"){

 

                Write-Verbose -Message "The PowerShell session has been running for $($global:ExosessionTotalTime.TotalMinutes) minutes. We need to shut it down and create a new session due to the access token expiration at 60 minutes."

 

                $ExosessionItem | Remove-PSSession

 

                Start-Sleep -Seconds 3

 

                $strSessionFound = $false

 

                $global:ExosessionTotalTime = $null #reset the timer

 

            } else { Write-Verbose -Message "The PowerShell session has been running for $($global:ExosessionTotalTime.TotalMinutes) minutes.)"}

 

 

 

            #Force repair PSSession

 

            if ($ExosessionItem.ComputerName.Contains("outlook.office365.com") -and $RepairPSSession){

 

                Write-Verbose -Message "Attempting to repair broken PowerShell session to Exchange Online using cached credential."

 

                $ExosessionItem | Remove-PSSession

 

                Start-Sleep -Seconds 3

 

                $strSessionFound = $false

 

                $global:ExosessionTotalTime = $null

 

            }elseif ($ExosessionItem.ComputerName.Contains("outlook.office365.com") -and $ExosessionItem.State -eq "Opened"){

 

                $strSessionFound = $true

 

            }

 

        }

 

 

  

        if (!$strSessionFound){

 

            Write-Verbose -Message "Creating new Exchange Online PowerShell session..."

 

            try{

 

                    $ExoSession  = New-ExoPSSession -UserPrincipalName $office365UserPrincipalName -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -ErrorAction SilentlyContinue -ErrorVariable $newOnlineSessionError

         

 

            }

 

catch{

Write-Verbose -Message "Throw error..."

               throw;

 

            } finally {

 

                if ($newOnlineSessionError) {

                 Write-Verbose -Message "Final error..."

                    throw $newOnlineSessionError

 

                }

            }

 

            Write-Verbose -Message "Importing remote PowerShell session..."

 

            $global:ExosessionStartTime = (Get-Date)

 

            Import-PSSession $ExoSession -AllowClobber | Out-Null

 

        }

 

    }

 

    end{}

 

}

 

 

## Create an Timer instance 

 

$timer = New-Object Timers.Timer

 

## Now setup the Timer instance to fire events

 

$timer.Interval = 600000

 

$timer.AutoReset = $true  # enable the event again after its been fired

 

$timer.Enabled = $true

 

 

## register your event

 

## $args[0] Timer object

 

## $args[1] Elapsed event properties

 

Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier Repair  -Action {Invoke-ExoOnlineConnection -Checktimer}

###############################################################

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
29 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies