繁体   English   中英

使用 ARM 模板在 Azure VM 上安装应用程序或软件

[英]Install applications or software on Azure VM with ARM Template

我们正在尝试使用ARM模板在 Azure VM 上安装Kaspersky Network Agent

此外,我们需要使用SAS令牌从 VM 存储中获取.exe.msi文件。 我正在寻找要操作的模板示例,但无法找到完成任务的模板。 你知道,如果有可能这样做吗?

如果是这样,你能分享一个执行类似任务的模板吗? 另外,请说明如何修改此案例的模板。

提前致谢

• 是的,您可以使用Azure VM 中ARM 模板中的自定义脚本扩展成功安装应用程序,如下所示。 请检查我为此目的部署的 ARM 模板文件。 此外,我在部署期间使用 SAS 令牌下载 Azure VM 中的应用程序包,还使用了 powershell 脚本来调用相关应用程序的静默安装。

ARM 模板:-

我正在使用默认快速入门模板通过 ARM 模板部署 Azure VM,如下链接所示: - https://docs.microsoft.com/en-us/azure/virtual-machines/windows/quick-create-template? toc=/azure/azure-resource-manager/templates/toc.json

在此模板中,我在上述 ARM 模板的“资源”部分添加了以下自定义脚本扩展安装内容。 请正确检查 ARM 模板代码的格式,即逗号、大括号、方括号等。另外,请确保打开 HTTPS 端口 443 入站,如下所示:-

"securityRules": [
      {
        "name": "default-allow-3389",
        "properties": {
          "priority": 1000,
          "access": "Allow",
          "direction": "Inbound",
          "destinationPortRange": "3389",
          "protocol": "Tcp",
          "sourcePortRange": "*",
          "sourceAddressPrefix": "*",
          "destinationAddressPrefix": "*"
        }
      },
      {
        "name": "AllowHTTPSInBound",
        "properties": {
          "priority": 1010,
          "access": "Allow",
          "direction": "Inbound",
          "destinationPortRange": "443",
          "protocol": "Tcp",
          "sourcePortRange": "*",
          "sourceAddressPrefix": "*",
          "destinationAddressPrefix": "*"
        }
      }
  ]

自定义脚本 VM 扩展:-

 {
          "type": "Microsoft.Compute/virtualMachines/extensions",
          "apiVersion": "2021-04-01",
          "name": "[concat(parameters('vmName'),'/', 'InstallWebServer')]",
          "location": "[parameters('location')]",
          "dependsOn": [
            "[concat('Microsoft.Compute/virtualMachines/',parameters('vmName'))]"
          ],
        "properties": {
        "publisher": "Microsoft.Compute",
        "type": "CustomScriptExtension",
        "typeHandlerVersion": "1.7",
        "autoUpgradeMinorVersion": true,
        "protectedSettings": {
          "storageAccountName": "techtrix",
          "storageAccountKey": "EN6iUzOfVe8Ht0xvyxnqK/iXEGTEunznASsumuz0FR4SCvc2mFFHUJfbMy1/GSK7gXk0MB38MMo7+AStoKxC/w==",
          "fileUris": [
            "https://techtrix.blob.core.windows.net/executable/Testdemo2.ps1"
          ],
          "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File Testdemo2.ps1"
        }
      }
    }

此外,请注意,您需要已预配存储帐户容器以在其中存储 powershell 脚本和应用程序包,以便您可以使用该存储帐户的密钥、其名称和 powershell 脚本的 blob URI 来代替请求的相同以上。 此外,请通过“commandToExecute”部分中的扩展名更改要执行的 powershell 脚本的名称。

完成上述操作后,请确保本地安装应用程序包的静默安装命令成功执行,以便在powershell脚本中进行相应修改。 我在这里使用安装的“7-zip”应用程序进行演示。 请在下面找到我的 powershell 脚本。 确保事先上传了此脚本和应用程序包,并且容器的访问级别设置为“匿名和公共访问”:-

 Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
 Install-Module -Name Az.Storage -AllowClobber -Force
 Import-Module -Name Az.Storage -Force
 $StorageAccountName = "techtrix"
 $ContainerName = "executable"
 $Blob1Name = "7z2107-x64.exe"
 $TargetFolderPath = "C:\"
 $context = New-AzStorageContext -StorageAccountName $StorageAccountName -SASToken "sp=r&st=2022-02-10T08:40:34Z&se=2022-02-10T16:40:34Z&spr=https&sv=2020-08-04&sr=b&sig=DRDulljKTJiRbVPAXAJkTHi8QlnlbjPpVR3aueEf9xU%3D"
 Get-AzStorageBlobContent -Blob $Blob1Name -Container $ContainerName -Context $context -Destination $TargetFolderPath
 $arg="/S"
 Start-Process -FilePath "C:\7z2107-x64.exe" -ArgumentList $arg ’

然后使用“adminUsername”、“adminPassword”和“location”中的所需值编辑参数文件,并将其保存在存储模板文件的相同位置。 现在,在本地使用提升的权限从 powershell 控制台执行以下命令,即通过在 powershell 本身中浏览到该路径来存储这些 ARM 模板文件的路径。

 az login
 az deployment group create -n <name of the deployment> -g <name of the resource group> --template-file "azuredeployVM.json" --parameters "azuredeployVM.parameters.json" ’

成功部署后,您将能够看到在虚拟机创建过程中安装的应用程序,如下所示:-

应用安装路径 应用程序安装 部署状态 分机状态

这样,您可以通过带有自定义脚本扩展名的 ARM 模板安装“.exe”或“.msi”。

安装 .exe 文件需要提升权限才能以管理员身份运行。

自定义脚本在尝试运行 .exe 文件时不起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM