首页 > 解决方案 > Powershell 计算 azure CLI 结果

问题描述

我正在尝试实现我的输出,如果它是一行,它会被写在一个变量中

这就是我到目前为止所拥有的

az group list --query '[].{name:name}' --output table
$filter = Read-Host -Prompt "Please filter to find the correct resource group"
az group list --query "[?contains(name, '$filter')].name" --output tsv

此代码的作用是您可以过滤所有资源组,然后您可以看到 TSV 中的输出

我要添加的是它检查它是否是唯一的行,然后写掉那行(如果它是一行)

标签: azurepowershellazure-cliazure-cli2

解决方案


既然你已经在使用 powershell,为什么不直接使用 powershell?

$filter = Read-Host -Prompt "Please filter to find the correct resource group"
Get-AzResourceGroup | Where-Object { $_.ResourceGroupName -eq $filter }

感谢您更改代码的帮助!现在的最终结果是:

Connect-AzureRmAccount
(get-azurermresourcegroup).ResourceGroupName 
$filter = Read-Host -Prompt "Please filter to find the correct resource group" 
$RGName = get-azurermresourcegroup | Where-Object { $_.ResourceGroupName -match $filter } 
$RGName.resourcegroupname

推荐阅读