Is there any way to check if Outlook can successfully connect to Exchange from Powershell? I would like to schedule that script for logging purposes.
After some research, I managed to cook up the following:
$outlook = New-Object -ComObject Outlook.Application
Start-Sleep -s 5
$session = $outlook.session
$inbox = $session.GetDefaultFolder([Microsoft.Office.Interop.Outlook.olDefaultFolders]::olFolderInbox)
$date = Get-Date
If ($session.Offline) {
$output = "Fail, mode $($session.ExchangeConnectionMode.ToString())"
} Else {
$output = "Success"
}
"[$date] $output `n" | Out-File -Append "$HOME\ol_log.txt" -Encoding "UTF8"
$outlook.Quit()
This writes "Success" even if I pull the cable. A different way to check I found is
$validStates = "olCachedConnectedDrizzle", "olCachedConnectedDrizzle", "olCachedConnectedHeaders", "olOnline"
if ($validStates -contains $session.ExchangeConnectionMode.ToString()) {...}
...which always fails, since connection mode here is olCachedDisconnected for some reason. If I manually open Outlook, I can see the connection is OK. All of this is happening in an internal network, so no O365 and such.
PS: Please tell me if the question is more appropriate for superuser.com, I'll move it there if need be.