Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
              Van toepassing op:Azure SQL Database
In dit PowerShell-voorbeeldscript worden de metrische prestatiegegevens van één database bewaakt, geschaald naar een hogere rekenkracht en wordt een waarschuwingsregel gemaakt voor een van de metrische prestatiegegevens.
Als u geen Azure-abonnement hebt, kunt u een gratis Azure-account maken voordat u begint.
Opmerking
In dit artikel wordt de Azure Az PowerShell-module gebruikt. Dit is de aanbevolen PowerShell-module voor interactie met Azure. Zie Azure PowerShell-installeren om aan de slag te gaan met de Az PowerShell-module. Om te leren hoe u naar de Az PowerShell-module kunt migreren, zie Migrate Azure PowerShell from AzureRM to Az.
Azure Cloud Shell gebruiken
Azure host Azure Cloud Shell, een interactieve shell-omgeving die u via uw browser kunt gebruiken. U kunt Bash of PowerShell met Cloud Shell gebruiken om te werken met Azure-services. U kunt de vooraf geïnstalleerde Cloud Shell-opdrachten gebruiken om de code in dit artikel uit te voeren zonder dat u iets hoeft te installeren in uw lokale omgeving.
Om Azure Cloud Shell op te starten:
| Optie | Voorbeeld/koppeling | 
|---|---|
| Selecteer Nu proberen in de rechterbovenhoek van een codeblok. Als u Try It selecteert, wordt de code niet automatisch gekopieerd naar Cloud Shell. | 
               
              
             | 
| Ga naar https://shell.azure.com, of selecteer de knop Cloud Shell starten om Cloud Shell in uw browser te openen. | 
              
               
              
              
             | 
| Klik op de knop Cloud Shell in het menu in de balk rechtsboven in de Azure-portal. | 
               
              
             | 
Om de code in dit artikel in Azure Cloud Shell uit te voeren:
Start Cloud Shell.
Selecteer de knop Kopiëren op een codeblok om de code te kopiëren.
Plak de code in de Cloud Shell-sessie door Ctrl+Shift+V- in Windows en Linux te selecteren of door Cmd+Shift+V- in macOS te selecteren.
Selecteer Voer in om de code uit te voeren.
Als u PowerShell lokaal wilt installeren en gebruiken, is voor deze zelfstudie Az PowerShell 1.4.0 of hoger vereist. Zie Azure PowerShell-module installerenals u een upgrade wilt uitvoeren. Als u PowerShell lokaal uitvoert, moet u ook Connect-AzAccount uitvoeren om een verbinding met Azure te maken.
Voorbeeldscript
# This script requires the following
# - Az.Resources
# - Az.Accounts
# - Az.Monitor
# - Az.Sql
# First, run Connect-AzAccount
# Set the subscription in which to create these objects. This is displayed on objects in the Azure portal.
$SubscriptionId = ''
# Set the resource group name and location for your server
$resourceGroupName = "myResourceGroup-$(Get-Random)"
$location = "westus2"
# Set an admin login and password for your server
$adminSqlLogin = "SqlAdmin"
$password = (New-Guid).Guid # Generates a randomized GUID password. 
# Set server name - the logical server name has to be unique in the system
$serverName = "server-$(Get-Random)"
# The sample database name
$databaseName = "mySampleDatabase"
# The ip address range that you want to allow to access your server via the firewall rule
$startIp = "0.0.0.0"
$endIp = "0.0.0.0"
# Set subscription 
Set-AzContext -SubscriptionId $subscriptionId 
# Create a new resource group
$resourceGroup = New-AzResourceGroup -Name $resourceGroupName -Location $location
# Create a new server with a system wide unique server name
$server = New-AzSqlServer -ResourceGroupName $resourceGroupName `
    -ServerName $serverName `
    -Location $location `
    -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminSqlLogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))
# Create a server firewall rule that allows access from the specified IP range
$serverFirewallRule = New-AzSqlServerFirewallRule -ResourceGroupName $resourceGroupName `
    -ServerName $serverName `
    -FirewallRuleName "AllowedIPs" -StartIpAddress $startIp -EndIpAddress $endIp
# Create a blank database with an S0 performance level
$database = New-AzSqlDatabase  -ResourceGroupName $resourceGroupName `
    -ServerName $serverName `
    -DatabaseName $databaseName `
    -RequestedServiceObjectiveName "S0" `
    -SampleName "AdventureWorksLT"
# Monitor the DTU consumption on the database in 5 minute intervals
$MonitorParameters = @{
  ResourceId = "/subscriptions/$($(Get-AzContext).Subscription.Id)/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/servers/$serverName/databases/$databaseName"
  TimeGrain = [TimeSpan]::Parse("00:05:00")
  MetricNames = "dtu_consumption_percent"
}
$metric = Get-AzMetric @monitorparameters
$metric.Data
# Scale the database performance to Standard S1
$database = Set-AzSqlDatabase -ResourceGroupName $resourceGroupName `
    -ServerName $servername `
    -DatabaseName $databasename `
    -Edition "Standard" `
    -RequestedServiceObjectiveName "S1"
# Set up an Alert rule using Azure Monitor for the database
# Add an Alert that fires when the pool utilization reaches 90%
# Objects needed: An Action Group Receiver (in this case, an email group), an Action Group, Alert Criteria, and finally an Alert Rule.
# Creates an new action group receiver object with a target email address.
$receiver = New-AzActionGroupReceiver `
    -Name "my Sample Azure Admins" `
    -EmailAddress "azure-admins-group@contoso.com"
# Creates a new or updates an existing action group.
$actionGroup = Set-AzActionGroup `
    -Name "mysample-email-the-azure-admins" `
    -ShortName "AzAdminsGrp" `
    -ResourceGroupName $resourceGroupName `
    -Receiver $receiver
# Fetch the created AzActionGroup into an object of type Microsoft.Azure.Management.Monitor.Models.ActivityLogAlertActionGroup
$actionGroupObject = New-AzActionGroup -ActionGroupId $actionGroup.Id
# Create a criteria for the Alert to monitor.
$criteria = New-AzMetricAlertRuleV2Criteria `
    -MetricName "dtu_consumption_percent" `
    -TimeAggregation Average `
    -Operator GreaterThan `
    -Threshold 90
# Create the Alert rule.
# Add-AzMetricAlertRuleV2 adds or updates a V2 (non-classic) metric-based alert rule.
Add-AzMetricAlertRuleV2 -Name "mySample_Alert_DTU_consumption_pct" `
        -ResourceGroupName $resourceGroupName `
        -WindowSize (New-TimeSpan -Minutes 1) `
        -Frequency (New-TimeSpan -Minutes 1) `
        -TargetResourceId "/subscriptions/$($(Get-AzContext).Subscription.Id)/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/servers/$serverName/databases/$databaseName" `
        -Condition $criteria `
        -ActionGroup $actionGroupObject `
        -Severity 3 #Informational
<#
# Set up an alert rule using Azure Monitor for the database
# Note that Add-AzMetricAlertRule is deprecated. Use Add-AzMetricAlertRuleV2 instead.
Add-AzMetricAlertRule -ResourceGroup $resourceGroupName `
    -Name "MySampleAlertRule" `
    -Location $location `
    -TargetResourceId "/subscriptions/$($(Get-AzContext).Subscription.Id)/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/servers/$serverName/databases/$databaseName" `
    -MetricName "dtu_consumption_percent" `
    -Operator "GreaterThan" `
    -Threshold 90 `
    -WindowSize $([TimeSpan]::Parse("00:05:00")) `
    -TimeAggregationOperator "Average" `
    -Action $(New-AzAlertRuleEmail -SendToServiceOwner)
#>
# Clean up deployment 
# Remove-AzResourceGroup -ResourceGroupName $resourceGroupName
Opmerking
Zie ondersteunde metrische gegevens voor een volledige lijst met metrische gegevens.
Aanbeveling
Gebruik Get-AzSqlDatabaseActivity om de status van databasebewerkingen op te halen en Stop-AzSqlDatabaseActivity te gebruiken om een database-updatebewerking te annuleren.
Opschoning van de implementatie
Gebruik de volgende opdracht om de resourcegroep en alle bijbehorende resources te verwijderen.
Remove-AzResourceGroup -ResourceGroupName $resourcegroupname
Uitleg van script
Dit script gebruikt de volgende commando's. Elke opdracht in de tabel linkt naar documentatie die specifiek is voor die opdracht.
| Opdracht | Opmerkingen | 
|---|---|
| New-AzResourceGroup | Hiermee maakt u een resourcegroep waarin alle resources worden opgeslagen. | 
| New-AzSqlServer | Hiermee maakt u een server die als host fungeert voor één database of elastische pool. | 
| Get-AzMetric | Geeft de grootte van de gebruiksgegevens voor de database weer. | 
| Set-AzSqlDatabase | Hiermee worden database-eigenschappen bijgewerkt of wordt de database verplaatst naar, uit of tussen elastische pools. | 
| Add-AzMetricAlertRule | (Afgeschaft) Hiermee voegt u een waarschuwingsregel toe of werkt u deze bij om in de toekomst automatisch metrische gegevens te bewaken. Alleen van toepassing op klassieke waarschuwingsregels op basis van metrische gegevens. | 
| Add-AzMetricAlertRuleV2 | Hiermee voegt u een waarschuwingsregel toe of werkt u deze bij om in de toekomst automatisch metrische gegevens te bewaken. Alleen van toepassing op niet-klassieke waarschuwingsregels op basis van metrische gegevens. | 
| Remove-AzResourceGroup | Verwijdert een resourcegroep, inclusief alle onderliggende resources. |