自定义脚本扩展在 Azure 虚拟机上下载并执行脚本。 此扩展适用于部署后配置、软件安装或其余任何配置/管理任务。 能够从 Azure 存储或 GitHub 下载脚本,或者在扩展运行时将脚本提供给 Azure 门户。 自定义脚本扩展与 Azure Resource Manager 模板集成,也可使用 Azure CLI、PowerShell、Azure 门户或 Azure 虚拟机 REST API 运行它。git
本文档详细说明了如何经过 Azure PowerShell 模块和 Azure Resource Manager 模板使用自定义脚本扩展,同时详细说明了 Windows 系统上的故障排除步骤。github
能够针对 Windows 10 客户端、Windows Server 2008 R二、20十二、2012 R2 和 2016 版本运行适用于 Windows 的自定义脚本扩展。sql
脚本须要存储在 Azure Blob 存储中或可经过有效 URL 访问的任何其余位置。shell
适用于 Windows 的自定义脚本扩展要求目标虚拟机已链接到 Internet。json
如下 JSON 显示自定义脚本扩展的架构。 扩展须要脚本位置(Azure 存储或其余具备有效 URL 的位置)以及命令才能执行。 若是使用 Azure 存储做为脚本源,则需 Azure 存储账户名称和账户密钥。 这些项目应视为敏感数据,并在扩展的受保护设置配置中指定。 Azure VM 扩展的受保护设置数据已加密,而且只能在目标虚拟机上解密。windows
{
"apiVersion": "2015-06-15", "type": "Microsoft.Compute/virtualMachines/extensions", "name": "config-app", "location": "[resourceGroup().location]", "dependsOn": [ "[concat('Microsoft.Compute/virtualMachines/', variables('vmName'),copyindex())]", "[variables('musicstoresqlName')]" ], "tags": { "displayName": "config-app" }, "properties": { "publisher": "Microsoft.Compute", "type": "CustomScriptExtension", "typeHandlerVersion": "1.9", "autoUpgradeMinorVersion": true, "settings": { "fileUris": [ "script location" ] }, "protectedSettings": { "commandToExecute": "myExecutionCommand", "storageAccountName": "myStorageAccountName", "storageAccountKey": "myStorageAccountKey" } } }
名称 | 值/示例 |
---|---|
apiVersion | 2015-06-15 |
publisher | Microsoft.Compute |
type | 扩展 |
typeHandlerVersion | 1.9 |
fileUris(例如) | https://raw.githubusercontent.com/Microsoft/dotnet-core-sample-templates/master/dotnet-core-music-windows/scripts/configure-music-app.ps1 |
commandToExecute(例如) | powershell -ExecutionPolicy Unrestricted -File configure-music-app.ps1 |
storageAccountName(例如) | examplestorageacct |
storageAccountKey(例如) | TmJK/1N3AbAZ3q/+hOXoi/l73zOqsaxXDhqa9Y83/v5UpXQp2DQIBuv2Tifp60cE/OaHsJZmQZ7teQfczQj8hg== |
注意 - 这些属性名称区分大小写。 使用上述名称可避免部署问题。api
可以使用 Azure Resource Manager 模板部署 Azure VM 扩展。 能够在 Azure 资源管理器模板中使用上一部分中详细介绍的 JSON 架构,以便在 Azure 资源管理器模板部署过程当中运行自定义脚本扩展。 若需包含自定义脚本扩展的示例模板,可访问 GitHub。架构
可使用 Set-AzureRmVMCustomScriptExtension
命令将自定义脚本扩展添加到现有虚拟机。 有关详细信息,请参阅 Set-AzureRmVMCustomScriptExtension 。app
Set-AzureRmVMCustomScriptExtension -ResourceGroupName myResourceGroup ` -VMName myVM ` -Location myLocation ` -FileUri myURL ` -Run 'myScript.ps1' ` -Name DemoScriptExtension
有关扩展部署状态的数据能够从 Azure 门户和使用 Azure PowerShell 模块进行检索。 若要查看给定 VM 的扩展部署状态,请运行如下命令。ui
Get-AzureRmVMExtension -ResourceGroupName myResourceGroup -VMName myVM -Name myExtensionName
扩展执行输出将记录到可在目标虚拟机上的如下目录中找到的文件中。
C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension
指定的文件下载到目标虚拟机上的如下目录中。
C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.*\Downloads\<n>
其中,<n>
是一个十进制整数,能够在不一样的扩展执行之间更改。 1.*
值与扩展的 typeHandlerVersion
的当前实际值匹配。 例如,实际目录多是 C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.8\Downloads\2
。
执行 commandToExecute
命令时,扩展会将该目录(例如 ...\Downloads\2
)设置为当前的工做目录。 这样能够经过 fileURIs
属性使用相对路径查找下载的文件。 请参阅下表中的示例。
绝对下载路径可能会随时间而变化,所以在可能状况下,最好是在 commandToExecute
字符串中选择使用相对的脚本/文件路径。 例如:
"commandToExecute": "powershell.exe . . . -File './scripts/myscript.ps1'"
会为经过 fileUris
属性列表下载的文件保留第一个 URI 段以后的路径信息。 以下表所示,下载的文件映射到下载子目录中,以便反映 fileUris
值的结构。
fileUris 中的 URI | 相对下载位置 | 绝对下载位置 * |
---|---|---|
https://someAcct.blob.core.chinacloudapi.cn/aContainer/scripts/myscript.ps1 |
./scripts/myscript.ps1 |
C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.8\Downloads\2\scripts\myscript.ps1 |
https://someAcct.blob.core.chinacloudapi.cn/aContainer/topLevel.ps1 |
./topLevel.ps1 |
C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.8\Downloads\2\topLevel.ps1 |
* 如上所示,绝对目录路径会在 VM 的生存期内更改,但不会在 CustomScript 扩展的某次执行期间更改。
若是对本文中的任何观点存在疑问,能够联系 MSDN Azure 和 CSDN Azure 上的 Azure 专家。 或者,也能够提交 Azure 支持事件。 请转到 Azure 支持站点并选择“获取支持”。 有关使用 Azure 支持的信息,请阅读 Azure 支持常见问题。当即访问http://market.azure.cn