Microsoft Exchange session best practice

Simple question, really: Is there a recommended method by which to run queries against mailbox servers, such as the $session being shared across endpoints etc.?

Hello @fromtheinternet I have not personally designed a dashboard to connect to a mailserver as of yet…However I do have several scheduled tasks on my computer which gather information from certain mailboxes and output it to a CSV…I could then load that CSV into a dashboard…so as the exchange commands need importing from a session, and you then need to close the session, just thinking this could cause lag on your dashboard…so depending what you want to display is doing a CSV option a route that you would use? If so you could then load into a grid and display the information that way, or a chart.

$Username = "YOUR-DOMAIN\Administrator"
$SecurePassword = Get-Content C:\Path-To-EncryptedPassword\password.txt | ConvertTo-SecureString
$UserCredential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$SecurePassword
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://YOUR-MAIL-SERVERNAME/PowerShell/ -Credential $UserCredential -Authentication Kerberos
Import-PSSession $Session
# run your exchange commands commands
Remove-PSSession $Session

Is how I would go about connecting to an Exchange server, I have this as ascheduled job and it works without fail. I personally would use results from a scheduled job if you need to run Exchange cmdlets in your dashboard?

1 Like

Cheers for the reply.

In the end I went with the Exchange pssnapin after installing the exchange tools.

I created a function that adds the snapin and then performs the required queries. For my case I then call that function from a new-udendpoint that runs on a schedule, saving the results to a $cache:variable.

It works great, and avoids the issues with making multiple remote session calls (though I suppose that would be mostly mitigated with a scheduled endpoint call, anyway).

@fromtheinternet: What exchange are you running? I’m running in 2013.
@psDevUK I have been unable to follow the code above in Powershell Universal Dashboard. However, normal PowerShell works wonders.

Here is the error message I get using code above with my company information:
KeyNotValid

Sorry for the late reply!

Exchange 2016. You need to install the exchange tools on the dashboard box.