Een gebruiker toevoegen
Met dit voorbeeldscript maakt u een gebruiker in API Management en een haalt u een abonnementssleutel op.
Notitie
U wordt aangeraden de Azure Az PowerShell-module te gebruiken om te communiceren met Azure. Zie Azure PowerShell installeren om aan de slag te gaan. Raadpleeg Azure PowerShell migreren van AzureRM naar Az om te leren hoe u naar de Azure PowerShell-module migreert.
Azure Cloud Shell
Azure host Azure Cloud Shell, een interactieve shell-omgeving die u via uw browser kunt gebruiken. U kunt Bash of PowerShell gebruiken met Cloud Shell om met Azure-services te werken. 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 Uitproberen in de rechterbovenhoek van een code- of opdrachtblok. Als u Proberen selecteert, wordt de code of opdracht 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. | ![]() |
Azure Cloud Shell gebruiken:
Start Cloud Shell.
Selecteer de knop Kopiëren in een codeblok (of opdrachtblok) om de code of opdracht te kopiëren.
Plak de code of opdracht in de Cloud Shell-sessie door Ctrl+Shift+V te selecteren in Windows en Linux of door Cmd+Shift+V in macOS te selecteren.
Selecteer Enter om de code of opdracht uit te voeren.
Als u PowerShell lokaal wilt installeren en gebruiken, wordt voor deze zelfstudie moduleversie 1.0 of hoger van Azure PowerShell vereist. Voer Get-Module -ListAvailable Az
uit om de versie te bekijken. Als u PowerShell wilt upgraden, raadpleegt u De Azure PowerShell-module installeren. Als u PowerShell lokaal uitvoert, moet u ook Connect-AzAccount
uitvoeren om verbinding te kunnen maken met Azure.
Voorbeeldscript
##########################################################
# Script to create a user in api management and get a subscription key
###########################################################
$random = (New-Guid).ToString().Substring(0,8)
#Azure specific details
$subscriptionId = "my-azure-subscription-id"
# Api Management service specific details
$apimServiceName = "apim-$random"
$resourceGroupName = "apim-rg-$random"
$location = "Japan East"
$organisation = "Contoso"
$adminEmail = "admin@contoso.com"
# User specific details
$userEmail = "user@contoso.com"
$userFirstName = "userFirstName"
$userLastName = "userLastName"
$userPassword = "userPassword"
$userNote = "fellow trying out my apim instance"
$userState = "Active"
# Subscription Name details
$subscriptionName = "subscriptionName"
$subscriptionState = "Active"
# Set the context to the subscription Id where the cluster will be created
Select-AzSubscription -SubscriptionId $subscriptionId
# Create a resource group.
New-AzResourceGroup -Name $resourceGroupName -Location $location
# Create the Api Management service. Since the SKU is not specified, it creates a service with Developer SKU.
New-AzApiManagement -ResourceGroupName $resourceGroupName -Name $apimServiceName -Location $location -Organization $organisation -AdminEmail $adminEmail
# Create the api management context
$context = New-AzApiManagementContext -ResourceGroupName $resourceGroupName -ServiceName $apimServiceName
# create a new user in api management
$user = New-AzApiManagementUser -Context $context -FirstName $userFirstName -LastName $userLastName `
-Password $userPassword -State $userState -Note $userNote -Email $userEmail
# get the details of the 'Starter' product in api management, which is created by default
$product = Get-AzApiManagementProduct -Context $context -Title 'Starter' | Select-Object -First 1
# generate a subscription key for the user to call apis which are part of the 'Starter' product
New-AzApiManagementSubscription -Context $context -UserId $user.UserId `
-ProductId $product.ProductId -Name $subscriptionName -State $subscriptionState
Resources opschonen
U kunt de opdracht Remove-AzResourceGroup gebruiken om de resourcegroep en alle gerelateerde resources te verwijderen wanneer u ze niet meer nodig hebt.
Remove-AzResourceGroup -Name myResourceGroup
Volgende stappen
Zie voor meer informatie over de Azure PowerShell-module de documentatie van Azure PowerShell.
Meer voorbeelden voor Azure PowerShell voor Azure API Management vindt u in Voorbeelden van PowerShell.