你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
通过 Azure REST API 开始使用 Azure 开发测试实验室。 Azure 开发测试实验室包含一组资源,例如 Azure 虚拟机 (VM) 和网络。 通过该基础结构可指定限制和配额,从而更好地管理这些资源。 通过 Azure REST API 可管理对 Azure 平台中托管的服务的操作。
先决条件
准备请求正文
准备要由 REST 调用使用的 请求正文。
将以下 JSON 语法复制粘贴到名为 body.json
的文件中。 将该文件保存在本地计算机或 Azure 存储帐户中。
{
"properties": {
"labStorageType": "Standard"
},
"___location": "westus2",
"tags": {
"Env": "alpha"
}
}
登录到 Azure 订阅
为变量提供适当的值,然后执行脚本。
$subscription = "subscriptionID" $resourceGroup = "resourceGroupName" $labName = "labName" $file = "path\body.json"
在工作站中,使用 PowerShell Connect-AzAccount cmdlet 登录 Azure 订阅,然后按屏幕说明操作。
# Sign in to your Azure subscription $sub = Get-AzSubscription -ErrorAction SilentlyContinue if(-not($sub)) { Connect-AzAccount } # If you have multiple subscriptions, set the one to use # Set-AzContext -SubscriptionId $subscription
生成用于提交的请求正文
PUT 请求的语法为:
https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{name}?api-version=2018-09-15
.
执行以下 PowerShell 脚本,将请求值传递到参数。 请求正文的内容也会传递给参数。
# build URI
$URI = "https://management.azure.com/subscriptions/$subscription/resourceGroups/$resourceGroup/providers/Microsoft.DevTestLab/labs/$labName`?api-version=2018-09-15"
# build body
$body = Get-Content $file
获取身份验证令牌
使用以下命令检索身份验证令牌:
$azContext = Get-AzContext
$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile)
$token = $profileClient.AcquireAccessToken($azContext.Subscription.TenantId)
$authHeader = @{
'Content-Type'='application/json'
'Authorization'='Bearer ' + $token.AccessToken
}
调用 REST API
使用以下命令调用 REST API 并查看响应。
# Invoke the REST API
$response = Invoke-RestMethod -Uri $URI -Method PUT -Headers $authHeader -Body $body
# Review output
$response | ConvertTo-Json
响应应类似于以下文本:
{
"properties": {
"labStorageType": "Standard",
"mandatoryArtifactsResourceIdsLinux": [
],
"mandatoryArtifactsResourceIdsWindows": [
],
"createdDate": "2021-10-27T20:22:49.7495913+00:00",
"premiumDataDisks": "Disabled",
"environmentPermission": "Reader",
"announcement": {
"title": "",
"markdown": "",
"enabled": "Disabled",
"expired": false
},
"support": {
"enabled": "Disabled",
"markdown": ""
},
"provisioningState": "Creating",
"uniqueIdentifier": "uniqueID"
},
"id": "/subscriptions/ContosoID/resourcegroups/groupcontoso/providers/microsoft.devtestlab/labs/myotherlab",
"name": "myOtherLab",
"type": "Microsoft.DevTestLab/labs",
"___location": "westus2",
"tags": {
"Env": "alpha"
}
}
清理资源
如果不打算继续使用此实验室,请将其删除,步骤如下:
为变量提供适当的值,然后执行脚本。
$subscription = "subscriptionID" $resourceGroup = "resourceGroupName" $labName = "labName"
执行以下脚本,从 Azure 开发测试实验室删除命名的实验室。
# build URI $URI = "https://management.azure.com/subscriptions/$subscription/resourceGroups/$resourceGroup/providers/Microsoft.DevTestLab/labs/$labName`?api-version=2018-09-15" # obtain access token $azContext = Get-AzContext $azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile $profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile) $token = $profileClient.AcquireAccessToken($azContext.Subscription.TenantId) $authHeader = @{ 'Content-Type'='application/json' 'Authorization'='Bearer ' + $token.AccessToken } # Invoke the REST API Invoke-RestMethod -Uri $URI -Method DELETE -Headers $authHeader
后续步骤
在本快速入门中,你使用 Azure REST API 创建了一个实验室。 若要了解如何访问实验室,请继续学习下一教程: