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 menyertakan sampel skrip Azure PowerShell berikut untuk Azure DevTest Labs:
- Menambahkan pengguna eksternal ke lab
- Membuat dan menetapkan peran kustom di lab
- Mengatur ukuran komputer virtual (VM) yang diizinkan untuk lab
- Menambahkan gambar Marketplace ke lab
- Membuat gambar kustom dari hard drive virtual (VHD)
Prasyarat
- Untuk menambahkan atau menetapkan pengguna atau peran, Anda memerlukan peran Pemilik di lab, atau peran Pemilik atau Administrator Akses Pengguna dalam langganan Azure yang berisi lab.
- Untuk mengatur ukuran VM lab yang diizinkan, tambahkan gambar Marketplace, atau buat gambar kustom, Anda memerlukan setidaknya peran Kontributor di lab atau langganan Azure.
- Semua skrip memerlukan Azure PowerShell. Anda dapat menggunakan Azure Cloud Shell atau menginstal PowerShell secara lokal.
- Di Cloud Shell, pilih lingkungan PowerShell .
- Untuk penginstalan PowerShell lokal, jalankan
Update-Module -Name Azuntuk mendapatkan versi terbaru Azure PowerShell, dan jalankan Connect-AzAccount untuk masuk ke Azure. Jika Anda memiliki beberapa langganan Azure, gunakanSet-AzContext -SubscriptionId "<SubscriptionId>"untuk memberikan ID langganan yang ingin Anda gunakan.
Menambahkan pengguna eksternal ke lab
Contoh skrip PowerShell ini menambahkan pengguna eksternal ke lab dengan peran Pengguna DevTest Labs . Pengguna yang akan ditambahkan harus berada dalam ID Microsoft Entra organisasi.
Untuk menggunakan skrip, ganti nilai parameter di # Values to change bawah komentar dengan nilai Anda sendiri. Anda bisa mendapatkan nilai subscriptionId, labResourceGroup, dan labName dari halaman utama lab di portal Azure.
Skrip ini menggunakan perintah berikut:
- Get-AzADUser: Mendapatkan objek pengguna dari ID Microsoft Entra.
- New-AzRoleAssignment: Menetapkan peran Pengguna DevTest Labs ke pengguna yang ditentukan pada cakupan yang ditentukan.
# Values to change
$subscriptionId = "<Azure subscription ID>"
$labResourceGroup = "<Lab resource group name>"
$labName = "<Lab name>"
$userDisplayName = "<User display name in Microsoft Entra ID>"
# Select the Azure subscription that contains the lab. This step is optional if you have only one subscription.
Select-AzSubscription -SubscriptionId $subscriptionId
# Get the user object.
$adObject = Get-AzADUser -SearchString $userDisplayName
# Assign the role.
$labId = ('/subscriptions/' + $subscriptionId + '/resourceGroups/' + $labResourceGroup + '/providers/Microsoft.DevTestLab/labs/' + $labName)
New-AzRoleAssignment -ObjectId $adObject.Id -RoleDefinitionName 'DevTest Labs User' -Scope $labId
Membuat dan menetapkan peran pengguna lab kustom
Contoh skrip PowerShell ini membuat peran kustom yang memungkinkan pengguna lab mengubah kebijakan lab, dan menetapkan peran kustom ke pengguna eksternal. Pengguna untuk menetapkan peran harus berada dalam ID Microsoft Entra organisasi.
Untuk menggunakan skrip, ganti nilai parameter di # Values to change bawah komentar dengan nilai Anda sendiri. Anda bisa mendapatkan nilai subscriptionId, rgName, dan labName dari halaman utama lab di portal Azure.
Skrip ini menggunakan perintah berikut:
-
Get-AzProviderOperation: Mencantumkan semua operasi yang tersedia untuk
Microsoft.DevTestLabpenyedia sumber daya. - Get-AzRoleDefinition: Mencantumkan semua tindakan yang diizinkan untuk peran Pengguna DevTest Labs .
- New-AzRoleDefinition: Membuat peran kustom Kontributor Kebijakan baru.
- New-AzRoleAssignment: Menetapkan peran kustom ke pengguna yang ditentukan pada cakupan yang ditentukan.
# Values to change
$subscriptionId = "<Azure subscription ID>"
$rgName = "<Lab resource group name>"
$labName = "<Lab name>"
$userDisplayName = "<User display name in Microsoft Entra ID>"
# List all the operations for a resource provider.
Get-AzProviderOperation -OperationSearchString "Microsoft.DevTestLab/*"
# List allowed actions for a role.
(Get-AzRoleDefinition "DevTest Labs User").Actions
# Create the custom role.
$policyRoleDef = (Get-AzRoleDefinition "DevTest Labs User")
$policyRoleDef.Id = $null
$policyRoleDef.Name = "Policy Contributor"
$policyRoleDef.IsCustom = $true
$policyRoleDef.AssignableScopes.Clear()
$policyRoleDef.AssignableScopes.Add("/subscriptions/" + $subscriptionId)
$policyRoleDef.Actions.Add("Microsoft.DevTestLab/labs/policySets/policies/*")
$policyRoleDef = (New-AzRoleDefinition -Role $policyRoleDef)
# Retrieve the user object.
$adObject = Get-AzADUser -SearchString $userDisplayName
# Create the role assignment.
$scope = '/subscriptions/' + $subscriptionId + '/resourceGroups/' + $rgName + '/providers/Microsoft.DevTestLab/labs/' + $labName + '/policySets/default/policies/*'
New-AzRoleAssignment -ObjectId $adObject.Id -RoleDefinitionName "Policy Contributor" -Scope $scope
Mengatur ukuran VM yang diizinkan
Sampel skrip PowerShell ini mengatur ukuran yang diizinkan untuk membuat VM lab. Berikan informasi yang diminta oleh skrip saat diminta.
- Get-AzResource: Mendapatkan sumber daya lab dan kebijakan lab.
- Set-AzResource: Memperbarui kebijakan ukuran VM lab.
- New-AzResource: Membuat kebijakan ukuran VM lab baru.
param (
[Parameter(Mandatory=$true, HelpMessage="The name of the DevTest Lab to update")]
[string] $DevTestLabName,
[Parameter(Mandatory=$true, HelpMessage="The array of VM sizes to add")]
[Array] $SizesToAdd
)
function Get-Lab
{
$lab = Get-AzResource -ResourceType 'Microsoft.DevTestLab/labs' -Name $DevTestLabName
if(!$lab)
{
throw "Lab named $DevTestLabName was not found"
}
return $lab
}
function Get-PolicyChanges ($lab)
{
#start by finding the existing policy
$script:labResourceName = $lab.Name + '/default'
$existingPolicy = (Get-AzResource -ResourceType 'Microsoft.DevTestLab/labs/policySets/policies' -Name $labResourceName -ResourceGroupName $lab.ResourceGroupName -ApiVersion 2016-05-15) | Where-Object {$_.Name -eq 'AllowedVmSizesInLab'}
if($existingPolicy)
{
$existingSizes = $existingPolicy.Properties.threshold
$savePolicyChanges = $false
}
else
{
$existingSizes = ''
$savePolicyChanges = $true
}
if($existingPolicy.Properties.threshold -eq '[]')
{
Write-Output "Skipping $($lab.Name) because it currently allows all sizes"
return
}
# Make a list of all the current allowed sizes plus the `$SizesToAdd`.
$finalVmSizes = $existingSizes.Replace('[', '').Replace(']', '').Split(',',[System.StringSplitOptions]::RemoveEmptyEntries)
foreach($vmSize in $SizesToAdd)
{
$quotedSize = '"' + $vmSize + '"'
if(!$finalVmSizes.Contains($quotedSize))
{
$finalVmSizes += $quotedSize
$savePolicyChanges = $true
}
}
if(!$savePolicyChanges)
{
Write-Output "No policy changes required for VMSize in lab $($lab.Name)"
}
return @{
existingPolicy = $existingPolicy
savePolicyChanges = $savePolicyChanges
finalVmSizes = $finalVmSizes
}
}
function Set-PolicyChanges ($lab, $policyChanges)
{
if($policyChanges.savePolicyChanges)
{
$thresholdValue = ('[' + [String]::Join(',', $policyChanges.finalVmSizes) + ']')
$policyObj = @{
subscriptionId = $lab.SubscriptionId
status = 'Enabled'
factName = 'LabVmSize'
resourceGroupName = $lab.ResourceGroupName
labName = $lab.Name
policySetName = 'default'
name = $lab.Name + '/default/allowedvmsizesinlab'
threshold = $thresholdValue
evaluatorType = 'AllowedValuesPolicy'
}
$resourceType = "Microsoft.DevTestLab/labs/policySets/policies/AllowedVmSizesInLab"
if($policyChanges.existingPolicy)
{
Write-Output "Updating $($lab.Name) VM Size policy"
Set-AzResource -ResourceType $resourceType -ResourceName $labResourceName -ResourceGroupName $lab.ResourceGroupName -ApiVersion 2016-05-15 -Properties $policyObj -Force
}
else
{
Write-Output "Creating $($lab.Name) VM Size policy"
New-AzResource -ResourceType $resourceType -ResourceName $labResourceName -ResourceGroupName $lab.ResourceGroupName -ApiVersion 2016-05-15 -Properties $policyObj -Force
}
}
}
$lab = Get-Lab
$policyChanges = Get-PolicyChanges $lab
Set-PolicyChanges $lab $policyChanges
Menambahkan gambar Marketplace ke lab
Contoh skrip PowerShell ini menambahkan gambar Marketplace ke gambar dasar yang tersedia untuk pembuatan VM lab. Berikan informasi yang diminta oleh skrip saat diminta.
Skrip menggunakan perintah berikut:
- Get-AzResource: Mendapatkan sumber daya gambar lab, kebijakan lab, dan galeri.
- Set-AzResource: Memodifikasi kebijakan gambar Marketplace lab yang ada.
- New-AzResource: Membuat kebijakan gambar Marketplace lab baru.
param (
[Parameter(Mandatory=$true, HelpMessage="The name of the DevTest Lab to update")]
[string] $DevTestLabName,
[Parameter(Mandatory=$true, HelpMessage="The array of Marketplace image names to enable")]
[Array] $ImagesToAdd
)
function Get-Lab
{
$lab = Get-AzResource -ResourceType 'Microsoft.DevTestLab/labs' -Name $DevTestLabName
if(!$lab)
{
throw "Lab named $DevTestLabName was not found"
}
return $lab
}
function Get-PolicyChanges ($lab)
{
#start by finding the existing policy
$script:labResourceName = $lab.Name + '/default'
$existingPolicy = (Get-AzResource -ResourceType 'Microsoft.DevTestLab/labs/policySets/policies' -Name $labResourceName -ResourceGroupName $lab.ResourceGroupName -ApiVersion 2016-05-15) | Where-Object {$_.Name -eq 'GalleryImage'}
if($existingPolicy)
{
$existingImages = [Array] (ConvertFrom-Json $existingPolicy.Properties.threshold)
$savePolicyChanges = $false
}
else
{
$existingImages = @()
$savePolicyChanges = $true
}
if($existingPolicy.Properties.threshold -eq '[]')
{
Write-Output "Skipping $($lab.Name) because it currently allows all marketplace images"
return
}
$allAvailableImages = Get-AzResource -ResourceType Microsoft.DevTestLab/labs/galleryImages -Name $lab.Name -ResourceGroupName $lab.ResourceGroupName -ApiVersion 2017-04-26-preview
$finalImages = $existingImages
# loop through the requested images and add them to the finalImages list if they aren't already there
foreach($image in $ImagesToAdd)
{
$imageObject = $allAvailableImages | Where-Object {$_.Name -eq $image}
if(!$imageObject)
{
throw "Image $image is not available in the lab"
}
$addImage = $true
$parsedAvailableImage = $imageObject.Properties.imageReference
foreach($finalImage in $finalImages)
{
# determine whether or not the requested image is already allowed in this lab
$parsedFinalImg = ConvertFrom-Json $finalImage
if($parsedFinalImg.offer -eq $parsedAvailableImage.offer -and $parsedFinalImg.publisher -eq $parsedAvailableImage.publisher -and $parsedFinalImg.sku -eq $parsedAvailableImage.sku -and $parsedFinalImg.osType -eq $parsedAvailableImage.osType -and $parsedFinalImg.version -eq $parsedAvailableImage.version)
{
$addImage = $false
break
}
}
if($addImage)
{
Write-Output " Adding image $image to the lab"
$finalImages += ConvertTo-Json $parsedAvailableImage -Compress
$savePolicyChanges = $true
}
}
if(!$savePolicyChanges)
{
Write-Output "No policy changes required for allowed Marketplace Images in lab $($lab.Name)"
}
return @{
existingPolicy = $existingPolicy
savePolicyChanges = $savePolicyChanges
finalImages = $finalImages
}
}
function Set-PolicyChanges ($lab, $policyChanges)
{
if($policyChanges.savePolicyChanges)
{
$thresholdValue = '["'
for($i = 0; $i -lt $policyChanges.finalImages.Length; $i++)
{
$value = $policyChanges.finalImages[$i]
if($i -ne 0)
{
$thresholdValue = $thresholdValue + '","'
}
$thresholdValue = $thresholdValue + $value.Replace('"', '\"')
}
$thresholdValue = $thresholdValue + '"]'
$policyObj = @{
status = 'Enabled'
factName = 'GalleryImage'
threshold = $thresholdValue
evaluatorType = 'AllowedValuesPolicy'
}
$resourceType = "Microsoft.DevTestLab/labs/policySets/policies/galleryimage"
if($policyChanges.existingPolicy)
{
Write-Output "Updating $($lab.Name) Marketplace Images policy"
Set-AzResource -ResourceType $resourceType -ResourceName $labResourceName -ResourceGroupName $lab.ResourceGroupName -ApiVersion 2017-04-26-preview -Properties $policyObj -Force
}
else
{
Write-Output "Creating $($lab.Name) Marketplace Images policy"
New-AzResource -ResourceType $resourceType -ResourceName $labResourceName -ResourceGroupName $lab.ResourceGroupName -ApiVersion 2017-04-26-preview -Properties $policyObj -Force
}
}
}
$lab = Get-Lab
$policyChanges = Get-PolicyChanges $lab
Set-PolicyChanges $lab $policyChanges
Membuat gambar kustom dari file VHD
Contoh skrip PowerShell ini membuat gambar kustom DevTest Labs dari file VHD dengan menggunakan templat penyebaran dari repositori templat DevTest Labs publik. Skrip ini memerlukan file Windows VHD yang diunggah ke akun Azure Storage lab.
Untuk menggunakan skrip, ganti nilai parameter di # Values to change bawah komentar dengan nilai Anda sendiri. Anda bisa mendapatkan nilai subscriptionId, labRg, dan labName dari halaman utama lab di portal Azure.
vhdUri Dapatkan nilai dari kontainer Azure Storage tempat Anda mengunggah file VHD.
Skrip ini menggunakan perintah berikut:
- Get-AzResource: Mendapatkan sumber daya lab dan akun penyimpanan lab.
- Get-AzStorageAccountKey: Mendapatkan kunci akses untuk akun penyimpanan lab.
- New-AzResourceGroupDeployment: Menyebarkan gambar kustom dan grup sumber daya berdasarkan templat.
# Values to change
$subscriptionId = '<Azure subscription ID>'
$labRg = '<Lab resource group name>'
$labName = '<Lab name>'
$vhdUri = '<URI for the uploaded VHD>'
$customImageName = '<Name for the custom image>'
$customImageDescription = '<Description for the custom image>'
# Select the Azure subscription.
Select-AzSubscription -SubscriptionId $subscriptionId
# Get the lab object.
$lab = Get-AzResource -ResourceId ('/subscriptions/' + $subscriptionId + '/resourceGroups/' + $labRg + '/providers/Microsoft.DevTestLab/labs/' + $labName)
# Get the lab storage account and lab storage account key values.
$labStorageAccount = Get-AzResource -ResourceId $lab.Properties.defaultStorageAccount
$labStorageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $labStorageAccount.ResourceGroupName -Name $labStorageAccount.ResourceName)[0].Value
# Set up the parameters object.
$parameters = @{existingLabName="$($lab.Name)"; existingVhdUri=$vhdUri; imageOsType='windows'; isVhdSysPrepped=$false; imageName=$customImageName; imageDescription=$customImageDescription}
# Create the custom image.
New-AzResourceGroupDeployment -ResourceGroupName $lab.ResourceGroupName -Name CreateCustomImage -TemplateUri 'https://raw.githubusercontent.com/Azure/azure-devtestlab/master/samples/DevTestLabs/QuickStartTemplates/201-dtl-create-customimage-from-vhd/azuredeploy.json' -TemplateParameterObject $parameters