添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

使用 JMESPath 查询获取命令输出

使用 show 命令的 --query 参数获取有关现有 Azure 资源的信息。 执行 JMESPath 查询 ,并返回 Azure 资源的一个或多个属性值。

--query 的语法区分大小写, 且特定于环境 。 如果收到空结果,请检查大写情况。 通过应用 了解 Bash、PowerShell 和 Cmd 中的 Azure CLI 语法差异来避免引用错误

除非指定了 --output 参数,否则这些示例依赖于在 为 Azure CLI 准备环境 中设置的 json 默认输出配置

获取 Azure 资源的 JSON 字典属性

使用在 了解 Bash、PowerShell 和 Cmd 直接的 Azure CLI 语法差异 中创建的存储帐户,获取新存储帐户的 primaryEndpoints

az storage account show --resource-group <msdocs-tutorial-rg-00000000> \
                        --name <msdocssa000000000> \
                        --query primaryEndpoints

控制台 JSON 字典输出:

"blob": "https://msdocssa00000000.blob.core.windows.net/", "dfs": "https://msdocssa00000000.dfs.core.windows.net/", "file": "https://msdocssa00000000.file.core.windows.net/", "internetEndpoints": null, "microsoftEndpoints": null, "queue": "https://msdocssa00000000.queue.core.windows.net/", "table": "https://msdocssa00000000.table.core.windows.net/", "web": "https://msdocssa00000000.z13.web.core.windows.net/"

获取单个 JSON 对象

指定以逗号分隔的存储帐户属性列表以返回数组(列表)中的单个属性。

az storage account show --resource-group <msdocs-tutorial-rg-00000000> \
                        --name <msdocssa000000000> \
                        --query "[id, primaryLocation, primaryEndpoints.blob, encryption.services.blob.lastEnabledTime]"

控制台 JSON 数组输出:

"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/msdocs-tutorial-rg-00000000/providers/Microsoft.Storage/storageAccounts/ msdocssa000000000", "eastus", "https://msdocssa000000000.blob.core.windows.net/", "yyyy-mm-ddT19:11:56.399484+00:00"

重命名属性

使用大括号({})和逗号分隔的列表重命名属性。 新属性名称不能包含空格。 此示例以 table 格式返回输出。

az storage account show --resource-group <msdocs-tutorial-rg-00000000> \
                        --name <msdocssa000000000> \
                        --query "{saName:name, saKind:kind, saMinTLSVersion:minimumTlsVersion}" \
                        --output table

控制台表输出。 --output table 中每个列名中第一个字母会大写:

SaName             SaKind     SaMinTLSversion
-----------------  ---------  -----------------
msdocssa000000000  StorageV2  TLS1_0

筛选查询结果

将你学到的关于引用的内容与刚刚学到的--query结合起来。 这些示例应用了筛选器。

PowerShell

在 Bash 中,不能在等号 (=) 符号之前或之后有空格。 可以选择对变量值使用引号,这样可使 msdocs-tutorial-rg-00000000"msdocs-tutorial-rg-00000000" 都是正确的。

rgName=<msdocs-tutorial-rg-00000000>
# Get a list of all Azure storage accounts that allow blob public access.
# Notice the backticks and escape characters needed for boolean values.
az storage account list --resource-group $rgName \
                        --query "[?allowBlobPublicAccess == \`true\`].name"
# Get a list of Azure storage accounts that were created in the last 30 days. Return the results as a table.
saDate=$(date +%F -d "-30days")
az storage account list --resource-group $rgName \
                        --query "[?creationTime >='$saDate'].{saName:name, createdTimeStamp:creationTime}" \
                        --output table
# Get a list of Azure storage accounts created in this tutorial
az storage account list --resource-group $rgName \
                        --query "[?contains(name, 'msdocs')].{saName:name, saKind:kind, saPrimaryLocation:primaryLocation, createdTimeStamp:creationTime}" \
                        --output table

使用 PowerShell 可以创建围绕等号 (=) 符号包含或不包含空格的变量,这样可使 rgName="msdocs-tutorial-rg-00000000"rgName = "msdocs-tutorial-rg-00000000" 都是正确的。 但是,在 PowerShell 中,必须围绕变量值使用引号。

