共用方式為


將成品新增至 DevTest Labs VM

此文章描述如何將「成品」新增至 Azure DevTest Labs 虛擬機器 (VM)。 成品指定要採取來佈建 VM 的動作,例如,執行 Windows PowerShell 指令碼、執行 Bash 命令或安裝軟體。 您可以使用參數來自訂成品,以符合自己的需求。

DevTest Labs 成品可以來自公用 DevTest Labs Git 存放庫 (英文) 或私人 Git 存放庫。 若要建立您自己的自訂成品,並將其儲存在存放庫,請參閱建立自訂成品。 若要將成品存放庫新增至實驗室,讓實驗室使用者能夠存取自訂成品,請參閱將成品存放庫新增至實驗室

DevTest Labs 實驗室擁有者可以在建立期間,指定要安裝於所有實驗室 VM 上的必要成品。 如需詳細資訊,請參閱指定 DevTest Labs VM 的必要成品

您無法在 VM 建立期間變更或移除必要成品,但可以新增任何可用的個別成品。 此文章描述如何使用 Azure 入口網站或 Azure PowerShell,將可用的成品新增至 VM。

從 Azure 入口網站將成品新增至 VM

您可以在 VM 建立期間新增成品,或將成品新增至現有的實驗室 VM。

在 VM 建立期間新增成品:

  1. 在實驗室首頁,選取 [新增]

  2. 在 [選擇基底] 頁面上,選取您想要的 VM 類型。

  3. 在 [建立實驗室資源] 畫面上,選取 [新增或移除成品]

  4. 在 [新增成品] 頁面上,選取您想要新增至 VM 的每個成品旁的箭號。

  5. 在每個 [新增成品] 窗格上,輸入任何必要和選擇性的參數值,然後選取 [確定]。 成品會出現在 [選取的成品] 下,而且已設定的成品數目會更新。

    顯示 [新增成品] 窗格上新增成品的螢幕快照。

  6. 您可以在新增成品之後加以變更。

    • 根據預設,成品會依照您新增的順序進行安裝。 若要重新排列順序,請在 [選取的成品] 清單中選取成品旁的省略符號 ...,然後選取 [上移]、[下移]、[移至頂端] 或 [移至底部]
    • 若要編輯成品的參數,請選取 [編輯] 以重新開啟 [新增成品] 窗格。
    • 若要從 [選取的成品] 清單中刪除成品,請選取 [刪除]
  7. 當您完成新增和排列成品之後,請選取 [確定]

  8. [建立實驗室資源] 畫面會顯示新增的成品數目。 若要在建立 VM 之前新增、編輯、重新排列或刪除成品,請再次選取 [新增或移除成品]

建立 VM 之後,安裝的成品即會出現在 VM 的 [成品] 頁面上。 若要查看每個成品安裝的詳細資料,請選取成品名稱。

在現有 VM 上安裝成品:

  1. 在實驗室首頁,從 [我的虛擬機器] 清單中選取 VM。

  2. 在 VM 頁面上,選取頂端功能表列或左側導覽中的 [成品]

  3. 在 [成品] 頁面上,選取 [套用成品]

    顯示現有 V M [成品] 窗格的螢幕快照。

  4. 在 [新增成品] 頁面上,選取成品並將其設定為與新 VM 相同。

  5. 當您完成新增成品之後,請選取 [安裝]。 成品會立即安裝於 VM 上。

使用 Azure PowerShell 將成品新增至 VM

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 若要開始使用,請參閱 安裝 Azure PowerShell。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

下列 PowerShell 指令碼會使用 Invoke-AzResourceAction Cmdlet 來將成品套用至 VM。

#Requires -Module Az.Resources

