Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Artikel ini memperlihatkan kepada Anda cara membuat komputer virtual (VM) Azure DevTest Labs di lab dengan menggunakan Azure PowerShell. Anda dapat menggunakan skrip PowerShell untuk mengotomatiskan pembuatan VM lab.
Prasyarat
Anda memerlukan prasyarat berikut untuk bekerja melalui artikel ini:
- Akses ke lab di DevTest Labs. Buat lab, atau gunakan lab yang ada.
- Azure PowerShell. Instal Azure PowerShell, atau gunakan Azure Cloud Shell di portal Microsoft Azure.
Skrip pembuatan VM PowerShell
Cmdlet PowerShell Invoke-AzResourceAction memanggil createEnvironment tindakan dengan ID sumber daya lab dan parameter VM. Parameter berada dalam tabel hash yang berisi semua properti VM. Properti berbeda untuk setiap jenis VM. Untuk mendapatkan properti untuk jenis VM yang Anda inginkan, lihat Mendapatkan properti VM.
Contoh skrip ini membuat VM Pusat Data Windows Server 2019. Sampel juga menyertakan properti untuk menambahkan disk data kedua di bawah dataDiskParameters.
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false)] $SubscriptionId,
[Parameter(Mandatory = $true)] $LabResourceGroup,
[Parameter(Mandatory = $true)] $LabName,
[Parameter(Mandatory = $true)] $NewVmName,
[Parameter(Mandatory = $true)] $UserName,
[Parameter(Mandatory = $true)] $Password
)
pushd $PSScriptRoot
try {
if ($SubscriptionId -eq $null) {
$SubscriptionId = (Get-AzContext).Subscription.SubscriptionId
}
$API_VERSION = '2016-05-15'
$lab = Get-AzResource -ResourceId "/subscriptions/$SubscriptionId/resourceGroups/$LabResourceGroup/providers/Microsoft.DevTestLab/labs/$LabName"
if ($lab -eq $null) {
throw "Unable to find lab $LabName resource group $LabResourceGroup in subscription $SubscriptionId."
}
$virtualNetwork = @(Get-AzResource -ResourceType 'Microsoft.DevTestLab/labs/virtualnetworks' -ResourceName $LabName -ResourceGroupName $lab.ResourceGroupName -ApiVersion $API_VERSION)[0]
#The preceding command puts the VM in the first allowed subnet in the first virtual network for the lab.
#If you need to use a specific virtual network, use | to find the network. For example:
#$virtualNetwork = @(Get-AzResource -ResourceType 'Microsoft.DevTestLab/labs/virtualnetworks' -ResourceName $LabName -ResourceGroupName $lab.ResourceGroupName -ApiVersion $API_VERSION) | Where-Object Name -EQ "SpecificVNetName"
$labSubnetName = $virtualNetwork.properties.allowedSubnets[0].labSubnetName
#Prepare all the properties needed for the createEnvironment call.
# The properties are slightly different depending on the type of VM base.
# The virtual network setup might also affect the properties.
$parameters = @{
"name" = $NewVmName;
"location" = $lab.Location;
"properties" = @{
"labVirtualNetworkId" = $virtualNetwork.ResourceId;
"labSubnetName" = $labSubnetName;
"notes" = "Windows Server 2019 Datacenter";
"osType" = "windows"
"expirationDate" = "2022-12-01"
"galleryImageReference" = @{
"offer" = "WindowsServer";
"publisher" = "MicrosoftWindowsServer";
"sku" = "2019-Datacenter";
"osType" = "Windows";
"version" = "latest"
};
"size" = "Standard_DS2_v2";
"userName" = $UserName;
"password" = $Password;
"disallowPublicIpAddress" = $true;
"dataDiskParameters" = @(@{
"attachNewDataDiskOptions" = @{
"diskName" = "adddatadisk"
"diskSizeGiB" = "1023"
"diskType" = "Standard"
}
"hostCaching" = "ReadWrite"
})
}
}
#The following line has the same effect as invoking the
# https://azure.github.io/projects/apis/#!/Labs/Labs_CreateEnvironment REST API
Invoke-AzResourceAction -ResourceId $lab.ResourceId -Action 'createEnvironment' -Parameters $parameters -ApiVersion $API_VERSION -Force -Verbose
}
finally {
popd
}
Simpan skrip sebelumnya dalam file bernama Create-LabVirtualMachine.ps1. Jalankan skrip dengan menggunakan perintah berikut. Masukkan nilai sendiri untuk placeholder Anda.
.\Create-LabVirtualMachine.ps1 -ResourceGroupName '<lab resource group name>' -LabName '<lab name>' -userName '<VM administrative username>' -password '<VM admin password>' -VMName '<VM name to create>'
Dapatkan properti VM
Bagian ini memperlihatkan cara mendapatkan properti tertentu untuk jenis VM yang ingin Anda buat. Anda bisa mendapatkan properti dari templat Azure Resource Manager (ARM) di portal Microsoft Azure, atau dengan memanggil DevTest Labs Azure REST API.
Menggunakan portal Microsoft Azure untuk mendapatkan properti VM
Membuat VM di portal Microsoft Azure menghasilkan templat Azure Resource Manager (ARM) yang memperlihatkan properti VM. Setelah memilih basis VM, Anda dapat melihat templat ARM dan mendapatkan properti tanpa benar-benar membuat VM. Metode ini adalah cara term mudah untuk mendapatkan deskripsi VM JSON jika Anda belum memiliki VM lab jenis tersebut.
Di portal Microsoft Azure, pada halaman Gambaran Umum untuk lab Anda, pilih Tambahkan di toolbar atas.
Pada halaman Pilih dasar , pilih jenis VM yang Anda inginkan. Bergantung pada pengaturan lab, basis VM dapat menjadi gambar Marketplace Azure, gambar kustom, rumus, atau lingkungan.
Pada halaman Buat sumber daya lab , secara opsional tambahkan artefak dan konfigurasikan pengaturan lain yang Anda inginkan pada tab Pengaturan dasar dan Pengaturan tingkat lanjut .
Pada tab Pengaturan tingkat lanjut , pilih Tampilkan templat ARM di bagian bawah halaman.
Pada halaman Tampilkan templat Azure Resource Manager , tinjau templat JSON untuk membuat VM. Bagian sumber daya memiliki properti VM.
Misalnya, bagian berikut
resourcesini memiliki properti untuk VM Pusat Data Windows Server 2022:"resources": [ { "apiVersion": "2018-10-15-preview", "type": "Microsoft.DevTestLab/labs/virtualmachines", "name": "[variables('vmName')]", "location": "[resourceGroup().location]", "properties": { "labVirtualNetworkId": "[variables('labVirtualNetworkId')]", "notes": "Windows Server 2022 Datacenter: Azure Edition Core", "galleryImageReference": { "offer": "WindowsServer", "publisher": "MicrosoftWindowsServer", "sku": "2022-datacenter-azure-edition-core", "osType": "Windows", "version": "latest" }, "size": "[parameters('size')]", "userName": "[parameters('userName')]", "password": "[parameters('password')]", "isAuthenticationWithSshKey": false, "labSubnetName": "[variables('labSubnetName')]", "disallowPublicIpAddress": true, "storageType": "Standard", "allowClaim": false, "networkInterface": { "sharedPublicIpAddressConfiguration": { "inboundNatRules": [ { "transportProtocol": "tcp", "backendPort": 3389 } ] } } } } ],Salin dan simpan templat untuk digunakan dalam otomatisasi PowerShell di masa mendatang, dan transfer properti ke skrip pembuatan PowerShell VM.
Menggunakan DevTest Labs Azure REST API untuk mendapatkan properti VM
Anda juga dapat memanggil DevTest Labs REST API untuk mendapatkan properti VM lab yang ada. Anda dapat menggunakan properti tersebut untuk membuat lebih banyak VM lab dengan jenis yang sama.
- Pada halaman Virtual Machines - list , pilih Coba di atas blok kode pertama.
- Pada halaman Coba REST API :
- Di bawah labName, masukkan nama lab Anda.
- Di bawah labResourceGroup, masukkan nama grup sumber daya lab.
- Di bawah subscriptionId, pilih langganan Azure lab.
- Pilih Jalankan.
- Di bagian Respons di bawah Isi, lihat properti untuk semua VM yang ada di lab.
Mengatur kedaluwarsa Mesin Virtual
Dalam skenario pelatihan, demo, dan uji coba, Anda dapat menghindari biaya yang tidak perlu dengan menghapus VM secara otomatis pada tanggal tertentu. Anda dapat mengatur properti VM expirationDate saat membuat VM. Skrip pembuatan PowerShell VM sebelumnya dalam artikel ini menetapkan tanggal kedaluwarsa di bawah properties:
"expirationDate": "2022-12-01"
Anda juga dapat mengatur tanggal kedaluwarsa pada VM yang ada dengan menggunakan PowerShell. Skrip PowerShell berikut menetapkan tanggal kedaluwarsa untuk VM lab yang sudah ada jika belum memiliki tanggal kedaluwarsa:
# Enter your own values:
$subscriptionId = '<Lab subscription Id>'
$labResourceGroup = '<Lab resource group>'
$labName = '<Lab name>'
$VmName = '<VM name>'
$expirationDate = '<Expiration date, such as 2022-12-16>'
# Sign in to your Azure account
Select-AzSubscription -SubscriptionId $subscriptionId
$VmResourceId = "subscriptions/$subscriptionId/resourcegroups/$labResourceGroup/providers/microsoft.devtestlab/labs/$labName/virtualmachines/$VmName"
$vm = Get-AzResource -ResourceId $VmResourceId -ExpandProperties
# Get the Vm properties
$VmProperties = $vm.Properties
# Set the expirationDate property
If ($VmProperties.expirationDate -eq $null) {
$VmProperties | Add-Member -MemberType NoteProperty -Name expirationDate -Value $expirationDate -Force
} Else {
$VmProperties.expirationDate = $expirationDate
}
Set-AzResource -ResourceId $VmResourceId -Properties $VmProperties -Force