简体   繁体   English

如何将命令输出设置为env。 在Windows的CMD变量?

[英]How to set command output as env. variable in windows cmd?

I have saved azure storage key in key vault and i want to retrieve the key using Azure cli and set it as env. 我已将Azure存储密钥保存在密钥库中,并且我想使用Azure cli检索密钥并将其设置为env。 variable in window cmd before i run terraform script. 在运行Terraform脚本之前,窗口cmd中的变量。

Below listed command doesn't work, can anyone tell me what needs to be changed ? 下面列出的命令不起作用,有人可以告诉我需要更改什么吗?

set ARM_ACCESS_KEY=$(az keyvault secret show --name terraform-backend-key --vault-name myKeyVault)

Error on initializing 初始化时出错

在此处输入图片说明

Main.tf Main.tf

variable "count" {}
variable "prefix" {
default="RG"
 }
terraform {
backend "azurerm" {
container_name       = "new"
storage_account_name  = "mfarg"
key                  = "terraform.tfstate"
}}
resource "azurerm_resource_group" "test" {
count ="${var.count}"
name     = "${var.prefix}-${count.index}"
location = "West US 2"
}

Command prompt output 命令提示符输出

在此处输入图片说明

To set the environment variable in Windows, I suggest you use the PowerShell command to achieve it. 要在Windows中设置环境变量,建议您使用PowerShell命令来实现它。 In PowerShell, you can just do it like this: 在PowerShell中,您可以这样操作:

$env:ACCESS_KEY=$(az keyvault secret show -n terraform-backend-key --vault-name myKeyVault --query value -o tsv)

Also, in your CLI command, you could not show the secret directly, it outputs the whole secret not just the access key as you want. 另外,在CLI命令中,您无法直接显示密码,它会输出整个密码,而不仅仅是您想要的访问密钥。 See the difference between the two commands. 查看两个命令之间的区别。

在此处输入图片说明

A late answer, but perhaps useful to those who still have the same problem. 答案较晚,但对仍然存在相同问题的人可能有用。 This method will work in windows Command prompt, cmd. 此方法将在Windows命令提示符cmd中工作。

For /f %%i in ('az keyvault secret show --vault-name "Your-KeyVault-Name" --name "Your-Secret-Name" --query "value"') do set "password=%%i"

Now if you just run "echo %password%" you will see your secret value. 现在,如果您只运行“ echo%password%”,您将看到您的秘密值。 Remember that az command has to be between ' ', like 'az keyvault secret etc'. 请记住,az命令必须位于“”之间,例如“ az keyvault secret等”。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM