I managed to get parameters to be passed from the data "template_file" into the PowerShell command line to execute on the server, if this helps anyone.
The credentials aren't actually needed in my case but I wanted to pass them anyway. The creds are being taken from the VM setup in Azure in my case.
resource "azurerm_virtual_machine_extension" "software" {
name = "install-software"
virtual_machine_id = azurerm_windows_virtual_machine.ADVM1.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.9"
protected_settings = <<SETTINGS
{
"commandToExecute": "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64encode(data.template_file.DomainControllerSetup.rendered)}')) | Out-File -filepath DomainControllerSetup.ps1\" && powershell -ExecutionPolicy Unrestricted -File DomainControllerSetup.ps1 -DomainName ${data.template_file.DomainControllerSetup.vars.DomainName} -AdmincredsUserName ${data.template_file.DomainControllerSetup.vars.AdmincredsUserName} -AdmincredsPassword ${data.template_file.DomainControllerSetup.vars.AdmincredsPassword}"
}
SETTINGS
}
data "template_file" "DomainControllerSetup" {
template = "${file("DomainControllerSetup.ps1")}"
vars = {
DomainName = "azlab.local"
AdmincredsUserName = "${azurerm_windows_virtual_machine.ADVM1.admin_username}"
AdmincredsPassword = "${azurerm_windows_virtual_machine.ADVM1.admin_password}"
}
}
This is my "create new forest" script - again if it helps. The creds aren't being used in this example, only the DomainName is used. However i wanted to keep it in there in case I wanted to promote a member server into an existing domain.
[CmdletBinding()]
param
(
[Parameter(ValuefromPipeline=$true,Mandatory=$true)] [string]$DomainName,
[Parameter(ValuefromPipeline=$true,Mandatory=$true)] [string]$AdmincredsUserName,
[Parameter(ValuefromPipeline=$true,Mandatory=$true)] [string]$AdmincredsPassword
)
$username = $AdmincredsUserName
$password = ConvertTo-SecureString -AsPlainText $AdmincredsPassword -Force
$Cred = New-Object System.Management.Automation.PSCredential ($username, $password)
install-windowsfeature AD-Domain-Services -IncludeManagementTools
Install-ADDSForest `
-DomainName $DomainName `
-SafeModeAdministratorPassword $password `
-CreateDnsDelegation:$false `
-DatabasePath "C:\Windows\NTDS" `
-InstallDns:$true `
-LogPath "C:\Windows\NTDS" `
-NoRebootOnCompletion:$false `
-SysvolPath "C:\Windows\SYSVOL" `
-Force:$true