I need to deploy azure custom script extension.My script is powershell i want to use terraform variable in powershell script. my script works perfectly when i use null resource but i can sent value in azure custom script extension.
my terraform script is below
resource "azurerm_virtual_machine_extension" "example_extension" {
name = "exampleExtension"
virtual_machine_id = azurerm_virtual_machine.example_vm.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.10"
settings = <<SETTINGS
{
"commandToExecute": "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64encode(file("testing.ps1"))}')) | Out-File -filepath testing.ps1\" && powershell -ExecutionPolicy Unrestricted -File testing.ps1 -example_variable '${var.example_variable}'"
}
SETTINGS
}
Powershell script
Param(
[Parameter(Mandatory=$true)]
[string]$example_variable
)
Write-Host "The value of example_variable is: $example_variable"
Getting error on extension
[
{
"code": "ComponentStatus/StdOut/succeeded",
"level": "Info",
"displayStatus": "Provisioning succeeded",
"message": "The value of example_variable is: \r\n\r\n\r\n Directory: C:\\Packages\\Plugins\\Microsoft.Compute.CustomScriptExtension\\1.10.15\\Downloads\\0\r\n\r\n\r\nMode LastWriteTime Length Name \r\n---- ------------- ------ ---- \r\n-a---- 5/8/2023 1:10 AM 0 .txt \r\n\r\n\r\n"
},
{
"code": "ComponentStatus/StdErr/succeeded",
"level": "Info",
"displayStatus": "Provisioning succeeded",
"message": "?Param : The term '?Param' is not recognized as the name of a cmdlet, function, script file, or operable program. \r\nCheck the spelling of the name, or if a path was included, verify that the path is correct and try again.\r\nAt C:\\Packages\\Plugins\\Microsoft.Compute.CustomScriptExtension\\1.10.15\\Downloads\\0\\testing.ps1:1 char:1\r\n+ ?Param(\r\n+ ~~~~~~\r\n + CategoryInfo : ObjectNotFound: (?Param:String) [], CommandNotFoundException\r\n + FullyQualifiedErrorId : CommandNotFoundException\r\n \r\n"
}
]
i am not using ?param as it is showing me. Is there any solution to deploy terraform variable in powershell script using microsoft extension.

testing.ps1file referenced in${base64encode(file("testing.ps1"))}has a UTF8 BOM at the start of the file which is being converted into the?in?Param, creating an invalid script when it's saved byOut-File. Try re-saving the originaltesting.ps1file without a BOM and see if that works (or at least changes the error)...