Issues with GET-Mailbox cmdlet when running Powershell Universal Dashboard as a service

I’m trying to get some Exchange (on premise) mailboxes reports in Universal Dashboard 2.9.
My code looks something like this:

New-UDInput -Title $Domain -SubmitText “Run Report” -Content {} -Endpoint {

$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri
http://$EXCHFQDN/Powershell” -Authentication Basic -Credential $Creds -AllowRedirection
Import-PSSession $s -DisableNameChecking -AllowClobber | Out-Null
$Mailboxes = Get-Mailbox -ResultSize Unlimited


This works perfectly when running the dashboard manually by starting the dashboard.ps1 from PowerShell lSE

But when I run the dashboard as a service, it doesn’t work.
It looks like the session with the Microsoft Exchange server is crated and imported successfully.
But, despite the remote Exchange session being created and imported, $Mailboxes variable is empty for some reason.

I created Universal Dashboard service following this guide.

Does anybody have any ideas about this issue?



Does your service account have PowerShell remote permission for exchange?

Set-User -Identity -RemotePowerShellEnabled $true

Absolutely. The user is a member of Remote Management Users group on the target Exchange server.
As I mentioned, It all works well if the Dashboard is ran manually, but when I create the service and run the Dashboard using that service, it starts to fail.

If I remember correctly, the remote management group is not enough here!
The service account, with which the dashboard runs, must be explicitly given the RemotePowerShellEnabled right! This value can also be set only in the Exchange Shell.

I’m sorry, probably I didn’t put enough information about the issue.
When I run my dashboard as a service, this portion of the code fails:

$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “http://$EXCHFQDN/Powershell” -Authentication Basic -Credential $Creds -AllowRedirection
Import-PSSession $s -DisableNameChecking -AllowClobber | out-null
$Mailboxes = Get-Mailbox -ResultSize Unlimited | where {$_.displayname -ne $DiscoverySearchMailbox}

The $mailboxes variable is empty.
I already determined that the session gets established and imported correctly.
$Creds work on the Exchange server.

So, I did a little bit of troubleshooting to try to get to the bottom of it and I found that the output of Get-mailbox command is redirected to the console instead to my $Mailboxes variable. That is where the problem is.

Does anybody has any idea why that happens or how to work around it?

That’s bizarre. What happens if you use Invoke-Command?

$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “http://$EXCHFQDN/Powershell” -Authentication Basic -Credential $Creds -AllowRedirection
$Mailboxes = Invoke-Command { Get-Mailbox -ResultSize Unlimited | where {$args[0].displayname -ne $DiscoverySearchMailbox} } -Session $s -ArgumentList $_

That worked.

I’ve got a lot of “The syntax is not supported by this runspace. This can occur if the runspace is in no-language mode.” errors when manipulating the Exchange cmdlets, so looks like Exchange doesn’t like that method much.
I had to rewrite the code, but I managed to make it work.

Thanks, Adam

1 Like