简体   繁体   English

如何将 powershell 脚本传递给 VM? Escaping \ 似乎不起作用

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

I am trying to have my terraform script run a powershell script on a VM when it's provisioned.我试图让我的 terraform 脚本在配置时在 VM 上运行 powershell 脚本。 I know it's trying to run but it's erroring out.我知道它正在尝试运行,但它出错了。 I believe it's because the backslash in the file paths.我相信这是因为文件路径中的反斜杠。 I've tried escaping it, by making each single back slash into a double, but then it seems to be passed literally instead of as a simple single backslash, and that is failing too.我已经尝试过 escaping 它,通过将每个单反斜杠变成双反斜杠,但它似乎是按字面意思传递而不是作为简单的单反斜杠传递,这也失败了。

So how do I do it?那么我该怎么做呢? anyone?任何人? thank you much非常感谢

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]
}

After my validation, the following terraform template is working.经过我的验证,以下 terraform 模板正在运行。 For more information, you could refer to this terraform-azurerm-promote-dc sample.有关更多信息,您可以参考此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

}

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建Powershell脚本在RM中克隆虚拟机 - How do I create a powershell script to clone a vm in RM Azure VM 运行 PowerShell 脚本不 Output - Azure VM Run PowerShell Script doesn't Output 如何在使用 Terraform (Azure) 创建 Windows VM 资源时运行 Powershell7 脚本 - How do I run a Powershell7 Script upon creation of a Windows VM Resource with Terraform (Azure) 如何在Powershell脚本中将MS Web Deploy安装到Azure Windows Server 2012 R2 VM? - How do I install MS Web Deploy to an Azure Windows Server 2012 R2 VM in a powershell script? 在远程机器上运行 PowerShell 脚本在 Azure Devops 中不起作用 - Running a PowerShell script on a remote machine doesn't work in Azure Devops 如何在Powershell中从Azure获取VM映像? - How do I get a VM Image from Azure in Powershell? 用于修改/更新 Azure VM 标记值的代码在 Azure PowerShell Runbook 中不起作用 - Codes for modifying/updating Azure VM Tags value doesn't work in Azure PowerShell Runbook Azure 自动化无法将参数传递给 VM 内的 powershell 脚本 - Azure automation cannot pass parameters to powershell script inside VM PowerShell - Set-Culture 似乎没有改变任何东西 - PowerShell - Set-Culture doesn't seem to change anything 在Azure自动化PowerShell脚本中从SQL打印消息不起作用 - Printing messages from SQL in Azure Automation powershell script doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM