čtvrtek 25. dubna 2019

When azure runbook cannot connect to Exchange server through hybrid worker

This is typical SAAS (read S*it as a service). Imagine, you have simple powershell runbook script which is trying to connect to Exchange server from hybrid worker:

$Credentials = Get-AutomationPSCredential -Name 'an_onprem_acount'
$exchOnPremURL = "https://exchangeserver.local/PowerShell/"
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $exchOnPremURL `
-Credential $Credentials -Authentication Basic -AllowRedirection
Import-PSSession -Session $Session -DisableNameChecking:$true -AllowClobber:$true

You can experience tons of different results except one you would expect.... working runbook :) Depends on your settings for hybrid worker, e.g. which account you are using to run the worker you can most probably see, that your runbook is constantly Queued or it is keep restarting itself until it is suspended.

Using google you can find few "solutions" and crazy workarounds like this, however, the solution is easy. You just need to send output of Import-PSSession command to/dev/null ... sorry, Out-Null:

$Credentials = Get-AutomationPSCredential -Name 'an_onprem_acount'
$exchOnPremURL = "https://exchangeserver.local/PowerShell/"
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $exchOnPremURL `
-Credential $Credentials -Authentication Basic -AllowRedirection
Import-PSSession -Session $Session -DisableNameChecking:$true -AllowClobber:$true | Out-Null

strange... right? but no one who works with MS products is not surprised....

Thank you to Bogdan G. S. and Lubomir H.