Files
Cloud-20Engineering/Powershell/RunBooks/SQLMaintenanceTemplate.ps1
2025-10-08 08:31:36 +02:00

25 lines
803 B
PowerShell
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
$AzureSQLServerName = "xxxx"
$AzureSQLDatabaseName = "yyyy"
$SQLOutput = ""
try {
$AzureSQLServerName = $AzureSQLServerName + ".database.windows.net"
Disable-AzContextAutosave -Scope Process
$AzureContext = (Connect-AzAccount -Identity).context
$token = (Get-AzAccessToken -ResourceUrl https://database.windows.net).Token
$SQLOutput = $(Invoke-Sqlcmd -ServerInstance $AzureSQLServerName `
-Database $AzureSQLDatabaseName `
-AccessToken "$token" `
-Query "exec [dbo].[AzureSQLMaintenance] @Operation='all' ,@LogToTable=1" -QueryTimeout 65535 -ConnectionTimeout 60 -Verbose) 4>&1
}
catch
{
Write-Host $_
throw "Error occured!"
}
finally {
Write-Output $SQLOutput
}