generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
devsecops/emvaldes/export-azure-resources (#17184)
* Shell/Bash, PowerShell and Python scripts to export Azure Resources into CSV format
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env pwsh | ||
|
||
# Define parameters | ||
param ( | ||
[string]$OutputFile = "azure-resources--powershell.csv" | ||
) | ||
|
||
# Output header | ||
"Location,Name,Resource Group" | Out-File -FilePath "azure-resources.csv" -Encoding utf8 ; | ||
|
||
# Fetch Azure resources and append to CSV | ||
az resource list --query '[].{"Location":location,"Name":name,"Resource Group":resourceGroup}' --output tsv | | ||
ForEach-Object { $_ -replace "`t", "," } | | ||
Out-File -FilePath $OutputFile -Append -Encoding utf8 ; | ||
|
||
# Display the contents of the generated CSV | ||
Get-Content -Path $OutputFile ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env python | ||
|
||
import subprocess | ||
import csv | ||
import argparse | ||
|
||
# Set up argument parser | ||
parser = argparse.ArgumentParser( description="Fetch Azure resources and export them to a CSV file." ) ; | ||
parser.add_argument( | ||
"--output-file", | ||
type=str, | ||
default="azure-resources--python.csv", | ||
help="Path to the output CSV file (default: azure-resources--python.csv)" | ||
) ; | ||
|
||
# Parse arguments | ||
args = parser.parse_args() ; | ||
output_file = args.output_file ; | ||
|
||
# Command to fetch Azure resources | ||
cmd = [ | ||
"az", "resource", "list", | ||
"--query", "[].{\"Location\":location, \"Name\":name, \"Resource Group\":resourceGroup}", | ||
"--output", "tsv" | ||
] ; | ||
|
||
# Run the Azure CLI command | ||
result = subprocess.run( cmd, stdout=subprocess.PIPE, text=True ) ; | ||
|
||
# Write header and data to CSV file | ||
with open( output_file, "w", newline="" ) as csvfile: | ||
writer = csv.writer( csvfile ) ; | ||
writer.writerow( ["Location", "Name", "Resource Group"] ) ; | ||
for line in result.stdout.splitlines(): | ||
writer.writerow( line.split( "\t" ) ) ; | ||
|
||
# Print the contents of the generated CSV | ||
with open( output_file, "r" ) as csvfile: | ||
print( csvfile.read() ) ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env bash | ||
|
||
if [[ ${#1} -gt 0 ]]; then | ||
OutputFile="${1}" ; | ||
else OutputFile="azure-resources--shell.csv" ; | ||
fi ; | ||
|
||
echo "Location,Name,Resource Group" \ | ||
| cat - <( az resource list --query "[].{\"Location\":location, \"Name\":name, \"Resource Group\":resourceGroup}" --output tsv \ | ||
| sed 's/\t/,/g' ) \ | ||
> ${OutputFile} ; | ||
|
||
cat ${OutputFile} ; |