簡體   English   中英

獲取 TSV 文件 output 從 Azure CLI 中的 az 資源列表中排除 2 種資源類型

[英]Getting TSV file output excluding 2 resource types from the az resource list in Azure CLI

我需要從 azure CLI 中獲取 azure 資源列表,不包括 2 種資源類型和 output 文件到 TSV 文件。 我能夠通過以下命令通過 PowerShell 執行此操作。

PowerShell 命令

get-azresource | where ResourceType -notmatch microsoft.network/privatednszones/virtualnetworklinks | where ResourceType -notmatch microsoft.insights/actiongroups | export-csv C:\Users\7.csv

我需要從 Azure CLI 中獲取此結果,將提到的資源類型排除到 TSV 文件中(因為我們無法在 CSV 中獲取 Azure CLI output)

您需要查找Export-Csv的信息,您可以在其中看到它有一個參數-Delimiter用於將不同的字符設置為字段分隔符(在您的情況下為 TAB 字符)而不是默認的逗號

為了防止在帶有字段類型定義的 output 文件頂部多出一行,還有一個開關-NoTypeInformation

然后查看您的代碼我有一些評論:

  • 您使用-notmatch這是一個正則表達式比較運算符,但要比較的字符串具有在正則表達式中具有特殊含義的字符(最顯着的是點)
  • 您似乎沒有在任何地方使用引號,因為您的字符串和 output 路徑不包含空格,在這種情況下這不是問題。 但是,我認為始終在此類字符串和路徑周圍使用單引號是一個好習慣

嘗試

# the resource types to exclude
$exclude = 'microsoft.network/privatednszones/virtualnetworklinks', 'microsoft.insights/actiongroups'
# create a regex string from the $exclude array, where the elements are joined with the regex OR ('|')
$notThese = ($exclude | ForEach-Object { [regex]::Escape($_) }) -join '|'

Get-AzResource | Where-Object {$_.ResourceType -notmatch $notThese} | 
Export-Csv -Path 'C:\Users\7.tsv' -Delimiter "`t" -NoTypeInformation

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM