无法为 Linux vm 添加自定义脚本扩展

时间:2021-02-18 04:26:25

标签: azure powershell

我有一个 linux Cent OS 虚拟机,正在尝试向其中添加自定义脚本。如果我从门户手动执行此操作,则可以为 linux 安装自定义脚本扩展。但是我无法通过我在下面添加的 powershell 代码安装自定义脚本。需要一些帮助。我将自定义脚本文件存储在一个存储帐户中,并在 Linux 上安装自定义脚本扩展时从那里调用它。

我得到的错误是:启用失败:处理文件下载失败:下载文件失败[0]:下载文件失败:意外状态代码:实际=404预期=200

if($vmname.OSType -eq "Linux"){

    $resourcegroup=$vmname.ResourceGroupName
    $location=$vmname.Location
    $vm=$vmname.Name
    $TheURI = "https://storageaccount.blob.core.windows.net/container/cstmscript.sh" 
    $ScriptSettings = @{"fileUris" = @($TheURI); "commandToExecute" = "sh cstmscript.sh";}
     
    Set-AzVMExtension -ResourceGroupName $resourcegroup -VMName $vm -Name "Custom Script" -Publisher "Microsoft.Azure.Extensions" -TypeHandlerVersion 2.0 -ExtensionType "CustomScript" -Location $location -Settings $ScriptSettings
    }

不能使用 Set-AzVMCustomScriptExtension,因为它仅适用于 Windows 虚拟机

2 个答案:

答案 0 :(得分:1)

谢谢 Satya 顺便说一句。我需要添加访问密钥。 这是代码

if($vmname.OSType -eq "Linux"){

    $resourcegroup=$vmname.ResourceGroupName
    $location=$vmname.Location
    $vm=$vmname.Name
    $TheURI = "https://storage.blob.core.windows.net/container/cstmscript.sh" 
    $ScriptSettings = @{"fileUris" = @($TheURI); "commandToExecute" = " sh cstmscript.sh";}
    $ProtectedSettings = @{"storageAccountName"="storage";"storageAccountKey" = ""};
     
    Set-AzVMExtension -ResourceGroupName $resourcegroup -VMName $vm -Name "Custom Script" -Publisher "Microsoft.Azure.Extensions" -TypeHandlerVersion 2.0 -ExtensionType "CustomScript" -Location $location -Settings $ScriptSettings -ProtectedSettings $ProtectedSettings
    }

答案 1 :(得分:0)

我曾尝试点击上面的网址

https://storageaccount.blob.core.windows.net/container/cstmscript.sh

enter image description here

以下遇到的或多或少的 404 错误:

Enable failed: processing file downloads failed: failed to download file[0]: failed to download file: unexpected status code: actual=404 expected=200

看起来您无法访问 cstmsscript.sh 并遇到 404 - 您必须以匿名方式托管该文件,或者使用其他方式授予访问权限(例如 SAS