如何在terraform local exec中运行powershell脚本

如何在terraform local exec中运行powershell脚本,terraform,terraform-provider-azure,Terraform,Terraform Provider Azure,我正试图在terraform模板中的本地exec provisioner的帮助下更改webapp的堆栈 # Create azure Webapp 1 resource "azurerm_app_service" "webapp1" { name = var.webapp1 location = azurerm_resource_group.dev.location resource_group_name = azurerm_res

我正试图在terraform模板中的本地exec provisioner的帮助下更改webapp的堆栈

# Create azure Webapp 1
resource "azurerm_app_service" "webapp1" {
  name                = var.webapp1
  location            = azurerm_resource_group.dev.location
  resource_group_name = azurerm_resource_group.dev.name
  app_service_plan_id = azurerm_app_service_plan.dev.id
  https_only          = "true"
  site_config {
    always_on                 = "true"
    ftps_state                = "FtpsOnly"
    dotnet_framework_version  = "v4.0"
    http2_enabled             = "true"
    min_tls_version           = "1.2"
    use_32_bit_worker_process = "false"
    default_documents = ["hostingstart.html"]

  }
 provisioner "local-exec" {
 command =<<EOT  
$PropertiesObject = @{"CURRENT_STACK" = "dotnetcore"}
New-AzResource -PropertyObject $PropertiesObject -ResourceGroupName azurerm_resource_group.dev.name -ResourceType Microsoft.Web/sites/config/metadata -ResourceName azurerm_app_service.webapp1.name -ApiVersion 2018-02-01 -Force
EOT}


知道如何修复它吗。

我宁愿使用文件,而不是原始脚本,因为脚本将来可能会更改,而且在单独的文件中更改更具可读性和舒适性

遵循以下步骤-

  • 为PowerShell脚本创建新文件(确保扩展名为.ps1)
  • 粘贴以下代码:
  • Param
    (
    [参数(必需=$True)]
    [字符串]$ResourceGroupName,
    [参数(必需=$True)]
    [字符串]$ResourceType,
    [参数(必需=$True)]
    [字符串]$ApiVersion,
    [参数(必需=$True)]
    [字符串]$PropertiesObject
    )   
    新建AzResource-PropertyObject$PropertiesObject-ResourceGroupName$ResourceGroupName-ResourceType$ResourceType-ResourceName$ResourceName-ApiVersion$ApiVersion-Force
    
  • 创建相关变量:
  • 变量“apiversion”{
    type=“string”
    default=“2018-02-01”
    }
    变量“resourcetype”{
    type=“string”
    default=“Microsoft.Web/sites/config/metadata 1”
    }
    变量“propertiesobject”{
    type=“映射”
    默认值={
    “当前_堆栈”=“dotnetcore”
    }
    }
    
  • 执行脚本并传递相关参数:
  • provisioner“本地执行”{
    command=“PowerShell-file.\%STEP 1%.ps1-ResourceGroupName azurerm\u resource\u group.dev.NAME-ResourceType${var.ResourceType}-apivision${var.apivision}-PropertiesObject${var.PropertiesObject}”
    }
    

    此示例尚未经过测试,但这就是我使用其他PowerShell脚本的方式。

    我更喜欢使用文件,而不是原始脚本,因为脚本将来可能会更改,并且在单独的文件中更改更具可读性和舒适性

    遵循以下步骤-

  • 为PowerShell脚本创建新文件(确保扩展名为.ps1)
  • 粘贴以下代码:
  • Param
    (
    [参数(必需=$True)]
    [字符串]$ResourceGroupName,
    [参数(必需=$True)]
    [字符串]$ResourceType,
    [参数(必需=$True)]
    [字符串]$ApiVersion,
    [参数(必需=$True)]
    [字符串]$PropertiesObject
    )   
    新建AzResource-PropertyObject$PropertiesObject-ResourceGroupName$ResourceGroupName-ResourceType$ResourceType-ResourceName$ResourceName-ApiVersion$ApiVersion-Force
    
  • 创建相关变量:
  • 变量“apiversion”{
    type=“string”
    default=“2018-02-01”
    }
    变量“resourcetype”{
    type=“string”
    default=“Microsoft.Web/sites/config/metadata 1”
    }
    变量“propertiesobject”{
    type=“映射”
    默认值={
    “当前_堆栈”=“dotnetcore”
    }
    }
    
  • 执行脚本并传递相关参数:
  • provisioner“本地执行”{
    command=“PowerShell-file.\%STEP 1%.ps1-ResourceGroupName azurerm\u resource\u group.dev.NAME-ResourceType${var.ResourceType}-apivision${var.apivision}-PropertiesObject${var.PropertiesObject}”
    }
    

    此示例尚未测试,但这就是我使用其他PowerShell脚本的方式。

    让我试试。当然,将映射变量传递到PowerShell脚本中可能会出现一些问题。在更糟糕的情况下,将其硬编码到脚本中。如果可以,请告诉我assist@VivekSingh,你测试过溶液了吗?它有效吗?@MoonHorse我测试了一个非常类似的解决方案,它有效。。如果此示例不适用,请让我知道,我会修复它,让我尝试一下。当然,将映射变量传递到PowerShell脚本中可能会出现一些问题。在更糟糕的情况下,将其硬编码到脚本中。如果可以,请告诉我assist@VivekSingh,你测试过溶液了吗?它有效吗?@MoonHorse我测试了一个非常类似的解决方案,它有效。。如果这个例子没有,请让我知道,我会解决它
    azurerm_app_service.webapp1 (local-exec): Executing: ["cmd" "/C" "    $PropertiesObject = @{\r\n    \"CURRENT_STACK\" = \"dotnetcore\"\r\n    }\r\n    New-AzResource -PropertyObject $PropertiesObject -ResourceGroupName azurerm_resource_group.dev.name -ResourceType Microsoft.Web/sites/config/metadata -ResourceName azurerm_app_service.webapp1.name -ApiVersion 2018-02-01 -Force  \r\n"]
    azurerm_app_service.webapp1 (local-exec): '$PropertiesObject' is not recognized as an internal or external command,
    azurerm_app_service.webapp1 (local-exec): operable program or batch file.
    
    
    Error: Error running command '    $PropertiesObject = @{
        "CURRENT_STACK" = "dotnetcore"
        }
        New-AzResource -PropertyObject $PropertiesObject -ResourceGroupName azurerm_resource_group.dev.name -ResourceType Microsoft.Web/sites/config/metadata -ResourceName azurerm_app_service.webapp1.name -ApiVersion 2018-02-01 -Force
    ': exit status 1. Output: '$PropertiesObject' is not recognized as an internal or external command,
    operable program or batch file.