{"id":2729,"date":"2018-01-26T11:19:48","date_gmt":"2018-01-26T10:19:48","guid":{"rendered":"https:\/\/www.lieben.nu\/liebensraum\/?p=2729"},"modified":"2018-01-26T11:19:48","modified_gmt":"2018-01-26T10:19:48","slug":"exchange-online-reconnect-script-v2","status":"publish","type":"post","link":"https:\/\/lieben.nu\/liebensraum\/2018\/01\/exchange-online-reconnect-script-v2\/","title":{"rendered":"Exchange Online reconnect script v2"},"content":{"rendered":"<p>A few weeks ago I posted a script that would automatically, periodically, reconnect to Exchange Online. In field testing it would still prompt for credentials after 1-2 days, whatever I did.<\/p>\n<p>So I took a different route and am now rewriting Microsofts&#8217; module on the fly to no longer prompt for credentials. If you use below function to connect to Exchange Online, you should never receive reconnect prompts \ud83d\ude42<\/p>\n<p><strong>disclaimer: don&#8217;t overwrite $o365Creds with invalid creds elsewhere in your script as those are used globally.<\/strong><\/p>\n<pre><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n\nfunction buildResilientExchangeOnlineSession {\n    Param(\n        &#x5B;Parameter(Mandatory=$true)]$o365Creds,\n        $commandPrefix\n    )\n    Write-Verbose &amp;quot;Connecting to Exchange Online&amp;quot;\n    Set-Variable -Scope Global -Name o365Creds -Value $o365Creds -Force\n    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https:\/\/outlook.office365.com\/powershell-liveid\/ -Credential $o365Creds -Authentication Basic -AllowRedirection\n    Import-PSSession $Session -AllowClobber -DisableNameChecking\n    Write-Verbose &amp;quot;Connected to Exchange Online, exporting module...&amp;quot;\n    $temporaryModulePath = (Join-Path $Env:TEMP -ChildPath &amp;quot;temporaryEXOModule&amp;quot;)\n    $res = Export-PSSession -Session $Session -CommandName * -OutputModule $temporaryModulePath -AllowClobber -Force\n    $temporaryModulePath = Join-Path $temporaryModulePath -ChildPath &amp;quot;temporaryEXOModule.psm1&amp;quot;\n    Write-Verbose &amp;quot;Rewriting Exchange Online module, please wait...&amp;quot;\n      $regex='^.*\\bhost\\.UI\\.PromptForCredential\\b.*$'\n    (Get-Content $temporaryModulePath) -replace $regex, &amp;quot;-Credential `$global:o365Creds ``&amp;quot; | Set-Content $temporaryModulePath\n    $Session | Remove-PSSession -Confirm:$False\n    Write-Verbose &amp;quot;Module rewritten, re-importing...&amp;quot;\n    if($commandPrefix){\n        Import-Module -Name $temporaryModulePath -Prefix $commandPrefix -DisableNameChecking -WarningAction SilentlyContinue -Force\n        Write-Verbose &amp;quot;Module imported, you may now use all Exchange Online commands using $commandPrefix as prefix&amp;quot;\n    }else{\n        Import-Module -Name $temporaryModulePath -DisableNameChecking -WarningAction SilentlyContinue -Force\n        Write-Verbose &amp;quot;Module imported, you may now use all Exchange Online commands&amp;quot;\n    }\n    return $temporaryModulePath\n}\n\n<\/pre>\n<p>download:\u00a0<a href=\"https:\/\/gitlab.com\/Lieben\/assortedFunctions\/blob\/master\/buildResilientExchangeOnlineSession.ps1\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/gitlab.com\/Lieben\/assortedFunctions\/blob\/master\/buildResilientExchangeOnlineSession.ps1<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few weeks ago I posted a script that would automatically, periodically, reconnect to Exchange Online. In field testing it would still prompt for credentials after 1-2 days, whatever I did. So I took a different route and am now rewriting Microsofts&#8217; module on the fly to no longer prompt for credentials. If you use &hellip; <a href=\"https:\/\/lieben.nu\/liebensraum\/2018\/01\/exchange-online-reconnect-script-v2\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Exchange Online reconnect script v2<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[4,18,32,39],"tags":[],"class_list":["post-2729","post","type-post","status-publish","format-standard","hentry","category-automation","category-exchange-online","category-office-365","category-powershell"],"_links":{"self":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/2729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/comments?post=2729"}],"version-history":[{"count":0,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/2729\/revisions"}],"wp:attachment":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/media?parent=2729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/categories?post=2729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/tags?post=2729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}