Skip to content

Commit f418e1f

Browse files
author
Jill Grant
authored
Merge pull request #242935 from SnehaSudhirG/26June-Automation-newscenario
New scenario, toc update
2 parents 881a0b7 + e89630a commit f418e1f

File tree

6 files changed

+109
-11
lines changed

6 files changed

+109
-11
lines changed

articles/automation/TOC.yml

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,18 @@
2828
href: learn/automation-tutorial-runbook-textual.md
2929
- name: Create a Python 3 runbook
3030
href: learn/automation-tutorial-runbook-textual-python-3.md
31-
31+
- name: Scenarios
32+
items:
33+
- name: Manage databases in Azure SQL database using Azure Automation
34+
href: manage-sql-server-in-automation.md
35+
- name: Configure data based on STIG
36+
href: automation-dsc-configuration-based-on-stig.md
37+
- name: Configure data at scale
38+
href: automation-dsc-config-data-at-scale.md
39+
- name: Create config from existing servers
40+
href: automation-dsc-config-from-server.md
41+
- name: Convert configurations to composite resources
42+
href: automation-dsc-create-composite.md
3243
- name: Concepts
3344
items:
3445
- name: Automation account authentication overview
@@ -230,16 +241,6 @@
230241
href: automation-dsc-extension-history.md
231242
- name: Troubleshoot State Configuration issues
232243
href: troubleshoot/desired-state-configuration.md
233-
- name: Scenarios
234-
items:
235-
- name: Configure data based on STIG
236-
href: automation-dsc-configuration-based-on-stig.md
237-
- name: Configure data at scale
238-
href: automation-dsc-config-data-at-scale.md
239-
- name: Create config from existing servers
240-
href: automation-dsc-config-from-server.md
241-
- name: Convert configurations to composite resources
242-
href: automation-dsc-create-composite.md
243244
- name: Change tracking and inventory
244245
items:
245246
- name: Use Azure Monitoring Agent (AMA)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
title: Manage databases in Azure SQL databases using Azure Automation
3+
description: This article explains on how to use Azure SQL server database using a system assigned managed identity in Azure Automation.
4+
services: automation
5+
ms.date: 06/26/2023
6+
ms.topic: conceptual
7+
---
8+
9+
# Manage databases in Azure SQL database using Azure Automation
10+
11+
This article describes the procedure to connect and manage databases in Azure SQL database using Azure Automation's [system-assigned managed identity](enable-managed-identity-for-automation.md). With Azure Automation, you can manage databases in Azure SQL Database by using the [latest Az PowerShell cmdlets](https://learn.microsoft.com/powershell/module/) that are available in [Azure Az PowerShell](https://learn.microsoft.com/powershell/azure/new-azureps-module-az?view=azps-10.2.0).
12+
13+
Azure Automation has these Azure Az PowerShell cmdlets available out of the box, so that you can perform all the SQL database management tasks within the service. You can also pair these cmdlets in Azure Automation with the cmdlets of other Azure services to automate complex tasks across Azure services and across third-party systems.
14+
15+
Azure Automation can also issue T-SQL (Transact SQL) commands against the SQL servers using PowerShell.
16+
17+
To run the commands against the database, you need to do the following:
18+
- Ensure that Automation account has a system-assigned managed identity.
19+
- Provide the appropriate permissions to the Automation managed identity.
20+
- Configure the SQL server to utilize Azure Active Directory authentication.
21+
- Create a user on the SQL server that maps to the Automation account managed identity.
22+
- Create a runbook to connect and execute the commands.
23+
- (Optional) If the SQL server is protected by a firewall, create a Hybrid Runbook Worker (HRW), install the SQL modules on that server, and add the HRW IP address to the allowlist on the firewall.
24+
25+
26+
## Connect to Azure SQL database using System-assigned Managed identity
27+
28+
To allow access from the Automation system managed identity to the Azure SQL database, follow these steps:
29+
30+
1. If the Automation system managed identity is **OFF**, do the following:
31+
1. Sign in to the [Azure portal](https://portal.azure.com).
32+
1. Go to your Automation account.
33+
1. In the Automation account page, under **Account Settings**, select **Identity**.
34+
1. Under the **System assigned** tab, select the **Status** as **ON**.
35+
36+
:::image type="content" source="./media/manage-sql-server-in-automation/system-assigned-managed-identity-status-on-inline.png" alt-text="Screenshot of setting the status to ON for System assigned managed identity." lightbox="./media/manage-sql-server-in-automation/system-assigned-managed-identity-status-on-expanded.png":::
37+
38+
1. After the System Managed Identity is **ON**, you must provide the account the required access using these steps:
39+
1. In the **Automation account | Identity** page, **System assigned** tab, under permissions, select **Azure role assignments**.
40+
1. In the Azure role assignments page, select **+Add role assignment (preview)**.
41+
1. In the **Add role assignment (preview)**, select the **Scope** as *SQL*, select the **Subscription**, **Resource** from the drop-down and **Role** according to minimum required permissions, and then select **Save**.
42+
43+
:::image type="content" source="./media/manage-sql-server-in-automation/add-role-assignment-inline.png" alt-text="Screenshot of adding role assignment when the system assigned managed identity's status is set to ON." lightbox="./media/manage-sql-server-in-automation/add-role-assignment-expanded.png":::
44+
45+
1. Configure the SQL server for Active Directory authentication by using these steps:
46+
1. Go to [Azure portal](https://portal.azure.com) home page and select **SQL servers**.
47+
1. In the **SQL server** page, under **Settings**, select **Azure Active Directory**.
48+
1. Select **Set admin** to configure SQL server for AD authentication.
49+
50+
1. Add authentication on the SQL side by using these steps:
51+
1. Go to [Azure portal](https://portal.azure.com) home page and select **SQL servers**.
52+
1. In the **SQL server** page, under **Settings**, select **SQL Databases**.
53+
1. Select your database to go to the SQL database page and select **Query editor (preview)** and execute the following two queries:
54+
- CREATE USER "AutomationAccount"
55+
- FROM EXTERNAL PROVIDER WITH OBJECT_ID= `ObjectID`
56+
- EXEC sp_addrolemember `dbowner`, "AutomationAccount"
57+
- Automation account - replace with your Automation account's name
58+
- Object ID - replace with object (principal) ID for your system managed identity principal from step 1.
59+
60+
## Sample code
61+
62+
### Connection to Azure SQL Server
63+
64+
```sql
65+
if ($($env:computerName) -eq "Client") {"Runbook running on Azure Client sandbox"} else {"Runbook running on " + $env:computerName}
66+
Disable-AzContextAutosave -Scope Process
67+
Connect-AzAccount -Identity
68+
$Token = (Get-AZAccessToken -ResourceUrl https://database.windows.net).Token
69+
Invoke-Sqlcmd -ServerInstance azuresqlserverxyz.database.windows.net -Database MyDBxyz -AccessToken $token -query 'select * from TableXYZ'
70+
```
71+
### Check account permissions on the SQL side
72+
73+
```sql
74+
SELECT roles.[name] as role_name, members.name as [user_name]
75+
from sys.database_role_members
76+
Join sys.database_principals roles on database_role_members.role_principal_id= roles.principal_id
77+
join sys.database_principals members on database_role_members.member_principal_id=members.principal_id
78+
Order By
79+
roles.[name], members.[name]
80+
```
81+
82+
> [!NOTE]
83+
> When a SQL server is running behind a firewall, you must run the Azure Automation runbook on a machine in your own network. Ensure that you configure this machine as a Hybrid Runbook Worker so that the IP address or network is not blocked by the firewall. For more information on how to configure a machine as a Hybrid Worker, see [create a hybrid worker](extension-based-hybrid-runbook-worker-install.md).
84+
85+
### Use Hybrid worker
86+
When you use a Hybrid worker, the modules that your runbook uses, must be installed locally from an elevated PowerShell prompt. For example, `- Install-module Az.Accounts and Install-module SqlServer`. To find the required module names, run a command on each cmdlet and then check the source. For example, to check module name for cmdlet `Connect-AzAccounts` which is part of the Az.Account module, run the command: `get-command Connect-AzAccount`
87+
88+
> [!NOTE]
89+
> We recommend that you add the following code on the top of any runbook that's intended to run on a Hybrid worker: `if ($($env:computerName) -eq "CLIENT") {"Runbook running on Azure CLIENT"} else {"Runbook running on " + $env:computerName}`. The code allows you to see the node it's running on and in case you accidentally run it on Azure cloud instead of the Hybrid worker, then it helps to determine the reason a runbook didn't work.
90+
91+
92+
## Next steps
93+
94+
* For details of credential use, see [Manage credentials in Azure Automation](shared-resources/credentials.md).
95+
* For information about modules, see [Manage modules in Azure Automation](shared-resources/modules.md).
96+
* If you need to start a runbook, see [Start a runbook in Azure Automation](start-runbooks.md).
97+
* For PowerShell details, see [PowerShell Docs](/powershell/scripting/overview).
89.6 KB
Loading
89.6 KB
Loading
Loading
143 KB
Loading

0 commit comments

Comments
 (0)