|
| 1 | +--- |
| 2 | +title: Manage Office 365 services using Azure Automation |
| 3 | +description: Tells how to use Azure Automation to manage Office 365 subscription services. |
| 4 | +services: automation |
| 5 | +ms.date: 04/01/2020 |
| 6 | +ms.topic: conceptual |
| 7 | +--- |
| 8 | + |
| 9 | +# Manage Office 365 services using Azure Automation |
| 10 | + |
| 11 | +You can use Azure Automation for management of Office 365 subscription services, for products such as Microsoft Word and Microsoft Outlook. Interactions with Office 365 are enabled by [Azure Active Directory (Azure AD)](https://docs.microsoft.com/azure/active-directory/fundamentals/active-directory-whatis). See [Use Azure AD in Azure Automation to authenticate to Azure](automation-use-azure-ad.md). |
| 12 | + |
| 13 | +>[!NOTE] |
| 14 | +>This article has been updated to use the new Azure PowerShell Az module. You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. To learn more about the new Az module and AzureRM compatibility, see [Introducing the new Azure PowerShell Az module](https://docs.microsoft.com/powershell/azure/new-azureps-module-az?view=azps-3.5.0). For Az module installation instructions on your Hybrid Runbook Worker, see [Install the Azure PowerShell Module](https://docs.microsoft.com/powershell/azure/install-az-ps?view=azps-3.5.0). For your Automation account, you can update your modules to the latest version using [How to update Azure PowerShell modules in Azure Automation](automation-update-azure-modules.md). |
| 15 | +
|
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +You need the following to manage Office 365 subscription services in Azure Automation. |
| 19 | + |
| 20 | +* An Azure subscription. See [Subscription decision guide](https://docs.microsoft.com/azure/cloud-adoption-framework/decision-guides/subscriptions/). |
| 21 | +* An Automation object in Azure to hold the user account credentials and runbooks. See [An introduction to Azure Automation](https://docs.microsoft.com/azure/automation/automation-intro). |
| 22 | +* Azure AD. See [Use Azure AD in Azure Automation to authenticate to Azure](automation-use-azure-ad.md). |
| 23 | +* An Office 365 tenant, with an account. See [Set up your Office 365 tenant](https://docs.microsoft.com/sharepoint/dev/spfx/set-up-your-developer-tenant). |
| 24 | + |
| 25 | +## Installing the MSOnline and MSOnlineExt modules |
| 26 | + |
| 27 | +Use of Office 365 within Azure Automation requires Microsoft Azure Active Directory for Windows PowerShell (`MSOnline` module). You'll also need the module [`MSOnlineExt`](https://www.powershellgallery.com/packages/MSOnlineExt/1.0.35), which simplifies Azure AD management in single- and multi-tenant environments. Install the modules as described in [Use Azure AD in Azure Automation to authenticate to Azure](automation-use-azure-ad.md). |
| 28 | + |
| 29 | +>[!NOTE] |
| 30 | +>To use MSOnline PowerShell, you must be a member of Azure AD. Guest users can't use the module. |
| 31 | +
|
| 32 | +## Creating an Azure Automation account |
| 33 | + |
| 34 | +To complete the steps in this article, you need an account in Azure Automation. See [Create an Azure Automation account](automation-quickstart-create-account.md). |
| 35 | + |
| 36 | +## Adding MSOnline and MSOnlineExt as assets |
| 37 | + |
| 38 | +Now add the installed MSOnline and MSOnlineExt modules to enable Office 365 functionality. Refer to [Manage modules in Azure Automation](shared-resources/modules.md). |
| 39 | + |
| 40 | +1. In the Azure portal, select **Automation Accounts**. |
| 41 | +2. Choose your Automation account. |
| 42 | +3. Select **Modules Gallery** under **Shared Resources**. |
| 43 | +4. Search for MSOnline. |
| 44 | +5. Select the `MSOnline` PowerShell module and click **Import** to import the module as an asset. |
| 45 | +6. Repeat steps 4 and 5 to locate and import the `MSOnlineExt` module. |
| 46 | + |
| 47 | +## Creating a credential asset (optional) |
| 48 | + |
| 49 | +It's optional to create a credential asset for the Office 365 administrative user who has permissions to run your script. It can help, though, to keep from exposing user names and passwords inside PowerShell scripts. For instructions, see [Creating a credential asset](automation-use-azure-ad.md#creating-a-credential-asset). |
| 50 | + |
| 51 | +## Creating an Office 365 service account |
| 52 | + |
| 53 | +To run Office 365 subscription services, you need an Office 365 service account with permissions to do what you want. You can use one global administrator account, one account per service, or have one function or script to execute. In any case, the service account requires a complex and secure password. See [Set up Office 365 for business](https://docs.microsoft.com/microsoft-365/admin/setup/setup?view=o365-worldwide). |
| 54 | + |
| 55 | +## Connecting to the Azure AD online service |
| 56 | + |
| 57 | +>[!NOTE] |
| 58 | +>To use the MSOnline module cmdlets, you must run them from Windows PowerShell. PowerShell Core does not support these cmdlets. |
| 59 | +
|
| 60 | +You can use the MSOnline module to connect to Azure AD from the Office 365 subscription. The connection uses an Office 365 user name and password or uses multi-factor authentication (MFA). You can connect using the Azure portal or a Windows PowerShell command prompt (does not have to be elevated). |
| 61 | + |
| 62 | +A PowerShell example is shown below. The [Get-Credential](https://docs.microsoft.com/powershell/module/microsoft.powershell.security/get-credential?view=powershell-7) cmdlet prompts for credentials and stores them in the `Msolcred` variable. Then the [Connect-MsolService](https://docs.microsoft.com/powershell/module/msonline/connect-msolservice?view=azureadps-1.0) cmdlet uses the credentials to connect to the Azure directory online service. If you want to connect to a specific Azure environment, use the `AzureEnvironment` parameter. |
| 63 | + |
| 64 | +```powershell |
| 65 | +$Msolcred = Get-Credential |
| 66 | +Connect-MsolService -Credential $MsolCred -AzureEnvironment "AzureCloud" |
| 67 | +``` |
| 68 | + |
| 69 | +If you don't receive any errors, you've connected successfully. A quick test is to run an Office 365 cmdlet, for example, `Get-MsolUser`, and see the results. If you receive errors, note that a common problem is an incorrect password. |
| 70 | + |
| 71 | +>[!NOTE] |
| 72 | +>You can also use the AzureRM module or the Az module to connect to Azure AD from the Office 365 subscription. The main connection cmdlet is [Connect-AzureAD](https://docs.microsoft.com/powershell/module/azuread/connect-azuread?view=azureadps-2.0). This cmdlet supports the `AzureEnvironmentName` parameter for specific Office 365 environments. |
| 73 | +
|
| 74 | +## Creating a PowerShell runbook from an existing script |
| 75 | + |
| 76 | +You access Office 365 functionality from a PowerShell script. Here's an example of a script for a credential named `Office-Credentials` with user name of `[email protected]`. It uses `Get-AutomationPSCredential` to import the Office 365 credential. |
| 77 | + |
| 78 | +```powershell |
| 79 | +$emailFromAddress = "[email protected]" |
| 80 | +$emailToAddress = "[email protected]" |
| 81 | +$emailSMTPServer = "outlook.office365.com" |
| 82 | +$emailSubject = "Office 365 License Report" |
| 83 | +
|
| 84 | +$credObject = Get-AutomationPSCredential -Name "Office-Credentials" |
| 85 | +Connect-MsolService -Credential $credObject |
| 86 | +
|
| 87 | +$O365Licenses = Get-MsolAccountSku | Out-String |
| 88 | +Send-MailMessage -Credential $credObject -From $emailFromAddress -To $emailToAddress -Subject $emailSubject -Body |
| 89 | +
|
| 90 | +$O365Licenses -SmtpServer $emailSMTPServer -UseSSL |
| 91 | +``` |
| 92 | + |
| 93 | +## Running the script in a runbook |
| 94 | + |
| 95 | +You can use your script in an Azure Automation runbook. For example purposes, we'll use the PowerShell runbook type. |
| 96 | + |
| 97 | +1. Create a new PowerShell runbook. Refer to [Create an Azure Automation runbook](https://docs.microsoft.com/azure/automation/automation-quickstart-create-runbook). |
| 98 | +2. From your Automation account, select **Runbooks** under **Process Automation**. |
| 99 | +3. Select the new runbook and click **Edit**. |
| 100 | +4. Copy your script and paste it into the textual editor for the runbook. |
| 101 | +5. Select **ASSETS**, then expand **Credentials** and verify that the Office 365 credential is there. |
| 102 | +6. Click **Save**. |
| 103 | +7. Select **Test pane**, then click **Start** to begin testing your runbook. See [Manage runbooks in Azure Automation](https://docs.microsoft.com/azure/automation/manage-runbooks). |
| 104 | +8. When testing is complete, exit from the Test pane. |
| 105 | + |
| 106 | +## Publishing and scheduling the runbook |
| 107 | + |
| 108 | +To publish and then schedule your runbook, see [Manage runbooks in Azure Automation](https://docs.microsoft.com/azure/automation/manage-runbooks). |
| 109 | + |
| 110 | +## Next steps |
| 111 | + |
| 112 | +* You can find information about Automation credential assets in [Credential assets in Azure Automation](shared-resources/credentials.md). |
| 113 | +* See [Manage modules in Azure Automation](shared-resources/modules.md) to find out how to work with Automation modules. |
| 114 | +* For an overview of runbook management, see [Manage runbooks in Azure Automation](https://docs.microsoft.com/azure/automation/manage-runbooks). |
| 115 | +* To learn more about the methods that can be used to start a runbook in Azure Automation, see [Starting a runbook in Azure Automation](automation-starting-a-runbook.md). |
| 116 | +* For more information about PowerShell, including language reference and learning modules, see the [PowerShell Docs](https://docs.microsoft.com/powershell/scripting/overview). |
0 commit comments