你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
重要
Azure 实验室服务将于 2027 年 6 月 28 日停用。 有关详细信息,请参阅停用指南。 为了简化迁移,Microsoft已发布自动化脚本来帮助清理实验室服务资源,这些脚本在 Azure 实验室服务停用脚本 GitHub 存储库中提供。
本文介绍如何使用 Python 和 Azure Python 库 (SDK) 创建实验室。 此实验室将使用以前创建的实验室计划中的设置。 有关 Azure 实验室服务的详细说明,请参阅 Azure 实验室服务简介。
先决条件
- 具有活动订阅的 Azure 帐户。 如果没有 Azure 订阅,请在开始之前创建一个免费帐户。
- 有权管理实验室的 Azure 帐户,例如实验室创建者、所有者、参与者或实验室服务参与者等 Azure RBAC 角色。 详细了解 Azure 实验室服务内置角色和工作范围。
- Azure 实验室计划。 如果还没有实验室计划,请按照快速入门:设置资源以创建实验室中的步骤操作。
创建实验室
在可以创建实验室之前,需要实验室计划对象。 在使用 Python 创建实验室计划中,了解如何在名为 BellowsCollege_labplan 的资源组中创建名为 BellowsCollege_rg 的实验室计划。
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
from datetime import timedelta
import time
from azure.identity import DefaultAzureCredential
from azure.mgmt.labservices import LabServicesClient
from azure.mgmt.resource import ResourceManagementClient
def main():
SUBSCRIPTION_ID = "<Subscription ID>"
TIME = str(time.time()).replace('.','')
GROUP_NAME = "BellowsCollege_rg"
LABPLAN = "BellowsCollege_labplan"
LAB = "BellowsCollege_lab"
LOCATION = 'southcentralus'
# Create clients
# # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
resource_client = ResourceManagementClient(
credential=DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID
)
labservices_client = LabServicesClient(
credential=DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID
)
#Get single LabServices Lab Plan
labservices_labplan = labservices_client.lab_plans.get(GROUP_NAME, LABPLAN)
print("Get lab plans")
print(labservices_labplan)
#Get image information
LABIMAGES = labservices_client.images.list_by_lab_plan(GROUP_NAME,LABPLAN)
image = (list(filter(lambda x: (x.name == "microsoftwindowsdesktop.windows-11.win11-21h2-pro"), LABIMAGES)))
#Get lab quota
USAGEQUOTA = timedelta(hours=10)
# Password
CUSTOMPASSWORD = "<custom password>"
# Create LabServices Lab
LABBODY = {
"name": LAB,
"location" : LOCATION,
"properties" : {
"networkProfile": {},
"connectionProfile" : {
"webSshAccess" : "None",
"webRdpAccess" : "None",
"clientSshAccess" : "None",
"clientRdpAccess" : "Public"
},
"AutoShutdownProfile" : {
"shutdownOnDisconnect" : "Disabled",
"shutdownWhenNotConnected" : "Disabled",
"shutdownOnIdle" : "None"
},
"virtualMachineProfile" : {
"createOption" : "TemplateVM",
"imageReference" : {
"offer": image[0].offer,
"publisher": image[0].publisher,
"sku": image[0].sku,
"version": image[0].version
},
"sku" : {
"name" : "Classic_Fsv2_2_4GB_128_S_SSD",
"capacity" : 2
},
"additionalCapabilities" : {
"installGpuDrivers" : "Disabled"
},
"usageQuota" : USAGEQUOTA,
"UseSharedPassword" : "Enabled",
"adminUser" : {
"username" : "testuser",
"password" : CUSTOMPASSWORD
}
},
"securityProfile" : {
"openAccess" : "Disabled"
},
"rosterProfile" : {},
"labPlanId" : labservices_labplan.id,
"title" : "lab-python",
"description" : "lab 99 description updated"
}
}
poller = labservices_client.labs.begin_create_or_update(
GROUP_NAME,
LAB,
LABBODY
)
lab_result = poller.result()
print(f"Created Lab {lab_result.name}")
# Get LabServices Labs
labservices_lab = labservices_client.labs.get(GROUP_NAME,LAB)
print("Get lab:\n{}".format(labservices_lab))
if __name__ == "__main__":
main()
清理资源
如果你不打算继续使用此应用程序,请按以下步骤删除组和实验室:
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
from datetime import timedelta
import time
from azure.identity import DefaultAzureCredential
from azure.mgmt.labservices import LabServicesClient
from azure.mgmt.resource import ResourceManagementClient
# - other dependence -
# - end -
#
def main():
SUBSCRIPTION_ID = "<Subscription ID>"
TIME = str(time.time()).replace('.','')
GROUP_NAME = "BellowsCollege_rg"
LABPLAN = "BellowsCollege_labplan"
LAB = "BellowsCollege_lab"
LOCATION = 'southcentralus'
# Create clients
# # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
resource_client = ResourceManagementClient(
credential=DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID
)
labservices_client = LabServicesClient(
credential=DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID
)
# Delete Lab
labservices_client.labs.begin_delete(
GROUP_NAME,
LAB
).result()
print("Deleted lab.\n")
# Delete Group
resource_client.resource_groups.begin_delete(
GROUP_NAME
).result()
if __name__ == "__main__":
main()
后续步骤
作为管理员,你可以详细了解 Azure PowerShell 模块和 Az.LabServices cmdlet。