Menu

Azure Automation: how to optimize your database through Windows PowerShell

Azure Automation: how to optimize your database through Windows PowerShell

Azure Automation allows you to automate frequent, time-consuming, and error-prone cloud administration tasks. The Azure Automation service helps you focus more on work that adds real business value. Using Windows PowerShell scripts and workflows, known as Runbooks, we can work smarter, controlling the creation, deployment, monitoring, and maintenance of Azure resources and third-party applications.

In this article, I will focus on explaining the actions and tasks performed by the script below. For instructions on configuring it in Azure Automation, see our colleague David Rodriguez Hernández’s blog, which provides a detailed implementation guide.

 

PowerShell Workflows for Advanced Automation

Automating database optimization (DB) with Windows PowerShell - Azure Automation

We use PowerShell Workflows to execute sequences of tasks, allowing administrators and developers to leverage automation features and the full flexibility of Windows PowerShell.

In a standard script, the entire code runs in the same execution environment, which defines available commands, variables, and other elements. In a workflow, each activity can execute in a different environment. Variables created at the top level are available throughout the workflow, but not outside it.

This script allows us to index database tables based on fragmentation percentage, adjust the database tier for faster and smoother performance, and send a log upon task completion.

 

Key script actions in four sections

  • Upgrade database performance tier – Temporarily raise the database tier to ensure fast and efficient indexing.

  • Check fragmentation and index tables – Assess table fragmentation and perform indexing.

  • Restore database performance tier – Reset the database tier to the level specified in the input parameters.

  • Send email notifications – Optionally send a notification with errors and current database performance status.

Note: You must create database and SMTP server credentials in your Azure subscription before executing the script.

 

Script input parameters

  • SqlServerName – SQL server name; the script appends the full path automatically.

  • DatabaseName – Name of the database to index.

  • SQLCredentialName – Identifier of pre-created database access credentials.

  • Edition – Azure SQL Database edition for indexing (Basic, Standard, Premium).

  • PerfLevel – Azure SQL Database performance level for indexing (Basic, S0, S1, S2, P1, P2, P3).

  • FinalEdition – Azure SQL Database edition to restore after indexing (Basic, Standard, Premium).

  • FinalPerfLevel – Performance level to restore after indexing (Basic, S0, S1, S2, P1, P2, P3).

  • FragPercentage – Optional. Fragmentation percentage threshold for indexing (default 10%).

  • SqlServerPort – Optional. SQL Server port (default 1433).

  • RebuiltOffline – Optional. Retry offline if online indexing fails (default false).

  • Table – Optional. Index a specific table (default indexes all tables).

  • SMTPServer – Optional, required for notifications. SMTP server address for sending emails.

  • SMTPCredentials – Optional, required for notifications. SMTP credential identifier.

  • FromMail – Optional, required for notifications. Sender email address.

  • ToMail – Optional, required for notifications. Recipient email address.

 

Changing the database tier

This section of the script resizes the SQL server. We focus on two commands: Set-AzureSqlDatabase to modify SQL configuration, and Get-AzureSqlDatabase to retrieve current database properties.

A loop checks when the tier change is complete before continuing the execution.

Change the database tier (resizing the SQL server with Set-AzureSqlDatabase and Get-AzureSqlDatabase) ✅

 

Checking fragmentation and indexing

The script analyzes table fragmentation using a connector object created with server credentials.

Check database table fragmentation (code) – Azure Automation and Windows PowerShell

After the check, it indexes each table using the appropriate SQL commands.

Indexación de tablas con comando SQL

Sending notifications after the script ends

Optionally, the script can email a log upon task completion. Fill in the designated parameters for email notifications:

Send-MailMessage-To "$Using:ToMail" -Subject $subject -Body "$Using:Body" -UseSsl -Port 587 -SmtpServer "$Using:SMTPServer" -From "$Using:FromMail" -BodyAsHtml -Credential $Cred

You can download this script from the Intelequia GitHub repository for full access and implementation. 

Categories

Related posts
End of support for Windows 10: prepare for change in your organization
By Sergio Darias Pérez  |  15 September 2025

On October 14, 2025, Microsoft will end support for all versions of Windows 10: Home, Pro, and Enterprise. What do you need to know?

Read more
Microsoft Tenant: What it is and how to manage it securely
By Sergio Darias Pérez  |  26 August 2025

Discover what a Microsoft Tenant is, its benefits, types, and how to manage it to maximize security, control, and efficiency in your company.

Read more
Why implement virtual desktops in your company?
By Hugo Figueroa González  |  21 August 2025

Explore how virtual desktops improve security, efficiency, and management in corporate environments, adapting to the needs of each business.

Read more