Skip to content

Commit 03f69eb

Browse files
20231114 powershell
1 parent cf7b4d1 commit 03f69eb

11 files changed

Lines changed: 126 additions & 20 deletions

azure-sql/database/elastic-jobs-overview.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: "Learn about how you can use elastic jobs to run Transact-SQL (T-SQ
44
author: WilliamDAssafMSFT
55
ms.author: wiassaf
66
ms.reviewer: srinia
7-
ms.date: 11/06/2023
7+
ms.date: 11/13/2023
88
ms.service: sql-database
99
ms.subservice: elastic-jobs
1010
ms.topic: overview
@@ -171,6 +171,7 @@ For more information on UMI in Azure SQL Database, see [Managed identities for A
171171

172172
> [!IMPORTANT]
173173
> When using Microsoft Entra ID authentication, create your `jobuser` user from that Microsoft Entra ID in every target database. Grant that user the permissions needed to execute your job(s) in each target database.
174+
174175
Using a system-assigned managed identity (SMI) is not supported.
175176

176177
### Authentication via database-scoped credentials
@@ -222,6 +223,9 @@ During job agent creation, a schema, tables, and a role called *jobs_reader* are
222223
|---------|---------|---------|
223224
|**jobs_reader** | SELECT | None |
224225

226+
> [!CAUTION]
227+
> You should not update internal catalog views in the *job database*, such as [jobs.target_group_members](/sql/relational-databases/system-catalog-views/jobs-target-group-members-elastic-jobs-transact-sql?view=azuresqldb-current&preserve-view=true). Manually changing these catalog views can corrupt the *job database* and cause failure. These views are for read-only querying only. You can use the stored procedures on your *job database* to add/delete target groups/members, such as [jobs.sp_add_target_group_member](/sql/relational-databases/system-stored-procedures/sp-add-target-group-member-elastic-jobs-transact-sql?view=azuresqldb-current&preserve-view=true).
228+
225229
> [!IMPORTANT]
226230
> Consider the security implications before granting any elevated access to the *job database*. A malicious user with permissions to create or edit jobs could create or edit a job that uses a stored credential to connect to a database under the malicious user's control, which could allow the malicious user to determine the credential's password or execute malicious commands.
227231
@@ -278,7 +282,7 @@ Most environments require less than 100 concurrent jobs at any time, so JA100 is
278282

279283
Exceeding the job agent's concurrency capacity tier with job targets will create queuing delays for some target databases/servers. For example, if you start a job with 110 target in the JA100 tier, 10 jobs will wait to start until others finish.
280284

281-
The tier or service objective of an elastic job agent can be modified through the Azure portal, [PowerShell](/powershell/module/az.sql/set-azsqlelasticjobagent), or [the Job Agents REST API](/rest/api/sql/2021-02-01-preview/job-agents). <!-- TODO update /2021-02-01/ when available --> For an example, see [Scale the job agent](elastic-jobs-tutorial.md#scale-the-job-agent).
285+
The tier or service objective of an elastic job agent can be modified through the Azure portal, [PowerShell](/powershell/module/az.sql/set-azsqlelasticjobagent), or [the Job Agents REST API](/rest/api/sql/job-agents). For an example, see [Scale the job agent](elastic-jobs-tutorial.md#scale-the-job-agent).
282286

283287
### Limit job impact on elastic pools
284288

azure-sql/database/elastic-jobs-powershell-create.md

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Learn how to create an elastic job agent and run scripts across man
44
author: WilliamDAssafMSFT
55
ms.author: wiassaf
66
ms.reviewer: srinia
7-
ms.date: 11/02/2023
7+
ms.date: 11/14/2023
88
ms.service: sql-database
99
ms.subservice: elastic-jobs
1010
ms.topic: tutorial
@@ -35,6 +35,8 @@ In this end-to-end tutorial, you learn the steps required to run a query across
3535

3636
Elastic database jobs have a set of PowerShell cmdlets.
3737

38+
These cmdlets were updated in November 2023.
39+
3840
### Install the latest elastic jobs cmdlets
3941

4042
If you don't have an Azure subscription, [create a free account](https://azure.microsoft.com/free/) before you begin.
@@ -169,28 +171,85 @@ An elastic job agent is an Azure resource for creating, running, and managing jo
169171

170172
The [New-AzSqlElasticJobAgent](/powershell/module/az.sql/new-azsqlelasticjobagent) cmdlet requires a database in Azure SQL Database to already exist, so the `resourceGroupName`, `serverName`, and `databaseName` parameters must all point to existing resources. Similarly, [Set-AzSqlElasticJobAgent](/powershell/module/az.sql/set-azsqlelasticjobagent) can be used to modify the elastic job agent.
171173

174+
To create a new elastic job agent using Microsoft Entra authentication with a user-assigned managed identity, use the `IdentityType` and `IdentityID` arguments of `New-AzSqlElasticJobAgent`:
175+
172176
```powershell
173177
Write-Output "Creating job agent..."
174178
$agentName = Read-Host "Please enter a name for your new elastic job agent"
175179
$parameters = @{
176180
Name = $agentName
181+
IdentityType = "UserAssigned"
182+
IdentityID = "/subscriptions/abcd1234-caaf-4ba9-875d-f1234/resourceGroups/contoso-jobDemoRG/providers/Microsoft.ManagedIdentity/userAssignedIdentities/contoso-UMI"
177183
}
178184
$jobAgent = $jobDatabase | New-AzSqlElasticJobAgent @parameters
179185
$jobAgent
180186
```
181187

188+
To create a new elastic job agent using database-scoped credentials, `IdentityType` and `IdentityID` are not provided.
189+
182190
## Create the job authentication
183191

184192
The elastic job agent must be able to authenticate to each target server or database.
185193

186194
As covered in [Create job agent authentication](elastic-jobs-tutorial.md#create-job-agent-authentication):
187195

188-
- Use database users mapped to user-assigned managed identity (UMI) to authenticate to target server(s)/database(s).
189-
- Using a UMI with Microsoft Entra authentication (formerly Azure Active Directory) is the recommended method.
190-
- Using PowerShell cmdlets to configure Microsoft Entra authentication is currently not supported. See [Create and manage elastic jobs by using T-SQL (preview)](elastic-jobs-tsql-create-manage.md).
196+
- Use database users mapped to [user-assigned managed identity (UMI) to authenticate to target server(s)/database(s)](#use-microsoft-entra-authentication-with-a-umi-for-authentication-to-targets).
197+
- Using a UMI with Microsoft Entra authentication (formerly Azure Active Directory) is the recommended method. PowerShell cmdlets now have new arguments to support Microsoft Entra authentication with a UMI.
198+
- This is the recommended authentication method.
191199
- Use database users mapped to [database-scoped credentials](#create-the-job-credentials) in each database.
192200
- Previously, database-scoped credentials were the only option for the elastic job agent to authenticate to targets.
193201

202+
### Use Microsoft Entra authentication with a UMI for authentication to targets
203+
204+
To use the recommended method of Microsoft Entra (formerly Azure Active Directory) authentication to a user-assigned managed identity (UMI), follow these steps. The elastic job agent connects to the desired target logical server(s)/databases(s) via Entra authentication.
205+
206+
In addition to the login and database users, note the addition of the `GRANT` commands in the following script. These permissions are required for the script we chose for this example job. Your jobs may require different permissions. Because the example creates a new table in the targeted databases, the database user in each target database needs the proper permissions to successfully run.
207+
208+
In each of the target server(s)/database(s), create a contained user mapped to the UMI.
209+
210+
- If the elastic job has logical server or pool targets, you must create the contained user mapped to the UMI in the `master` database of the target logical server.
211+
- For example, to create a contained database login in the `master` database, and a user in the user database, based on the user-assigned managed identity (UMI) named `job-agent-UMI`:
212+
213+
```powershell
214+
$targetServer = '<target server name>'
215+
$adminLogin = '<username>'
216+
$adminPassword = '<password>'
217+
218+
# For the target logical server, in the master database
219+
# Create the login named [job-agent-UMI] based on the UMI [job-agent-UMI], and a user
220+
$params = @{
221+
'database' = 'master'
222+
'serverInstance' = $targetServer.ServerName + '.database.windows.net'
223+
'username' = $adminLogin
224+
'password' = $adminPassword
225+
'outputSqlErrors' = $true
226+
'query' = 'CREATE LOGIN [job-agent-UMI] FROM EXTERNAL PROVIDER;'
227+
}
228+
Invoke-SqlCmd @params
229+
$params.query = "CREATE USER [job-agent-UMI] FROM LOGIN [job-agent-UMI]"
230+
Invoke-SqlCmd @params
231+
232+
# For each target database in the target logical server
233+
# Create a database user from the job-agent-UMI login
234+
$targetDatabases = @( $db1.DatabaseName, $Db2.DatabaseName )
235+
$createJobUserScript = "CREATE USER [job-agent-UMI] FROM LOGIN [job-agent-UMI]"
236+
237+
# Grant permissions as necessary. For example ALTER and CREATE TABLE:
238+
$grantAlterSchemaScript = "GRANT ALTER ON SCHEMA::dbo TO [job-agent-UMI]"
239+
$grantCreateScript = "GRANT CREATE TABLE TO [job-agent-UMI]"
240+
241+
$targetDatabases | % {
242+
$params.database = $_
243+
$params.query = $createJobUserScript
244+
Invoke-SqlCmd @params
245+
$params.query = $grantAlterSchemaScript
246+
Invoke-SqlCmd @params
247+
$params.query = $grantCreateScript
248+
Invoke-SqlCmd @params
249+
}
250+
```
251+
252+
194253
### <a id="create-the-job-credentials"></a> Use database-scoped credentials for authentication to targets
195254

196255
Job agents use credentials specified by the target group upon execution and execute scripts. These database-scoped credentials are also used to connect to the `master` database to discover all the databases in a server or an elastic pool, when either of these are used as the target group member type.

azure-sql/database/elastic-jobs-tsql-create-manage.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Learn how to create an elastic job agent and run scripts across man
44
author: WilliamDAssafMSFT
55
ms.author: wiassaf
66
ms.reviewer: srinia
7-
ms.date: 11/06/2023
7+
ms.date: 11/13/2023
88
ms.service: sql-database
99
ms.subservice: elastic-jobs
1010
ms.topic: how-to
@@ -235,6 +235,8 @@ SELECT * FROM jobs.target_group_members WHERE target_group_name = N'PoolGroup';
235235

236236
With T-SQL, create jobs using system stored procedures in the jobs database: [jobs.sp_add_job](/sql/relational-databases/system-stored-procedures/sp-add-job-elastic-jobs-transact-sql?view=azuresql-db&preserve-view=true) and [jobs.sp_add_jobstep](/sql/relational-databases/system-stored-procedures/sp-add-jobstep-elastic-jobs-transact-sql?view=azuresql-db&preserve-view=true). The T-SQL commands are syntax are similar to the steps needed to create SQL Agent jobs and job steps in SQL Server.
237237

238+
You should not update internal catalog views in the *job database*. Manually changing these catalog views can corrupt the *job database* and cause failure. These views are for read-only querying only. You can use the stored procedures in the `jobs` schema on your *job database*.
239+
238240
- When using Microsoft Entra authentication for a Microsoft Entra ID or user-assigned managed identity to authenticate to target server(s)/database(s), the *@credential_name* argument shouldn't be provided for `sp_add_jobstep` or `sp_update_jobstep`. Similarly, omit the optional *@output_credential_name* and *@refresh_credential_name* arguments.
239241
- When using database-scoped credentials to authenticate to target server(s)/database(s), the *@credential_name* parameter is required for `sp_add_jobstep` and `sp_update_jobstep`.
240242
- For example, `@credential_name = 'job_credential'`.

azure-sql/database/elastic-jobs-tutorial.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: "Learn how to create, configure, and manage elastic jobs to run Tra
44
author: WilliamDAssafMSFT
55
ms.author: wiassaf
66
ms.reviewer: srinia
7-
ms.date: 11/02/2023
7+
ms.date: 11/14/2023
88
ms.service: sql-database
99
ms.subservice: elastic-jobs
1010
ms.topic: how-to
@@ -21,7 +21,7 @@ This article provides the steps required to create, configure, and manage elasti
2121
2222
## Create and configure the elastic job agent
2323

24-
1. Create or identify an empty S1 or higher Azure SQL Database. This database should be in the same server as the job agent. This database is used as the *job database* during elastic job agent creation. You can [create a single database](single-database-create-quickstart.md?view=azuresql-db&preserve-view=true) via the Azure portal, Azure CLI, Azure CLI (sql up), or PowerShell.
24+
1. Create or identify an empty S1 or higher Azure SQL Database, using the DTU purchase model. This database should be in the same server as the job agent. This database is used as the *job database* during elastic job agent creation. You can [create a single database](single-database-create-quickstart.md?view=azuresql-db&preserve-view=true) via the Azure portal, Azure CLI, Azure CLI (sql up), or PowerShell.
2525
1. Create an **elastic job agent** in the Azure portal or with [PowerShell](elastic-jobs-powershell-create.md#create-the-elastic-job-agent).
2626

2727
The instructions to create an elastic job agent in the Azure portal are as follows:
@@ -62,9 +62,18 @@ Use [Microsoft Entra (formerly Azure Active Directory)](authentication-aad-overv
6262
- [A new UMI can be created](/azure/active-directory/managed-identities-azure-resources/how-manage-user-assigned-managed-identities) through the Azure portal, Azure CLI, PowerShell, Resource Manager, or REST API.
6363
1. Assign the UMI to the created elastic job agent.
6464
- It's recommended to assign a UMI when creating the elastic job agent, see the steps in [Create and configure the elastic job agent](#create-and-configure-the-elastic-job-agent). When creating a job agent in the Azure portal, in the **Identity** tab, assign to the elastic job agent.
65-
- Currently in the Elastic Jobs preview, you cannot use PowerShell cmdlets to create or modify elastic job agents to use Microsoft Entra authentication with a UMI.
6665
- To update an existing elastic job agent to use a UMI, on the Azure portal page for the **elastic job agent**, navigate to **Identity** under the **Security** menu in the resource menu. Select and assign the UMI to the elastic job agent.
67-
- The [REST API can also be used to create or update the elastic job agent](/rest/api/sql/2021-02-01-preview/job-agents/create-or-update?tabs=HTTP). <!-- TODO update /2021-02-01/ when available -->
66+
- When creating or updating an elastic job agent with the `New-AzSqlElasticJobAgent` or `Set-AzSqlElasticJobAgent` PowerShell cmdlets, use the parameters: `-IdentityType UserAssigned -IdentityID <identity resource path>`. For example:
67+
```powershell
68+
$parameters = @{
69+
Name = '<job agent name>'
70+
ResourceGroupName = '<Resource_Group_Name>'
71+
IdentityType = 'UserAssigned'
72+
IdentityID = '/subscriptions/fa58cf66-caaf-4ba9-875d-f1234/resourceGroups/<resource group name>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<UMI name here>'
73+
}
74+
Set-AzSqlElasticJobAgent @parameters
75+
```
76+
- The [REST API can also be used to create or update the elastic job agent](/rest/api/sql/job-agents/create-or-update).
6877
1. Create a target group and add targets for the jobs. [Define the target group and targets (the databases you want to run the job against) using PowerShell](elastic-jobs-powershell-create.md#define-target-servers-and-databases) or [define the target group and targets using T-SQL](elastic-jobs-tsql-create-manage.md#define-target-servers-and-databases).
6978
1. In each of the target server(s)/database(s), [create a contained user mapped to the UMI](#use-microsoft-entra-authentication-with-a-user-assigned-managed-identity-umi).
7079
1. In the output database, create and assign permissions to the UMI job user. Connect to the output database and run the following example script for a user named `jobuserUMI`:
@@ -166,7 +175,7 @@ To proceed with the Azure portal:
166175

167176
By default, job agents are created at JA100, allowing up to 100 elastic job executions concurrently. Initiating a service level change is an asynchronous operation and the new service level will be made available after a short provisioning delay.
168177

169-
If you need more than 100 concurrent executions of elastic job agents, higher service levels are available, see [Concurrent capacity tiers](elastic-jobs-overview.md#concurrent-capacity-tiers). You can currently change the service level of a job agent via the Azure portal or REST API.
178+
If you need more than 100 concurrent executions of elastic job agents, higher service levels are available, see [Concurrent capacity tiers](elastic-jobs-overview.md#concurrent-capacity-tiers). You can currently change the service level of a job agent via the Azure portal, PowerShell, or REST API.
170179

171180
Exceeding the service level with concurrent jobs will create queuing delays before jobs start in excess of the [service level's concurrent jobs limit](elastic-jobs-overview.md#concurrent-capacity-tiers).
172181
@@ -178,10 +187,21 @@ Exceeding the service level with concurrent jobs will create queuing delays befo
178187
1. Review the cost card.
179188
1. Select **Update**.
180189
190+
### Scale the elastic job agent by using PowerShell
191+
192+
The optional `-ServiceObjective` parameter for `Set-AzSqlElasticJobAgent` can be used to specify a new service objective. For example:
193+
```powershell
194+
$parameters = @{
195+
Name = '<job agent name>'
196+
ResourceGroupName = '<Resource_Group_Name>'
197+
ServiceObjective = 'JA200'
198+
}
199+
Set-AzSqlElasticJobAgent @parameters
200+
```
181201
182202
### Scale the elastic job agent by using REST API
183203
184-
You can use the [Job agent REST API](/rest/api/sql/2021-02-01-preview/job-agents#operations) <!-- update 2021-02-01-preview --> to scale a job agent. For example:
204+
You can use the [Job agent REST API](/rest/api/sql/job-agents) to scale a job agent. For example:
185205
186206
```json
187207
{

docs/relational-databases/system-catalog-views/jobs-job-executions-elastic-jobs-transact-sql.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "jobs.job_executions (Azure Elastic Jobs) (Transact-SQL)"
33
description: "The jobs.job_executions system view contains information about Azure Elastic job execution history."
44
author: WilliamDAssafMSFT
55
ms.author: wiassaf
6-
ms.date: 11/03/2023
6+
ms.date: 11/13/2023
77
ms.service: sql-database
88
ms.subservice: system-objects
99
ms.topic: "reference"
@@ -66,6 +66,9 @@ The following table lists the possible job execution states in `lifecycle`:
6666

6767
Members of the *jobs_reader* role can SELECT from this view. For more information, see [Elastic jobs in Azure SQL Database (preview)](/azure/azure-sql/database/elastic-jobs-overview?view=azuresql-db&preserve-view=true#elastic-job-database-permissions).
6868

69+
> [!CAUTION]
70+
> You should not update internal catalog views in the *job database*. Manually changing these catalog views can corrupt the *job database* and cause failure. These views are for read-only querying only. You can use the stored procedures on your *job database*.
71+
6972
## Remarks
7073

7174
All times in elastic jobs are in the UTC time zone.

docs/relational-databases/system-catalog-views/jobs-job-versions-elastic-jobs-transact-sql.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "jobs.job_versions (Azure Elastic Jobs) (Transact-SQL)"
33
description: "The jobs.job_versions system view contains job version history in Azure Elastic jobs."
44
author: WilliamDAssafMSFT
55
ms.author: wiassaf
6-
ms.date: 10/30/2023
6+
ms.date: 11/13/2023
77
ms.service: sql-database
88
ms.subservice: system-objects
99
ms.topic: "reference"
@@ -33,6 +33,9 @@ Contains version history about jobs in the [Azure Elastic Jobs service for Azure
3333

3434
Members of the *jobs_reader* role can SELECT from this view. For more information, see [Elastic jobs in Azure SQL Database (preview)](/azure/azure-sql/database/elastic-jobs-overview?view=azuresql-db&preserve-view=true#elastic-job-database-permissions).
3535

36+
> [!CAUTION]
37+
> You should not update internal catalog views in the *job database*. Manually changing these catalog views can corrupt the *job database* and cause failure. These views are for read-only querying only. You can use the stored procedures on your *job database*.
38+
3639
## Remarks
3740

3841
All times in elastic jobs are in the UTC time zone.

0 commit comments

Comments
 (0)