Automating Exchange Shell With Ansible Via Powershell.

Automating Exchange Shell With Ansible Via Powershell.

Lets take a look at how I've been automating Exchange Shell via Powershell using Ansible.

I have Exchange Shell installed so that Office 365 email accounts can be created.

I need to run the following commands :-

Enable-Remotemailbox?test.user?-RemoteRoutingAddress?test.user@0365.com

set-remotemailbox test.user -Emailaddresspolicyenabled $false

Set-remotemailbox test.user -primarysmtpaddress [email protected]

 Set-remotemailbox $username -Emailaddresses @{Remove="[email protected]"}        

  • I need to create the user in 0365.
  • Untick the "Automatically update email addresses based on the email address policy applied to this recipient", to allow me to override the default policy.
  • Change the default email address.
  • Remove the default email address created by the default policy. This is partly a workaround due to an ongoing service migration.

Exchange Shell

The above commands work direct in Exchange Shell, however I need a way of getting to Exchange Shell from Powershell, to allow me to use the win_shell module in Ansible. With this module I can see the output of the sent commands.

This can be achieved with the following command.

Add-PSSnapin?Microsoft.Exchange.Management.PowerShell.SnapIn        

So In Powershell our full code would look like this.

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn

Enable-Remotemailbox test.user -RemoteRoutingAddress [email protected]

set-remotemailbox test.user -Emailaddresspolicyenabled $false

Set-remotemailbox test.user -primarysmtpaddress [email protected]

 Set-remotemailbox $username -Emailaddresses @{Remove="[email protected]"}        

This allows Powershell to invoke Exchange Shell and pass the relevant commands that are native in Exchange Shell.

Obviously we need to pass in variables, and those variables are in Ansible, which in turn creates an "Escaping" problem, due to special characters and double quotes. Here's the most efficient way I have found to handle this, If you know of others please comment, I'm certainly no Powershell expert.

Note in the snippet below I have instantiated some variables for brevity, in the real world code they are passed from another role.

---
- name: Email Creation Via Exchange Shell
  hosts: localhost
  gather_facts: no
 
  vars:
    _name: test.user
    _O365_domain: '@O365.com'
    _default_email: new.com

- set_fact:
    _joined_O365: "{{ _name }}{{ _O365_domain}}"
    _default_email: "{{ _name }}@{{ _default_email }}"
    _remove_email: "{{ _name }}@old.com"
 
- name: Create Email Using Exchange Management Shell
  ansible.windows.win_shell: |
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
    Enable-Remotemailbox "{{ _name }}" -RemoteRoutingAddress "{{ _joined_O365 }}"
    set-remotemailbox "{{ _name }}" -Emailaddresspolicyenabled $false
    Set-remotemailbox "{{ _name }}" -primarysmtpaddress "{{ _default_email }}"        

The first section above is just simple variable swapping, Powershell doesn't mandate single or double quotes in this instance, even though the documentation states the use of " ".

However things change in the next snippet. The command needs the following characters @, { } and " " in the syntax and these are mandatory.

- name: Remove Default Email
  ansible.windows.win_powershell:
    script: |
      $username = '{{ _name }}'
      $remove = '{{ _remove_email }}'
      Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
      Set-remotemailbox $username -Emailaddresses @{Remove="$remove"}
        

This is where I've amended things. I found it cleaner instead of trying to escape quotes to simply create variables in Powershell that reference the Ansible variables.

This allows me to keep the same formatting and is easily readable to Microsoft engineers.

Hope this has been of some use for those having similar issues with Powershell.

Boyd Tweed

Network Automation | CCNP Network Engineer | Ansible | Python | Linux | Napalm | API | Networking | Git | Infrastructure as code | REST | Nornir

1 年

Great share my friend!!!!

Neil Perkins

Senior Strategic Accounts Director - Public Sector - EMEA - Enterprise Strategy Lead Industry

1 年

Nice my man

要查看或添加评论,请登录

Stephen Paynter的更多文章

  • Creating a Docker GitLab Runner

    Creating a Docker GitLab Runner

    Ever wondered how to set up your own GitLab Runner using Docker. Let's dive in! Step 1: Prerequisites Before we begin…

    3 条评论
  • Ansible-Navigator: The Basics

    Ansible-Navigator: The Basics

    Im now solely using Ansible-Navigator to run Ansible Playbooks within my home lab. All my runtimes are done within…

    3 条评论
  • CRIB SHEET: Installing Ansible AWX with Kubernetes On Ubuntu 20.04.

    CRIB SHEET: Installing Ansible AWX with Kubernetes On Ubuntu 20.04.

    As of writing (September 2023) this is the quickest and lightest way I've found of installing Ansible AWX on a fresh…

    2 条评论
  • What is Git Bundle?

    What is Git Bundle?

    Git Bundle is a feature in Git that allows developers to package repositories into a single file for easy sharing. This…

    2 条评论
  • The Devnet Diaries : Flask

    The Devnet Diaries : Flask

    Creating an API with Flask is a relatively simple process that can be accomplished in just a few steps. Flask is a…

    3 条评论
  • Python Classes, a simplified look.

    Python Classes, a simplified look.

    By what you wrote, you are missing a critical piece of understanding: the difference between a class and an object…

    1 条评论
  • Running the Cisco CWS DevNet Expert Image

    Running the Cisco CWS DevNet Expert Image

    I've had some fun over the last few days trying to get the Devnet Expert Candidate Workstation image running. Here's…

    6 条评论
  • Ansible AWX on Docker

    Ansible AWX on Docker

    A step by step installation process for Ansible AWX on Ubuntu 20.4 on Docker.

    10 条评论
  • Installing Ansible AWX on Ubuntu 20.4 With Kubernetes KS3

    Installing Ansible AWX on Ubuntu 20.4 With Kubernetes KS3

    A workflow to get you up and running with Ansible AWX. Minimum requirements , Ubuntu 20.

    15 条评论
  • Cisco 300-435 ENAUTO Crib Sheet 4

    Cisco 300-435 ENAUTO Crib Sheet 4

    Part 4 in a series of crib notes I'm using to study for the Cisco ENAUTO DevNet exam. Its been a while since I posted…

    1 条评论

社区洞察

其他会员也浏览了