【发布时间】:2021-06-04 21:19:32
【问题描述】:
我有以下代码:
#look up the app function name for this resource group
switch -Regex (az resource list -g $RESOURCE_NAME -o table) {
'^(\w+[.-]*\w+).*\bserverFarms\b' {
$Matches[1]; break
}
}
$FUNCTION_APP = $Matches[1]
echo $Matches.Count
echo $Matches[0]
echo $Matches[1]
az resource list 命令返回以下输出:
> az resource list -g $AZ_RESOURCE_GROUP_NAME -o table
Name ResourceGroup Location Type
---------------------- --------------- ---------- ---------------------------------
jf-aa-ext-app-12345 jfFuncs eastus microsoft.insights/components
omstorage jfFuncs eastus Microsoft.Storage/storageAccounts
jf-aa-ext-app-12345 jfFuncs eastus Microsoft.Web/serverFarms
jf-aa-ext-app-12345 jfFuncs eastus Microsoft.Web/sites
尝试从包含“serverFarms”的行中提取“jf-aa-ext-app-12345”。 但它只返回“jf-aa”作为匹配项。
【问题讨论】:
-
^(\S*).*\bserverFarms\b是否提供您需要的结果? -
在the question you asked and got that code 中,多人推荐使用 Azure 模块来获取此信息,而不是解析 cli 输出。为什么不用模块?
-
@TheMadTechnician 因为这段代码......最终将需要在 docker 容器中运行,我可能无法选择安装 azure 模块......我知道它会有 cli......
-
我找到了我需要的正则表达式。 '^([\w+.-]*).*\bserverFarms\b'
-
az有一个--output json选项,使用ConvertFrom-Json更容易解析 - 不需要正则表达式。从那里,您可以通过where-object走向胜利...
标签: regex powershell