我需要部署Azure自定义脚本扩展。我的脚本是PowerShell,我想在PowerShell脚本中使用terraform变量。当我使用空资源时,我的脚本工作得很好,但我可以在Azure自定义脚本扩展中发送值。
我的地形脚本在下面
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脚本
Param(
[Parameter(Mandatory=$true)]
[string]$example_variable
)
Write-Host "The value of example_variable is: $example_variable"
扩展时出错
[
{
"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"
}
]
我没有使用?因为它正在向我展示。有没有任何解决方案,部署terraform变量在powershell脚本使用微软扩展。
2条答案
按热度按时间oewdyzsn1#
检查powershell执行后的文件内容是否正确获取。
用途
获取powershell文件。
验证码:
参考:Terraform azurerm_virtual_machine_extension, run local PowerShell Script using CustomScriptExtension - Stack Overflow
mwkjh3gx2#
在这里,我将所需的文件存储在存储帐户中,并从keyvault(在我的情况下)获取所需的秘密。
在我的file2.ps1中,我通过以下方式获得这些值
这个方法适用于linux和windows,根据操作系统,唯一的变化是你必须修改你的执行脚本。希望这有帮助!