LoginSignup
2
2

More than 5 years have passed since last update.

貼って使う Azure CLI スニペット(リソースグループ編)

Last updated at Posted at 2019-02-12

概要

  • Azure リソースのメンテナンス等で使えそうなコマンドのスニペット(PowerShell ベース)をまとめました
  • PowerShell コンソールに貼って使えます
  • 今回は リソースグループ、および汎用リソース関連 に関するスニペットをまとめます

前提

  • Azure CLI がインストールされている前提です
  • 実行するには、事前に az login コマンドで 認証する必要があります(参考:Azure CLI の概要 - サインイン
  • 環境情報:
    • Azure CLI(v.2.0.57)
    • PowerShell(v.5.1.17134.407)
  • 安全に試せるよう 情報取得系 中心にまとめます(一部注意事項あります)
  • 一部のスニペットは実行時にパラメータが必要となります

リソースグループ一覧を取得したい

az group list

リソースグループ名だけ一覧取得したい

(az group list | ConvertFrom-Json) | % {$_.name}

リソースグループ名で検索し、名称とロケーションを一覧取得したい

Param(
    [Parameter(Mandatory=$true)] $keyword
); `
(az group list | ConvertFrom-Json) | where {$_.name -like "*$keyword*"} | % { $_.name + " @ " + $_.location}

リソースグループが存在するか確認したい

  • 存在する場合は true が、存在しない場合は false が出力される
Param(
    [Parameter(Mandatory=$true)] $resourceGroup
); `
az group exists -g $resourceGroup

総リソース数を知りたい

  • 特定リソースグループの総リソース数を取得
Param(
    [Parameter(Mandatory=$true)] $resourceGroup
); `
@((az resource list -g $resourceGroup | ConvertFrom-Json)).Count
  • サブスクリプション全体の総リソース数を取得
@((az resource list | ConvertFrom-Json)).Count

デプロイ進行中のリソースがいくつあるか知りたい

Param(
    [Parameter(Mandatory=$true)] $resourceGroup
); `
@((az group deployment list -g $resourceGroup | ConvertFrom-Json) | where {$_.properties.provisioningState -eq "Running"}).Count

リソースグループにデプロイされている全リソースの ARMテンプレート を出力したい

  • 【注意】<リソースグループ名>-template.json ファイルが作成されます
Param(
    [Parameter(Mandatory=$true)] $resourceGroup
); `
az group export -g $resourceGroup > .\$resourceGroup-template.json
  • 上記で取得したテンプレートはそのままの状態で正常動作しない可能性あり(適宜修正が必要となるケースあり)

ARMテンプレートをデプロイしたい

  • 【注意!】認証済みのAzureサブスクリプションに対し、リソースが作成されます!
Param(
    [Parameter(Mandatory=$true)] $resourceGroup,
    [Parameter(Mandatory=$true)] $templateFile
); `
az group deployment create -g $resourceGroup --template-file $templateFile
  • 上記実行すると必要なパラメータを尋ねられるので入力していく
  • 上記は テンプレートファイルが正しく作成されて いなければならない
    • 単に az group export でエクスポートしただけのJSONファイルでは、正常に動作しない可能性あり
2
2
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
2
2