1
- # Connect-AzAccount
2
- # The SubscriptionId in which to create these objects
1
+ # This script requires the following
2
+ # - Az.Resources
3
+ # - Az.Accounts
4
+ # - Az.Monitor
5
+ # - Az.Sql
6
+
7
+ # First, run Connect-AzAccount
8
+
9
+ # Set the subscription in which to create these objects. This is displayed on objects in the Azure portal.
3
10
$SubscriptionId = ' '
4
11
# Set the resource group name and location for your server
5
12
$resourceGroupName = " myResourceGroup-$ ( Get-Random ) "
6
13
$location = " westus2"
7
14
# Set an admin login and password for your server
8
15
$adminSqlLogin = " SqlAdmin"
9
- $password = " ChangeYourAdminPassword1 "
16
+ $password = ( New-Guid ).Guid # Generates a randomized GUID password.
10
17
# Set server name - the logical server name has to be unique in the system
11
18
$serverName = " server-$ ( Get-Random ) "
12
19
# The sample database name
13
20
$databaseName = " mySampleDatabase"
14
- # The ip address range that you want to allow to access your server
21
+ # The ip address range that you want to allow to access your server via the firewall rule
15
22
$startIp = " 0.0.0.0"
16
23
$endIp = " 0.0.0.0"
17
24
18
25
# Set subscription
19
26
Set-AzContext - SubscriptionId $subscriptionId
20
27
21
- # Create a resource group
28
+ # Create a new resource group
22
29
$resourceGroup = New-AzResourceGroup - Name $resourceGroupName - Location $location
23
30
24
- # Create a server with a system wide unique server name
31
+ # Create a new server with a system wide unique server name
25
32
$server = New-AzSqlServer - ResourceGroupName $resourceGroupName `
26
33
- ServerName $serverName `
27
34
- Location $location `
@@ -39,13 +46,14 @@ $database = New-AzSqlDatabase -ResourceGroupName $resourceGroupName `
39
46
- RequestedServiceObjectiveName " S0" `
40
47
- SampleName " AdventureWorksLT"
41
48
42
- # Monitor the DTU consumption on the imported database in 5 minute intervals
49
+ # Monitor the DTU consumption on the database in 5 minute intervals
43
50
$MonitorParameters = @ {
44
51
ResourceId = " /subscriptions/$ ( $ (Get-AzContext ).Subscription.Id) /resourceGroups/$resourceGroupName /providers/Microsoft.Sql/servers/$serverName /databases/$databaseName "
45
52
TimeGrain = [TimeSpan ]::Parse(" 00:05:00" )
46
53
MetricNames = " dtu_consumption_percent"
47
54
}
48
- (Get-AzMetric @MonitorParameters - DetailedOutput).MetricValues
55
+ $metric = Get-AzMetric @monitorparameters
56
+ $metric.Data
49
57
50
58
# Scale the database performance to Standard S1
51
59
$database = Set-AzSqlDatabase - ResourceGroupName $resourceGroupName `
@@ -54,7 +62,47 @@ $database = Set-AzSqlDatabase -ResourceGroupName $resourceGroupName `
54
62
- Edition " Standard" `
55
63
- RequestedServiceObjectiveName " S1"
56
64
57
- # Set an alert rule to automatically monitor DTU in the future
65
+
66
+ # Set up an Alert rule using Azure Monitor for the database
67
+ # Add an Alert that fires when the pool utilization reaches 90%
68
+ # Objects needed: An Action Group Receiver (in this case, an email group), an Action Group, Alert Criteria, and finally an Alert Rule.
69
+
70
+ # Creates an new action group receiver object with a target email address.
71
+ $receiver = New-AzActionGroupReceiver `
72
+ - Name " my Sample Azure Admins" `
73
+ - EmailAddress " azure-admins-group@contoso.com"
74
+
75
+ # Creates a new or updates an existing action group.
76
+ $actionGroup = Set-AzActionGroup `
77
+ - Name " mysample-email-the-azure-admins" `
78
+ - ShortName " AzAdminsGrp" `
79
+ - ResourceGroupName $resourceGroupName `
80
+ - Receiver $receiver
81
+
82
+ # Fetch the created AzActionGroup into an object of type Microsoft.Azure.Management.Monitor.Models.ActivityLogAlertActionGroup
83
+ $actionGroupObject = New-AzActionGroup - ActionGroupId $actionGroup.Id
84
+
85
+ # Create a criteria for the Alert to monitor.
86
+ $criteria = New-AzMetricAlertRuleV2Criteria `
87
+ - MetricName " dtu_consumption_percent" `
88
+ - TimeAggregation Average `
89
+ - Operator GreaterThan `
90
+ - Threshold 90
91
+
92
+ # Create the Alert rule.
93
+ # Add-AzMetricAlertRuleV2 adds or updates a V2 (non-classic) metric-based alert rule.
94
+ Add-AzMetricAlertRuleV2 - Name " mySample_Alert_DTU_consumption_pct" `
95
+ - ResourceGroupName $resourceGroupName `
96
+ - WindowSize (New-TimeSpan - Minutes 1 ) `
97
+ - Frequency (New-TimeSpan - Minutes 1 ) `
98
+ - TargetResourceId " /subscriptions/$ ( $ (Get-AzContext ).Subscription.Id) /resourceGroups/$resourceGroupName /providers/Microsoft.Sql/servers/$serverName /databases/$databaseName " `
99
+ - Condition $criteria `
100
+ - ActionGroup $actionGroupObject `
101
+ - Severity 3 # Informational
102
+
103
+ <#
104
+ # Set up an alert rule using Azure Monitor for the database
105
+ # Note that Add-AzMetricAlertRule is deprecated. Use Add-AzMetricAlertRuleV2 instead.
58
106
Add-AzMetricAlertRule -ResourceGroup $resourceGroupName `
59
107
-Name "MySampleAlertRule" `
60
108
-Location $location `
@@ -65,6 +113,7 @@ Add-AzMetricAlertRule -ResourceGroup $resourceGroupName `
65
113
-WindowSize $([TimeSpan]::Parse("00:05:00")) `
66
114
-TimeAggregationOperator "Average" `
67
115
-Action $(New-AzAlertRuleEmail -SendToServiceOwner)
116
+ #>
68
117
69
118
# Clean up deployment
70
119
# Remove-AzResourceGroup -ResourceGroupName $resourceGroupName
0 commit comments