Passing variable values to inline PowerShell provisioner

Windows VM: Passing HCL variables to PowerShell script that has required parameters

I am trying to pass variable values to a Packer script. I need that script to do the following:

  1. Get a Windows AVD image from the Azure Marketplace
  2. Run a PowerShell script (uploaded to the temporary VM with a file provisioner) that has 2 mandatory parameters:
    1. Storage Account URL (ImageSourceAddress) (to download some packages for later local installation)
    2. Image “use type” (Privileged Access Workstation or Desktop)
  3. Run “Sysprep”, which prepares the image for duplication
  4. Upload the image to an Azure Compute Gallery as a new image version

I have everything working except the PowerShell provisioner. I can get the PowerShell provisioner to work if I hard-code the variable values into the inline code, like this example:

  provisioner "powershell" {
    inline = [
      "C://Temp//Configure_AVD_Images.ps1 -ImageSourceAddress 'http://contoso.com/avdstorage' -ImageUseType 'PAW' -Verbose"
    ]
  }

I have declared the variables in the .HCL code:

variable "ImageSourceAddress" {type = string}

variable "ImageUseType" {type = string}

The problem is that I need to be able to pass the variable values from a ‘variables.pkr.hcl’ file, to Packer, then have Packer pass those variable values to the PowerShell script. I have searched and searched. I have tried about every example I can find. No matter what I do, I either get a parameter validation error, or the script couldn’t run because a null value was passed to a parameter.

I have tried the following for the inline section of the PowerShell provisioner:

  • “C://Temp//Configure_AVD_Images.ps1 -ImageSourceAddress $ImageSourceAddress -ImageUseType $ImageUseType -Verbose”
  • “C://Temp//Configure_AVD_Images.ps1 -ImageSourceAddress {var.ImageSourceAddress} -ImageUseType {var.ImageUseType} -Verbose”
  • “C://Temp//Configure_AVD_Images.ps1 -ImageSourceAddress {ImageSourceAddress} -ImageUseType ${ImageUseType} -Verbose”

Here are some errors I am getting:

azure-arm.autogenerated_1: C:\Temp\Configure_AVD_Images.ps1 : Cannot bind argument to parameter 'ImageSourceAddress' because it is
azure-arm.autogenerated_1: an empty string.
azure-arm.autogenerated_1: At C:\Windows\Temp\script-64bb19b8-79f6-924d-9e37-27d51e2c19cd.ps1:2 char:71
azure-arm.autogenerated_1: + ... figure_AVD_Images.ps1 -ImageSourceAddress $ImageSourceAddress -ImageU ...

azure-arm.autogenerated_1: C:\Temp\Configure_AVD_Images.ps1 : Cannot validate argument on parameter 'ImageUseType'. The argument
azure-arm.autogenerated_1: "ImageUseType" does not belong to the set "CommonDesktop,PAW" specified by the ValidateSet attribute.
azure-arm.autogenerated_1: Supply an argument that is in the set and then try the command again.
azure-arm.autogenerated_1: At C:\Windows\Temp\script-64bb1cdc-b752-47e0-d280-91be26e08195.ps1:2 char:143
azure-arm.autogenerated_1: + ... geSourceAddress {ImageSourceAddress} -ImageUseType {ImageUseType} -Verbos ...

Any help is greatly appreciated.