Adding PowerShell commands in the provioners section in the following HCL tempalte

Varma 695 Reputation points
2024-02-09T09:56:02.08+00:00

I would like to add following commands in the below provsioners section, I tried itbut ending with multiple syntax errors. Please suggest

New-Item -ItemType Directory -Path "c:\Java" 

#Downloads the Java MSI file, I changed from Invoke-Webrequest to Invoke-RestMethod to ensure it waits for the download to complete before moving on
Invoke-RestMethod -uri "https://download.oracle.com/java/21/latest/jdk-21_windows-x64_bin.msi" -OutFile "c:\Java\jdk-21_windows-x64_bin.msi"

#Starts the installation, also added a Wait command here to ensure the installation is complete before moving on to next step, added /quiet to ensure no user interaction is needed.
Start-Process msiexec.exe -Wait -ArgumentList "/I c:\Java\jdk-21_windows-x64_bin.msi /quiet"

#Shortened the environmental command by using the command below, it creates a Environmental variable called #JAVA_HOME with the value of 'C:\Program Files\Java\jdk-21\bin'
#$Env:JAVA_HOME = 'C:\Program Files\Java\jdk-21\bin'

#Added back to set the variable to system instead of user
[System.Environment]::SetEnvironmentVariable("JAVA_HOME","C:\Program Files\Java\jdk-21\bin","Machine")

#Writes out the content of the environmental variable JAVA_HOME
Write-Host $Env:JAVA_HOME "has been added to the environmental variable JAVA_HOME"



I want to add in the below file under provisoner inline seciton:

source "azure-arm" "autogenerated_1" {
  azure_tags = {
    dept = "Engineering"
    task = "Image deployment"
  }
  build_resource_group_name         = "myPackerGroup"
  client_id                         = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"
  client_secret                     = "ppppppp-pppp-pppp-pppp-ppppppppppp"
  communicator                      = "winrm"
  image_offer                       = "WindowsServer"
  image_publisher                   = "MicrosoftWindowsServer"
  image_sku                         = "2016-Datacenter"
  managed_image_name                = "myPackerImage"
  managed_image_resource_group_name = "myPackerGroup"
  os_type                           = "Windows"
  subscription_id                   = "yyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyy"
  tenant_id                         = "zzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz"
  vm_size                           = "Standard_D2_v2"
  winrm_insecure                    = true
  winrm_timeout                     = "5m"
  winrm_use_ssl                     = true
  winrm_username                    = "packer"
}

build {
  sources = ["source.azure-arm.autogenerated_1"]

  provisioner "powershell" {
    inline = ["Add-WindowsFeature Web-Server", "while ((Get-Service RdAgent).Status -ne 'Running') { Start-Sleep -s 5 }", "while ((Get-Service WindowsAzureGuestAgent).Status -ne 'Running') { Start-Sleep -s 5 }", "& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit", "while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10  } else { break } }"]
  }

}

Please suggest command with no syntax error

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
6,841 questions
0 comments No comments
{count} votes

Accepted answer
  1. Martin Therkelsen 1,095 Reputation points MVP
    2024-02-09T11:26:13.3833333+00:00

    You can use this code.

    build {
      sources = ["source.azure-arm.autogenerated_1"]
    
      provisioner "powershell" {
        inline = ["Add-WindowsFeature Web-Server"]    
      }  
      provisioner "powershell" {
        inline = ["New-Item -ItemType Directory -Path 'c:/Java'",
          "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12",
          "Invoke-RestMethod -uri 'https://download.oracle.com/java/21/latest/jdk-21_windows-x64_bin.msi' -OutFile 'c:/Java/jdk-21_windows-x64_bin.msi'",
          "Start-Process msiexec.exe -Wait -ArgumentList '/I c:/Java/jdk-21_windows-x64_bin.msi /quiet'",
          "[System.Environment]::SetEnvironmentVariable('JAVA_HOME','C:/Program Files/Java/jdk-21/bin','Machine')",
          "Write-Host $Env:JAVA_HOME 'has been added to the environmental variable JAVA_HOME'" 
        ]
      }
      provisioner "powershell" {
        inline = ["while ((Get-Service RdAgent).Status -ne 'Running') { Start-Sleep -s 5 }",
         "while ((Get-Service WindowsAzureGuestAgent).Status -ne 'Running') { Start-Sleep -s 5 }",
         "& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit",
         "while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10  } else { break } }"]
      }
    
    }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful