Secure connection to Exchange 2013 with Powershell

Summary:

Using Powershell to make a secure connection to Exchange. Why? Because no password should be stored plain text, ANYWHERE!

Versions:

Powershell: 4.0 or greater

Exchange: 2013

Instructions:

First you have to create a secure string from the password of an account. This should be done on the server/workstation that will make the connection in the future. Do this by executing:

read-host -assecurestring | convertfrom-securestring | out-file C:\temp\Cred\securestring.txt

As a result, you should get a file called ‘securestring.txt’ on the location specified. Please open it for verification:

  • You will not be able to see your password.
  • The file actually has content.

 

Next chunck of code is used for connecting to the Exchange using the secured file made in the previous step.

$ExchangeServerUrl = “http://exchangeserver.domain.local/powershell/”

#Build EXChange Remote Secure Connection
#Build $cred variabele using secure password file

$username = “Domain\ServiceAccount”
$password = cat C:\temp\Cred\securestring.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential `
-argumentlist $username, $password

#Build and import secure session to Exchange 2013 on ExchangeServer using $cred credentials
#Now Exchange CMDlets can be used as if it concerns an Exchange Posh session
$session = new-pssession -configurationname MIcrosoft.eXchange -connectionuri $ExchangeServerUrl -authentication kerberos -credential $cred
Import-PSSession $Session

You are now connected to your Exchange server using Powershell. This allows you to perform various Exchange tasks from Powershell without opening the Exchange Console:

  • Enable-MailUser
  • Set-MailUser
  • Get-MailUser
  • Get-MailContact
  • ….

 

When done, don’t forget you should clear your object from memory (should be done¬†with any object):

Remove-PSSession $Session

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.