使用 && 对多个属性进行 AZ CLI 查询筛选
AZ CLI query filter on multiple properties using &&
我正在尝试创建一个 az cli 查询来评估我是否登录了正确的租户和订阅。我知道我必须使用 ?
和 &&
运算符,但无法将它们组合成正确的组合,但这会起作用。当我使用下面的行查询单个值时,工作正常:
az account list --query "[?id=='my_subscription_id']" --output json
但是当我尝试下面的任何一行时,它告诉我它是无效的 jmespath_type 值:
az account list --query "[?id=='my_subscription_id' && ?tenantId=='my_tenant_id']" --output json
az account list --query "[(?id=='my_subscription_id') && (?tenantId=='my_tenant_id')]" --output json
当我尝试下面的行时,它给了我错误 ] was unexpected at this time
:
az account list --query "[(?id=='my_subscription_id')&&(?tenantId=='my_tenant_id')]" --output json
我知道这可以做到,只是似乎还找不到合适的混合物。
更新信息:
经过进一步测试,我取得了一些进步,但仍不完全符合我的预期。假设租户ID是123,我要的订阅ID是ABC,我的账号也可以访问订阅ID EFG。当运行以下命令时:
az account list --query "[].{subscriptionId:id,tenantId:tenantId}"
我得到输出:
{
"subscriptionId": "ABC",
"tenantId": "123"
},
{
"subscriptionId": "EFG",
"tenantId": "123"
}
我希望 运行 下面的命令 return 只是匹配的单个记录:
az account list --query "[?id == 'ABC' && tenantid == '123'].{subscriptionId:id,tenantId:tenantId}" --output json
但是,事实并非如此。它 returns []
.
运行 下面的命令 return 是满足两个条件的单个记录:
az account list --query "[?id == 'ABC' || tenantid == '123'].{subscriptionId:id,tenantId:tenantId}" --output json
根据文档,&&
是 AND,||
是 OR。我想当 运行 中包含 ||
的命令行会 return 两个记录,但它只有 return 包含两个值的那个。
I am trying to create an az cli query that can evaluate if I am logged
into the correct tenant and subscription.
事实上,一个订阅只能信任一个租户,所以你可以只过滤订阅Id,它会得到唯一一个匹配的租户ID。在 this blog.
中阅读更多详细信息
A directory is the Azure AD service and each directory may have one or
more domains. An Azure subscription has a trust relationship with
Azure Active Directory which means that the subscription trusts Azure
AD to authenticate users, services, and devices.
A directory can have many subscriptions associated with it, but only
one tenant. Multiple subscriptions can trust the same Azure AD
directory, but each subscription can only trust a single directory.
在这种情况下,您已经知道订阅 ID。您还获得了订阅 ID 和租户 ID 映射记录的输出。您可以通过像这样过滤您的订阅 ID 来获得准确的结果。或如您所知使用它:az account list --query "[?id=='my_subscription_id']" --output json
然后您可以验证您是否登录了正确的租户。
az account list --query "[].{SubID:id,TenantID:tenantId}[?SubID=='my_subscription_id']" -o table
结果
我正在尝试创建一个 az cli 查询来评估我是否登录了正确的租户和订阅。我知道我必须使用 ?
和 &&
运算符,但无法将它们组合成正确的组合,但这会起作用。当我使用下面的行查询单个值时,工作正常:
az account list --query "[?id=='my_subscription_id']" --output json
但是当我尝试下面的任何一行时,它告诉我它是无效的 jmespath_type 值:
az account list --query "[?id=='my_subscription_id' && ?tenantId=='my_tenant_id']" --output json
az account list --query "[(?id=='my_subscription_id') && (?tenantId=='my_tenant_id')]" --output json
当我尝试下面的行时,它给了我错误 ] was unexpected at this time
:
az account list --query "[(?id=='my_subscription_id')&&(?tenantId=='my_tenant_id')]" --output json
我知道这可以做到,只是似乎还找不到合适的混合物。
更新信息:
经过进一步测试,我取得了一些进步,但仍不完全符合我的预期。假设租户ID是123,我要的订阅ID是ABC,我的账号也可以访问订阅ID EFG。当运行以下命令时:
az account list --query "[].{subscriptionId:id,tenantId:tenantId}"
我得到输出:
{
"subscriptionId": "ABC",
"tenantId": "123"
},
{
"subscriptionId": "EFG",
"tenantId": "123"
}
我希望 运行 下面的命令 return 只是匹配的单个记录:
az account list --query "[?id == 'ABC' && tenantid == '123'].{subscriptionId:id,tenantId:tenantId}" --output json
但是,事实并非如此。它 returns []
.
运行 下面的命令 return 是满足两个条件的单个记录:
az account list --query "[?id == 'ABC' || tenantid == '123'].{subscriptionId:id,tenantId:tenantId}" --output json
根据文档,&&
是 AND,||
是 OR。我想当 运行 中包含 ||
的命令行会 return 两个记录,但它只有 return 包含两个值的那个。
I am trying to create an az cli query that can evaluate if I am logged into the correct tenant and subscription.
事实上,一个订阅只能信任一个租户,所以你可以只过滤订阅Id,它会得到唯一一个匹配的租户ID。在 this blog.
中阅读更多详细信息A directory is the Azure AD service and each directory may have one or more domains. An Azure subscription has a trust relationship with Azure Active Directory which means that the subscription trusts Azure AD to authenticate users, services, and devices.
A directory can have many subscriptions associated with it, but only one tenant. Multiple subscriptions can trust the same Azure AD directory, but each subscription can only trust a single directory.
在这种情况下,您已经知道订阅 ID。您还获得了订阅 ID 和租户 ID 映射记录的输出。您可以通过像这样过滤您的订阅 ID 来获得准确的结果。或如您所知使用它:az account list --query "[?id=='my_subscription_id']" --output json
然后您可以验证您是否登录了正确的租户。
az account list --query "[].{SubID:id,TenantID:tenantId}[?SubID=='my_subscription_id']" -o table
结果