簡體   English   中英

How to Use Powershellscript in Azure classic Release pipeline - 腳本文件存儲在Azure Devops Secure File

[英]How to Use Powershellscript in Azure classic Release pipeline - script file stored in Azure Devops Secure File

我在 ARM 模板中為 VM 使用自定義腳本擴展:

         {
   "type": "Microsoft.Compute/virtualMachines/extensions",
 "name": "[concat(parameters('vm-Name'),'-0',copyIndex(1),'/script')]",
   "apiVersion": "2015-05-01-preview",
   "location": "[resourceGroup().location]",
      "copy": {
                "name": "storagepoolloop",
                "count": "[parameters('virtualMachineCount')]"
            },
   "dependsOn": [
       "virtualMachineLoop",
       "nicLoop"
   ],
   "properties": {
       "publisher": "Microsoft.Compute",
       "type": "CustomScriptExtension",
       "typeHandlerVersion": "1.4",
       "settings": {
           "fileUris": [

       ],
      "commandToExecute": "[parameters('commandToExecute')]"
     }
   }
 }

其中參數 = "powershell.exe $(Agent.TempDirectory)/$(script.secureFilePath)"

我正在使用 azure devops 安全文件來存儲我的腳本。 在部署虛擬機之前,我有下載安全文件任務。 我也試過直接引用腳本文件名“powershell.exe $(Agent.TempDirectory)/puscript.ps1”

我正在使用經典的發布管道,如果這不是正確的方法,請指導如何使用存儲在安全文件中的 powershell 腳本。

任何幫助表示贊賞。 提前致謝。

該腳本需要下載到您正在創建的 VM 上,而不是下載到部署 ARM 的計算機上。該命令在 VM 啟動擴展之前不會實際執行,因此變量$(Agent.TempDirectory)指的是執行管道的機器上的目錄,在虛擬機啟動時不存在。

通過在我用來創建 VM 的圖像中包含腳本,我對 VM 自定義擴展做了同樣的事情。 如果您不使用自定義圖像,您可以添加存儲帳戶信息以在protectedSettings中下載它,如下所示:

"protectedSettings": {
    "commandToExecute": "powershell.exe puscript.ps1",
    "storageAccountName": "yourstorageaccount",
    "storageAccountKey": "<account key>",
    "fileUris": [
        "https://yourstorageaccount.blob.core.windows.net/container/puscript.ps1"
    ]
}

參考: https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows#extension-schema

您可以嘗試以下步驟:

  1. 使用下載安全文件任務下載 PowerShell 腳本文件。 在任務上,設置一個 Reference name 以供使用。

    在此處輸入圖像描述

  2. 使用PowerShell 任務(或Azure PowerShell 任務)執行 PowerShell 腳本。

    在此處輸入圖像描述

假設您想執行 PowerShell 腳本來運行 ARM 模板部署,您可以使用Azure PowerShell 任務

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM