Because I am not allowed to enable Microsoft Defender for SQL from Azure Portal, I am tring to find a Powershell or AzureCLI command to enable it from CloudShell. The final step is to use that command in a pipeline. I am expecting to be a command that get as parameters the name of the server and the group and automatically enable the Microsoft Defender for SQL.
I tried this:
az sql server threat-policy update --resource-group <my-resource-group> --server-name <my-sql-server-name> --state Enabled
The answer was:
'threat-policy' is misspelled or not recognized by the system.
EDIT After more investigation I have found a partial solution. I will explain bellow:
Update-AzSqlServerAdvancedThreatProtectionSetting -Enable $true -ResourceGroupName 'my-resource-group' -ServerName 'my-server-name'
the status was changed to "Enabled status: Enabled at the subscription-level" and bellow of the status an warning is shown with an "enable" button saying that "SQL Vulnerability Assessment is not configured. Click to enable express configuration".
I finally found a solution. I will describe it below:
Update-AzSqlServerAdvancedThreatProtectionSetting -Enable $true -ResourceGroupName '<your-resource-group-name>' -ServerName '<your-sql-server-name>'
$SubscriptionId = '<your-sub-id>'
$ResourceGroupName = '<your-resoruce-group-name>'
$ServerName = '<your-server-name>'
param
(
[Parameter(Mandatory = $True)]
[string]$SubscriptionId,
[Parameter(Mandatory = $True)]
[string]$ResourceGroupName,
[Parameter(Mandatory = $True)]
[string]$ServerName,
[Parameter(Mandatory = $False)]
[switch]$Force
)
function SetSqlVulnerabilityAssessmentServerSetting($SubscriptionId, $ResourceGroupName, $ServerName) {
$Uri = "/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Sql/servers/$ServerName/sqlVulnerabilityAssessments/default?api-version=2022-02-01-preview"
$Body = @{
properties = @{
state = "Enabled"
}
}
$Body = $Body | ConvertTo-Json
return SendRestRequest -Method "Put" -Uri $Uri -Body $Body
}
function SendRestRequest(
[Parameter(Mandatory = $True)]
[string] $Method,
[Parameter(Mandatory = $True)]
[string] $Uri,
[parameter( Mandatory = $false )]
[string] $Body = "DEFAULT") {
$Params = @{
Method = $Method
Path = $Uri
}
if (!($Body -eq "DEFAULT")) {
$Params = @{
Method = $Method
Path = $Uri
Payload = $Body
}
}
Invoke-AzRestMethod @Params
}
SetSqlVulnerabilityAssessmentServerSetting -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -ServerName $ServerName