rgName="<msdocs-tutorial-rg-00000000>"
# Get a list of all Azure storage accounts that allow blob public access.
az storage account list --resource-group $rgName `
                        --query "[?allowBlobPublicAccess == ``true``].name"
# Get a list of Azure storage accounts that were created in the last 30 days. Return the results as a table.
$saDate=Get-Date
$saDate=$saDate.AddDays(-30).tostring("yyyy-mm-dd")
az storage account list --resource-group $rgName `
                        --query "[?creationTime >='$saDate'].{saName:name, createdTimeStamp:creationTime}" `
                        --output table
# Get a list of Azure storage accounts created in this tutorial
az storage account list --resource-group $rgName `
                        --query "[?contains(name, 'msdocs')].{saName:name, saKind:kind, saPrimaryLocation:primaryLocation, createdTimeStamp:creationTime}" `
                        --output table

创建新的 Azure 资源,将输出存储在变量中

了解将命令输出存储在变量中,有利于创建输出机密应受保护的 Azure 资源。 例如,创建服务主体、重置凭据或获取 Azure 密钥保管库机密时,应保护命令输出。

创建新的 Azure Key Vault 和机密,将命令输出返回到变量。 Azure Key Vault 名称必须全局唯一,因此此示例中使用 $RANDOM 标识符。 有关更多 Azure Key Vault 命名规则,请参阅 Azure Key Vault 的常见错误代码

这些示例使用 echo 来验证变量值,因为这是教学教程。 不要对生产级环境中的机密和密码值使用 echo

PowerShell
# Set your variables.
let "randomIdentifier=$RANDOM*$RANDOM"
rgName=<msdocs-tutorial-rg-00000000>
kvName=msdocs-kv-$randomIdentifier
location=eastus
# Set your default output to none
az config set core.output=none
# Create a new Azure Key Vault returning the Key Vault ID
myNewKeyVaultID=$(az keyvault create --name $kvName --resource-group $rgName --location $location --query id --output tsv)
echo "My new Azure Kev Vault ID is $myNewKeyVaultID"
# Wait about 1 minute for your Key Vault creation to complete.
# Create a new secret returning the secret ID
kvSecretName=<myKVSecretName>
kvSecretValue=<myKVSecretValue>
myNewSecretID=$(az keyvault secret set --vault-name $kvName --name $kvSecretName --value $kvSecretValue --query id --output tsv)
echo "My new secret ID is $myNewSecretID"
# Reset your default output to json
az config set core.output=json
# Set your variables.
$randomIdentifier=(New-Guid).ToString().Substring(0,8)
$rgName="<msdocs-tutorial-rg-00000000>"
$kvName="msdocs-kv-$randomIdentifier"
$location="eastus"
# Set your default output to none
az config set core.output=none
# Create a new Azure Key Vault returning the Key Vault ID
$myNewKeyVaultID=$(az keyvault create --name $kvName --resource-group $rgName --location $location --query id --output tsv)
echo "My new Azure Kev Vault ID is $myNewKeyVaultID"
# Wait about 1 minute for your Key Vault creation to complete.
# Create a new secret returning the secret ID
$kvSecretName="<myKVSecretName>"
$kvSecretValue="<myKVSecretValue>"
$myNewSecretID=$(az keyvault secret set --vault-name $kvName --name $kvSecretName --value $kvSecretValue --query id --output tsv)
echo "My new secret ID is $myNewSecretID"
# Reset your default output to json
az config set core.output=json

获取 JSON 文件的内容并将其存储在变量中

下一部分是加入教程的“延伸任务”。 但是,若要在开发、阶段和生产环境中管理 Azure 资源,通常需要读取配置文件。

是否已准备好扩展 Azure CLI 技能? 创建包含以下 JSON 的 JSON 文件,或自己选择文件内容。 将文本文件保存到本地驱动器。 如果使用的是 Azure Cloud Shell,请使用菜单栏中的 upload/download files 图标将文本文件存储在云存储驱动器中。

"environments": { "dev": [ "id": "1", "kv-secretName": "dev1SecretName", "status": "inactive", "id": "2", "kv-secretName": "dev2SecretName", "status": "active" "stg": { "id": "3", "kv-secretName": "dev3SecretName" "prod": { "id": "4", "kv-secretName": "dev4SecretName"

将 JSON 文件的内容存储在变量中,以便在 Azure CLI 命令中进一步使用。 在此示例中,将 msdocs-tutorial.json 更改为文件的名称。 不要将 echo 命令保存在生产级脚本中,因为输出会保存在日志文件中。

PowerShell

此 Bash 脚本在 Azure Cloud Shell 中进行了测试,取决于必须在环境中安装的 Bash jq

# Show the contents of a file in the console
fileName=msdocs-tutorial.json
cat $fileName | jq
# Get a JSON dictionary object
stgKV=$(jq -r '.environments.stg."kv-secretName"' $fileName)
echo $stgKV
# Filter a JSON array
devKV=$(jq -r '.environments.dev[] | select(.status=="active") | ."kv-secretName"' $fileName)
echo $devKV

是否收到了“找不到 jq 命令”错误? 这是因为此脚本依赖于 Bash jq 命令。 在环境中安装 jq,或在 Azure Cloud Shell 中运行此脚本。

此 PowerShell 脚本已在 Windows PowerShellPowerShell 7 中进行过测试。

# Show the contents of a file in the console
$fileName="c:\myPath\msdocs-tutorial.json"
$fileContents = Get-Content -Path $fileName | ConvertFrom-Json
# Get a JSON dictionary object
$stgKV=$($fileContents.environments.stg."kv-secretName")
echo $stgKV
# Filter a JSON array
$devKV=$($fileContents.environments.dev | 
    Where-Object status -eq 'active' | 
    Select-Object -ExpandProperty 'kv-secretName')
echo $devKV

现在,你有一个存储在变量中的特定于环境的 Azure Key Vault 机密名称,可以使用它连接到 Azure 资源。 若要重复使用 Azure CLI 脚本,此方法适用于 Azure VM 和 SQL Server 连接字符串的 IP 地址。

获取更多详细信息

想要更详细地了解本教程步骤中介绍的某项主题吗? 请使用下表中的链接了解详细信息。

了解详细信息