param
(
[Parameter(Mandatory=$true, HelpMessage="The ID of the subscription that contains the lab")]
   [string] $SubscriptionId,
[Parameter(Mandatory=$true, HelpMessage="The name of the lab that has the VM")]
   [string] $DevTestLabName,
[Parameter(Mandatory=$true, HelpMessage="The name of the VM")]
   [string] $VirtualMachineName,
[Parameter(Mandatory=$true, HelpMessage="The repository where the artifact is stored")]
   [string] $RepositoryName,
[Parameter(Mandatory=$true, HelpMessage="The artifact to apply to the VM")]
   [string] $ArtifactName,
[Parameter(ValueFromRemainingArguments=$true)]
   $Params
)

# Set the appropriate subscription
Set-AzContext -SubscriptionId $SubscriptionId | Out-Null
 
# Get the lab resource group name
$resourceGroupName = (Get-AzResource -ResourceType 'Microsoft.DevTestLab/labs' | Where-Object { $_.Name -eq $DevTestLabName}).ResourceGroupName
if ($resourceGroupName -eq $null) { throw "Unable to find lab $DevTestLabName in subscription $SubscriptionId." }

# Get the internal repository name
$repository = Get-AzResource -ResourceGroupName $resourceGroupName `
                    -ResourceType 'Microsoft.DevTestLab/labs/artifactsources' `
                    -ResourceName $DevTestLabName `
                    -ApiVersion 2016-05-15 `
                    | Where-Object { $RepositoryName -in ($_.Name, $_.Properties.displayName) } `
                    | Select-Object -First 1

if ($repository -eq $null) { "Unable to find repository $RepositoryName in lab $DevTestLabName." }

# Get the internal artifact name
$template = Get-AzResource -ResourceGroupName $resourceGroupName `
                -ResourceType "Microsoft.DevTestLab/labs/artifactSources/artifacts" `
                -ResourceName "$DevTestLabName/$($repository.Name)" `
                -ApiVersion 2016-05-15 `
                | Where-Object { $ArtifactName -in ($_.Name, $_.Properties.title) } `
                | Select-Object -First 1

if ($template -eq $null) { throw "Unable to find template $ArtifactName in lab $DevTestLabName." }

# Find the VM in Azure
$FullVMId = "/subscriptions/$SubscriptionId/resourceGroups/$resourceGroupName`
                /providers/Microsoft.DevTestLab/labs/$DevTestLabName/virtualmachines/$virtualMachineName"

$virtualMachine = Get-AzResource -ResourceId $FullVMId

# Generate the artifact id
$FullArtifactId = "/subscriptions/$SubscriptionId/resourceGroups/$resourceGroupName`
                        /providers/Microsoft.DevTestLab/labs/$DevTestLabName/artifactSources/$($repository.Name)`
                        /artifacts/$($template.Name)"

# Handle the input parameters to pass through
$artifactParameters = @()

# Fill the artifact parameter with the additional -param_ data and strip off the -param_
$Params | ForEach-Object {
   if ($_ -match '^-param_(.*)') {
      $name = $_ -replace '^-param_'
   } elseif ( $name ) {
      $artifactParameters += @{ "name" = "$name"; "value" = "$_" }
      $name = $null #reset name variable
   }
}

# Create a structure to pass the artifact data to the action

$prop = @{
artifacts = @(
    @{
        artifactId = $FullArtifactId
        parameters = $artifactParameters
    }
    )
}

# Apply the artifact
if ($virtualMachine -ne $null) {
   # Apply the artifact by name to the virtual machine
   $status = Invoke-AzResourceAction -Parameters $prop -ResourceId $virtualMachine.ResourceId -Action "applyArtifacts" -ApiVersion 2016-05-15 -Force
   if ($status.Status -eq 'Succeeded') {
      Write-Output "##[section] Successfully applied artifact: $ArtifactName to $VirtualMachineName"
   } else {
      Write-Error "##[error]Failed to apply artifact: $ArtifactName to $VirtualMachineName"
   }
} else {
   Write-Error "##[error]$VirtualMachine was not found in the DevTest Lab, unable to apply the artifact"
}

下一步