1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Azureのリソースを一覧で取得し、CSVファイルに出力する

Posted at

Azureのリソースを一覧で取得する

前提

  • 最新のAzureCLIがインストールされていること

方法

以下のスクリプトファイルを保存し、PowerShellから実行する(.\GetResourcesList.ps1)

GetResourcesList.ps1
<#
    .SYNOPSIS
        リソースを一覧で取得する

    .PARAMETER OutputFolderPath
        結果を出力するフォルダーの完全パス
        デフォルトは"CurrentDirectory\Outputs"

    .OUTPUTS
        ResourcesList_yyyyMMddhhmmss.csv

    .EXAMPLE
        PS> .\GetResourcesList.ps1
#>

Param(
    [Parameter(HelpMessage = '出力するフォルダのパス')]
    [String]$OutputFolderPath = (Get-Item $PSScriptRoot).FullName + '\Outputs'
)

$ErrorActionPreference = 'Stop'

# ログイン
az login

# 現在時刻
$CurrentTime = Get-Date -Format 'yyyyMMddHHmmss'

# フォルダチェック(フォルダが存在しない場合は新規作成する)
if (!(Test-Path -Path $OutputFolderPath -PathType Container)) {
    New-Item $OutputFolderPath -ItemType Directory
}

# 出力先のファイルパスを設定
$OutputFilePath = Join-Path `
    -Path $OutputFolderPath `
    -ChildPath ('ResourcesList_' + $CurrentTime + '.csv')

# 仮想マシンの一覧を取得する
$Resources = az resource list `
    --output json | ConvertFrom-Json

# CSVへ出力するためのオブジェクトを生成
$ResourcesList = New-Object System.Collections.ArrayList
$ResourceProperty = New-Object -TypeName PSObject

# CSVへ出力する情報をオブジェクトへ設定する
foreach ($resource in $Resources) {
    $ResourceProperty = @{
        'ResourceID'    = $resource.id
        'ResourceName'  = $resource.name
        'ResourceGroup' = $resource.resourceGroup
        'ResourceType'  = $resource.type
        'Location'      = $resource.location
        'Plan'          = $resource.plan
        'SKU'           = $resource.sku.name
        'ChangedTime'   = $resource.changedTime
        'CreatedTime'   = $resource.createdTime
    }
    $ResourcesList += New-Object PSObject `
        -Property $ResourceProperty
}

# CSVへ出力する
$ResourcesList `
| Select-Object `
    -Property 'ResourceID', `
    'ResourceName', `
    'ResourceGroup', `
    'ResourceType', `
    'Location', `
    'Plan', `
    'SKU', `
    'ChangedTime', `
    'CreatedTime' `
| Sort-Object `
    -Property ResourceGroup, `
    ResourceName, `
    CreatedTime `
| Export-Csv `
    -Path $OutputFilePath `
    -Encoding Default `
    -NoTypeInformation `
    -Force

概要

このコマンドで以下の情報を取得できる

  • リソースID
  • リソース名
  • リソースグループ
  • リソース種別
  • リージョン
  • プラン
  • SKU
  • 更新日時
  • 作成日
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?