【发布时间】:2021-08-21 01:17:17
【问题描述】:
我有一个我正在尝试编写的 powershell 脚本。我需要从控制台获取一些输入/输出并将其传送到拆分命令中,但我不知道如何。
我正在运行一个 azure cli 命令...列出一堆资源。我需要提取存储帐户的名称。 这是一些示例输出:
Name ResourceGroup Location Type
------------------ ------------- ------------ ----------
asdf1234-insights jjResourceGrp eastus microsoft.insights/components
asdf1234-storage jjResourceGrp eastus Microsoft.Storage/storageAccounts
asdf1234 jjResourceGrp eastus Microsoft.Web/serverFarms
asdf1234 jjResourceGrp eastus Microsoft.Web/sites
这是我现在用来查找存储帐户的 powershell 命令:
az resource list -g jjResourceGrp -o table | Select-String -Pattern "storageAccounts"
但我真正需要的是从该行中提取“asdf1234-storage”。 任何帮助将不胜感激。
【问题讨论】:
-
有什么理由不使用相应的 Az PowerShell cmdlet?
-
天哪,我很高兴能帮助进行文本解析!要安装 Azure 模块,您可以执行
Find-Module -Name 'Az' | Install-Module -
@Ash 为什么你这样做我们都想看到一些
regex在行动:( -
编辑我上面的评论为时已晚,但您需要在 Az cmdlet 中使用 here is the function。如果您不能使用 Az cmdlet,那么为什么不将您的输出保留为默认值,即 json,然后使用
ConvertFrom-Json将您的输出转换为可导航对象? [咧嘴一笑] @TeamRegex -
@SantiagoSquarzon 这里不需要正则表达式。
(az resource list -g jjResourceGrp -o table) -replace '\s+',','|convertfrom-csv|?{$_.type -match 'storageaccounts'}|% Name没问题
标签: powershell azure-cli