从URL复制文件的Azure VMSS Powershell扩展

时间:2019-02-28 16:53:57

标签: azure azure-virtual-machine azure-vm-scale-set

使用PS创建一个扩展,现在需要执行以下操作。

1:获取邮政编码 2:解压缩并复制到C:\ Scripts

目录中

这是安装扩展程序的PS(实际上会在比例尺设置下的扩展程序中创建扩展程序)

$dscConfig = @{
  "wmfVersion" = "latest";
  "configuration" = @{
    "url" = "https://foo.blob.core.windows.net/dsc.zip";
    "script" = "configure.ps1";
    "function" = "AzureDscDemo";
  };
}

$vmss = Get-AzVmss `
                -ResourceGroupName "FooVmssResource" `
                -VMScaleSetName "FooVmss"

$vmss = Add-AzVmssExtension `
    -VirtualMachineScaleSet $vmss `
    -Publisher Microsoft.Powershell `
    -Type DSC `
    -TypeHandlerVersion 2.24 `
    -Name "DSC" `
    -Setting $dscConfig

Update-AzVmss `
    -ResourceGroupName "FooVmssResource" `
    -Name "FooVmss"  `
    -VirtualMachineScaleSet $vmss

现在在dsc.zip中,我有一个名为configure.ps1的脚本,带有一个名为AzureDscDemo的函数,这是我遇到麻烦的地方。如何获取zip文件并保存到服务器上的文件路径,并且更好地将其解压缩。

Configuration AzureDscDemo {
       Node Localhost {
           File DscFile {
               Type = "Directory"
               Ensure = "Present"
               DestinationPath = "C:\Scripts"
              # Copy zip to scripts????
           }
      }
}

1 个答案:

答案 0 :(得分:1)

您无需下载并解压缩它,扩展程序将为您完成。它还将运行您从文件中指定的功能,并在提供参数时传递参数。

现在,如果您要下载其他zip文件,则需要为其编码。但这是扩展程序的工作方式:

"url" = "https://foo.blob.core.windows.net/dsc.zip" <<< get zip from this url and unzip it to a special folder on the vm
"script" = "configure.ps1" <<< load this file into memory
"function" = "AzureDscDemo" <<< call this function from inside the file

使用powershell dsc下载远程文件:

    xRemoteFile 'DownloadFile'
    {
        DestinationPath = $DestinationPath
        Uri             = $Uri
        UserAgent       = $UserAgent
        Headers         = $Headers
    }

https://github.com/PowerShell/xPSDesiredStateConfiguration/blob/dev/Examples/xRemoteFile_DownloadFileConfig.ps1