使用 VM 映像生成器和 PowerShell 创建 Azure 虚拟桌面映像
本单元演示如何使用 Azure VM 映像生成器自动执行自定义。 然后,可以将映像分发到 Azure 计算库(以前共享映像库),可以在其中将映像复制到其他区域、控制规模,并在组织内外共享映像。
为了简化 VM 映像生成器配置的部署,我们的示例使用 Azure 资源管理器模板,其中嵌套了 VM 映像生成器模板。 此方法提供了一些优势,例如变量和参数输入。 还可以从命令行传递参数。
本单元旨在概述该过程或复制和粘贴练习。
生成 Windows 映像的注意事项
VM 大小:对于 Windows,请使用Standard_D2_v2或更高版本。 默认大小为Standard_D1_v2,不适用于 Windows。
本文使用 PowerShell 自定义工具脚本。 请使用以下设置,否则构建过程将停止响应:
"runElevated": true, "runAsSystem": true,
例如:
{ "type": "PowerShell", "name": "installFSLogix", "runElevated": true, "runAsSystem": true, "scriptUri": "https://raw.githubusercontent.com/azure/azvmimagebuilder/main/solutions/14_Building_Images_WVD/0_installConfFsLogix.ps1"
请注释你的代码:VM 映像生成器构建日志(customization.log)很冗长。 如果使用“write-host”注释脚本,则会将脚本发送到日志,这应该可以简化故障排除。
write-host 'AIB Customization: Starting OS Optimizations script'
退出代码:VM 映像生成器要求所有脚本返回 0 个退出代码。 如果使用非零退出代码,VM 映像生成器将失败自定义并停止生成。 如果您有复杂的脚本,请添加监测工具并发出退出代码,这些将显示在 customization.log 文件中。
Write-Host "Exit code: " $LASTEXITCODE
测试:在独立 VM 上测试和重新测试代码。 确保没有用户提示、使用正确的权限等。
网络:
Set-NetAdapterAdvancedProperty
在优化脚本中设置,但 VM 映像生成器生成失败。 因为它会导致网络断开,所以已被注释掉。我们在调查此问题。
先决条件
必须安装最新的 Azure PowerShell cmdlet。 有关详细信息,请参阅 Azure PowerShell 概述。
# Check to ensure that you're registered for the providers and RegistrationState is set to 'Registered'
Get-AzResourceProvider -ProviderNamespace Microsoft.VirtualMachineImages
Get-AzResourceProvider -ProviderNamespace Microsoft.Storage
Get-AzResourceProvider -ProviderNamespace Microsoft.Compute
Get-AzResourceProvider -ProviderNamespace Microsoft.KeyVault
Get-AzResourceProvider -ProviderNamespace Microsoft.ContainerInstance
# If they don't show as 'Registered', run the following commented-out code
## Register-AzResourceProvider -ProviderNamespace Microsoft.VirtualMachineImages
## Register-AzResourceProvider -ProviderNamespace Microsoft.Storage
## Register-AzResourceProvider -ProviderNamespace Microsoft.Compute
## Register-AzResourceProvider -ProviderNamespace Microsoft.KeyVault
## Register-AzResourceProvider -ProviderNamespace Microsoft.ContainerInstance
设置环境和变量
# Step 1: Import module
Import-Module Az.Accounts
# Step 2: get existing context
$currentAzContext = Get-AzContext
# Destination image resource group
$imageResourceGroup="avdImageDemoRg"
# Location (see possible locations in the main docs)
$location="westus2"
# Your subscription. This command gets your current subscription
$subscriptionID=$currentAzContext.Subscription.Id
# Image template name
$imageTemplateName="avd10ImageTemplate01"
# Distribution properties object name (runOutput). Gives you the properties of the managed image on completion
$runOutputName="sigOutput"
# Create resource group
New-AzResourceGroup -Name $imageResourceGroup -Location $location
权限、用户标识和角色
创建用户标识。
# setup role def names, these need to be unique $timeInt=$(get-date -UFormat "%s") $imageRoleDefName="Azure Image Builder Image Def"+$timeInt $identityName="aibIdentity"+$timeInt ## Add Azure PowerShell modules to support AzUserAssignedIdentity and Azure VM Image Builder 'Az.ImageBuilder', 'Az.ManagedServiceIdentity' | ForEach-Object {Install-Module -Name $_ -AllowPrerelease} # Create the identity New-AzUserAssignedIdentity -ResourceGroupName $imageResourceGroup -Name $identityName -Location $location $identityNameResourceId=$(Get-AzUserAssignedIdentity -ResourceGroupName $imageResourceGroup -Name $identityName).Id $identityNamePrincipalId=$(Get-AzUserAssignedIdentity -ResourceGroupName $imageResourceGroup -Name $identityName).PrincipalId
向标识分配权限以分发图像。 以下命令使用之前指定的参数下载并更新模板。
$aibRoleImageCreationUrl="https://raw.githubusercontent.com/azure/azvmimagebuilder/main/solutions/12_Creating_AIB_Security_Roles/aibRoleImageCreation.json" $aibRoleImageCreationPath = "aibRoleImageCreation.json" # Download the config Invoke-WebRequest -Uri $aibRoleImageCreationUrl -OutFile $aibRoleImageCreationPath -UseBasicParsing ((Get-Content -path $aibRoleImageCreationPath -Raw) -replace '<subscriptionID>',$subscriptionID) | Set-Content -Path $aibRoleImageCreationPath ((Get-Content -path $aibRoleImageCreationPath -Raw) -replace '<rgName>', $imageResourceGroup) | Set-Content -Path $aibRoleImageCreationPath ((Get-Content -path $aibRoleImageCreationPath -Raw) -replace 'Azure Image Builder Service Image Creation Role', $imageRoleDefName) | Set-Content -Path $aibRoleImageCreationPath # Create a role definition New-AzRoleDefinition -InputFile ./aibRoleImageCreation.json # Grant the role definition to the VM Image Builder service principal New-AzRoleAssignment -ObjectId $identityNamePrincipalId -RoleDefinitionName $imageRoleDefName -Scope "/subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup"
创建 Azure 计算图库
如果您还没有 Azure 计算图库,则需要创建一个。
$sigGalleryName= "myaibsig01"
$imageDefName ="win10avd"
# Create the gallery
New-AzGallery -GalleryName $sigGalleryName -ResourceGroupName $imageResourceGroup -Location $location
# Create the gallery definition
New-AzGalleryImageDefinition -GalleryName $sigGalleryName -ResourceGroupName $imageResourceGroup -Location $location -Name $imageDefName -OsState generalized -OsType Windows -Publisher 'myCo' -Offer 'Windows' -Sku '10avd'
配置 VM 映像生成器模板
我们准备了一个模板,该模板使用前面指定的参数下载和更新 VM 映像生成器模板。 该模板安装 FSLogix、作系统优化和 Microsoft Teams,并在末尾运行 Windows 更新。
如果打开模板,可以在源属性中看到正在使用的图像。 在此示例中,它使用 Windows 10 多会话映像。
Windows 10 映像
应注意两种关键类型的图像:多会话和单会话。
多会话映像适用于共享使用。 下面是 Azure 中映像详细信息的示例:
"publisher": "MicrosoftWindowsDesktop",
"offer": "Windows-10",
"sku": "20h2-avd",
"version": "latest"
单会话映像用于个人使用。 下面是 Azure 中映像详细信息的示例:
"publisher": "MicrosoftWindowsDesktop",
"offer": "Windows-10",
"sku": "19h2-ent",
"version": "latest"
还可以更改可用的 Windows 10 映像:
Get-AzVMImageSku -Location westus2 -PublisherName MicrosoftWindowsDesktop -Offer windows-10
下载并配置模板
现在,下载模板并将其配置为供自己使用。
$templateUrl="https://raw.githubusercontent.com/azure/azvmimagebuilder/main/solutions/14_Building_Images_WVD/armTemplateWVD.json"
$templateFilePath = "armTemplateWVD.json"
Invoke-WebRequest -Uri $templateUrl -OutFile $templateFilePath -UseBasicParsing
((Get-Content -path $templateFilePath -Raw) -replace '<subscriptionID>',$subscriptionID) | Set-Content -Path $templateFilePath
((Get-Content -path $templateFilePath -Raw) -replace '<rgName>',$imageResourceGroup) | Set-Content -Path $templateFilePath
((Get-Content -path $templateFilePath -Raw) -replace '<region>',$location) | Set-Content -Path $templateFilePath
((Get-Content -path $templateFilePath -Raw) -replace '<runOutputName>',$runOutputName) | Set-Content -Path $templateFilePath
((Get-Content -path $templateFilePath -Raw) -replace '<imageDefName>',$imageDefName) | Set-Content -Path $templateFilePath
((Get-Content -path $templateFilePath -Raw) -replace '<sharedImageGalName>',$sigGalleryName) | Set-Content -Path $templateFilePath
((Get-Content -path $templateFilePath -Raw) -replace '<region1>',$location) | Set-Content -Path $templateFilePath
((Get-Content -path $templateFilePath -Raw) -replace '<imgBuilderId>',$identityNameResourceId) | Set-Content -Path $templateFilePath
欢迎随时查看 模板。
提交模板
模板必须提交至服务系统。 这样做会下载任何依赖项目,例如脚本,并验证、检查权限,并将其存储在临时资源组中,其前缀为 IT_。
New-AzResourceGroupDeployment -ResourceGroupName $imageResourceGroup -TemplateFile $templateFilePath -TemplateParameterObject @{"api-Version" = "2020-02-14"; "imageTemplateName" = $imageTemplateName; "svclocation" = $location}
# Optional - if you have any errors running the preceding command, run:
$getStatus=$(Get-AzImageBuilderTemplate -ResourceGroupName $imageResourceGroup -Name $imageTemplateName)
$getStatus.ProvisioningErrorCode
$getStatus.ProvisioningErrorMessage
生成映像
Start-AzImageBuilderTemplate -ResourceGroupName $imageResourceGroup -Name $imageTemplateName -NoWait
注意
该命令不会等待 VM 映像生成器服务完成映像生成,因此可以查询状态,如下所示。
$getStatus=$(Get-AzImageBuilderTemplate -ResourceGroupName $imageResourceGroup -Name $imageTemplateName)
# Shows all the properties
$getStatus | Format-List -Property *
# Shows the status of the build
$getStatus.LastRunStatusRunState
$getStatus.LastRunStatusMessage
$getStatus.LastRunStatusRunSubState
创建 VM
生成映像后,可以从该映像生成 VM。 使用 New-AzVM(Az PowerShell 模块.Compute)中的示例。