简体   繁体   中英

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

I need to get the azure resource list from the azure CLI excluding 2 kinds of resource types and output the file to a TSV file. I was able do this via PowerShell from the following command.

PowerShell Command

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

I need to get this result from Azure CLI excluding the mentioned resource types to TSV file (Since we cannot get the Azure CLI output in CSV)

You need to lookup the info for Export-Csv , where you can see it has a parameter -Delimiter you use to set a different character as field separator (in your case a TAB character) instead of the default comma .

To prevent an extra line on top of the output file with field type definitions, there is also a switch -NoTypeInformation

Then looking at your code I have some remarks:

  • You use -notmatch which is a regex comparison operator, but the strings to compare against have characters that have special meaning in regex (most notably the dot)
  • You seem to not use quotes anywhere and because your strings and output path do not contain spaces, in this case that is not a problem. However, I think it is a good habit to always use single quotes around such strings and paths

Try

# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM