如何将 powershell 脚本传递给虚拟机?转义 \ 似乎不起作用

How do I pass a powershell script to a VM? Escaping \ doesn't seem to work

我正在尝试让我的 Terraform 脚本 运行 在 VM 上配置一个 powershell 脚本。我知道它正在尝试 运行 但它出错了。我相信这是因为文件路径中的反斜杠。我试过通过将每个单反斜杠变成双斜杠来转义它,但它似乎是按字面意思传递的,而不是作为简单的单反斜杠传递的,这也失败了。

那我该怎么做呢?任何人?非常感谢

resource "azurerm_virtual_machine_extension" "dc" {
  name                 = var.DomainControllerVMName
  virtual_machine_id   = azurerm_windows_virtual_machine.dc.id
  publisher            = "Microsoft.Azure.Extensions"
  type                 = "CustomScript"
  type_handler_version = "2.0"

  settings = jsonencode({
    commandToExecute = "$password = convertto-securestring RkP83Ls4S8wV -asplaintext -force;Install-windowsfeature -name AD-Domain-Services –IncludeManagementTool;Install-ADDSForest -CreateDnsDelegation:$false -DatabasePath C:\windows\NTDS -DomainMode WinThreshold -DomainName mdk.mydomain.com -DomainNetbiosName MDK -ForestMode WinThreshold -InstallDns:$true -SafeModeAdministratorPassword $password -LogPath C:\windows\NTDS -NoRebootOnCompletion:$false -SysvolPath C:\windows\SYSVOL -Force:$true -Confirm:$false"
  })


  tags = {
    environment = "Production"
  }

  depends_on = [azurerm_windows_virtual_machine.dc]
}

经过我的验证,以下 Terraform 模板可以正常工作。有关详细信息,您可以参考此 terraform-azurerm-promote-dc 示例。

resource "azurerm_virtual_machine_extension" "create-active-directory-forest" {
  name                 = var.DomainControllerVMName
  virtual_machine_id   = azurerm_windows_virtual_machine.dc.id
  publisher            = "Microsoft.Compute"
  type                 = "CustomScriptExtension"
  type_handler_version = "1.10"

settings = <<SETTINGS
  {
      "commandToExecute": "powershell.exe -Command \"$password = convertto-securestring Password12345 -asplaintext -force;Install-windowsfeature -name AD-Domain-Services –IncludeManagementTool;Install-ADDSForest -CreateDnsDelegation:$false -DatabasePath C:\windows\NTDS -DomainMode WinThreshold -DomainName mdk.mydomain.com -DomainNetbiosName MDK -ForestMode WinThreshold -InstallDns:$true -SafeModeAdministratorPassword $password -LogPath C:\windows\NTDS -NoRebootOnCompletion:$false -SysvolPath C:\windows\NTDS -Force:$true -Confirm:$false;shutdown -r -t 10;exit 0\""
  }
SETTINGS

}