適用於 Windows 的自定義腳本擴展

自定義腳本擴展在 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

Internet 鏈接

適用於 Windows 的自定義腳本擴展要求目標虛擬機已鏈接到 Internet。json

擴展架構

如下 JSON 顯示自定義腳本擴展的架構。 擴展須要腳本位置(Azure 存儲或其餘具備有效 URL 的位置)以及命令才能執行。 若是使用 Azure 存儲做爲腳本源,則需 Azure 存儲賬戶名稱和賬戶密鑰。 這些項目應視爲敏感數據,並在擴展的受保護設置配置中指定。 Azure VM 擴展的受保護設置數據已加密,而且只能在目標虛擬機上解密。windows

JSON
{
    "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架構

PowerShell 部署

可使用 Set-AzureRmVMCustomScriptExtension 命令將自定義腳本擴展添加到現有虛擬機。 有關詳細信息,請參閱 Set-AzureRmVMCustomScriptExtension app

PowerShell
Set-AzureRmVMCustomScriptExtension -ResourceGroupName myResourceGroup `  -VMName myVM `  -Location myLocation `  -FileUri myURL `  -Run 'myScript.ps1' `  -Name DemoScriptExtension 

故障排除和支持

故障排除

有關擴展部署狀態的數據能夠從 Azure 門戶和使用 Azure PowerShell 模塊進行檢索。 若要查看給定 VM 的擴展部署狀態,請運行如下命令。ui

PowerShell
Get-AzureRmVMExtension -ResourceGroupName myResourceGroup -VMName myVM -Name myExtensionName 

擴展執行輸出將記錄到可在目標虛擬機上的如下目錄中找到的文件中。

cmd
C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension 

指定的文件下載到目標虛擬機上的如下目錄中。

cmd
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 字符串中選擇使用相對的腳本/文件路徑。 例如:

JSON
"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

相關文章
相關標籤/搜索