LoginSignup
1
2

More than 5 years have passed since last update.

貼って使う Azure CLI スニペット(Storage編)

Last updated at Posted at 2019-02-12

概要

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

前提

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

ストレージアカウント の一覧を取得したい

az storage account list

ストレージアカウントの 名称、所属リソースグループ を一覧取得したい

(az storage account list | ConvertFrom-Json) | % {$_.name + " (" + $_.resourceGroup + ")"}

汎用V2でないストレージアカウントを一覧取得したい

(az storage account list | ConvertFrom-Json) | where {$_.kind -ne "StorageV2"} | % {$_.name + " (" + $_.resourceGroup + ")"}

特定のストレージアカウントのアクセスに必要な情報をまとめて取得したい

Param(
    [Parameter(Mandatory=$true)] $storageAccount
); `
$keys = (az storage account keys list -n $storageAccount | ConvertFrom-Json) | % { @{name=$_.keyName; val=$_.value} }; `
$eps = (az storage account show -n $storageAccount | ConvertFrom-Json) | % {$_.primaryEndpoints}; `
echo "✓ エンドポイントURL:"; `
echo ("  Blob: " + $eps.blob); `
echo ("  File: " + $eps.file); `
echo ("  Queue: " + $eps.queue); `
echo ("  Table: " + $eps.table); `
foreach ($key in $eps.Keys) { echo ($key + ": " + $eps[$key]) }; `
echo "✓ アクセス用のキー:"; `
$keys | % { echo ("  " + $_.name + ": " + $_.val) };

特定ストレージアカウントの Blob Storage に コンテナが存在するかどうか確認したい

  • 前処理でアクセスキーも取得している
  • 存在すれば True, しなければ False が表示される
Param(
    [Parameter(Mandatory=$true)] $storageAccount,
    [Parameter(Mandatory=$true)] $containerName
); `
$key = (az storage account keys list -n $storageAccount | ConvertFrom-Json)[0].value; `
(az storage container exists -n $containerName --account-name $storageAccount --account-key $key| ConvertFrom-Json).exists;

特定ストレージアカウントの Blob Storage コンテナ名 を一覧取得したい

  • 前処理でアクセスキーも取得している
Param(
    [Parameter(Mandatory=$true)] $storageAccount
); `
$key = (az storage account keys list -n $storageAccount | ConvertFrom-Json)[0].value; `
(az storage container list --account-name $storageAccount --account-key $key | ConvertFrom-Json) | % { $_.name };

特定ストレージアカウントの Table Storage テーブル名 を一覧取得したい

  • 前処理でアクセスキーも取得している
Param(
    [Parameter(Mandatory=$true)] $storageAccount
); `
$key = (az storage account keys list -n $storageAccount | ConvertFrom-Json)[0].value; `
(az storage table list --account-name $storageAccount --account-key $key | ConvertFrom-Json) | % { $_.name };

特定ストレージアカウントの File Storage ファイル共有名 を一覧取得したい

  • 前処理でアクセスキーも取得している
Param(
    [Parameter(Mandatory=$true)] $storageAccount
); `
$key = (az storage account keys list -n $storageAccount | ConvertFrom-Json)[0].value; `
(az storage share list --account-name $storageAccount --account-key $key | ConvertFrom-Json) | % { $_.name };

特定ストレージアカウントの Queue Storage キュー名 を一覧取得したい

  • 前処理でアクセスキーも取得している
Param(
    [Parameter(Mandatory=$true)] $storageAccount
); `
$key = (az storage account keys list -n $storageAccount | ConvertFrom-Json)[0].value; `
(az storage queue list --account-name $storageAccount --account-key $key | ConvertFrom-Json) | % { $_.name };

キューを1つピーク(削除せず参照)し、コンテンツを取得する

  • 前処理でアクセスキーも取得している

  • キューが Base64 エンコードされていない場合

Param(
    [Parameter(Mandatory=$true)] $storageAccount,
    [Parameter(Mandatory=$true)] $queueName
); `
$key = (az storage account keys list -n $storageAccount | ConvertFrom-Json)[0].value; `
(az storage message peek -q $queueName --account-name $storageAccount --account-key $key | ConvertFrom-Json) | % { $_.content };
  • キューが Base64 エンコードされている場合は以下で取得
Param(
    [Parameter(Mandatory=$true)] $storageAccount,
    [Parameter(Mandatory=$true)] $queueName
); `
$key = (az storage account keys list -n $storageAccount | ConvertFrom-Json)[0].value; `
(az storage message peek -q $queueName --account-name $storageAccount --account-key $key | ConvertFrom-Json) | `
    %{ [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($_.content)) };
1
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
1
2