2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PowerShellでAzure 設定と操作例

Posted at

はじめに

PowerShellからAzureを操作するための設定を行う覚書です。

PowerShellのインストール

下記のサイトを参考にインストールを行います。

Azモジュールのインストール

インストールしたPowerShellを管理者で実行して下記のコマンドを実行します。

Azモジュールのインストール
Install-Module -Name Az -AllowClobber

実行後に対話に答えていくとモジュールがインストールされます。

Azureへの接続

PowerShellからAzureへの接続を構成するために下記のコマンドを実行します。

Azureへの接続
Connect-AzAccount -DeviceCode

実行するとURLとパスコードが表示されるので、ブラウザからアクセスしてパスコードを入力します。

WARNING: To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code AAAAAAAA to authenticate.

その後ブラウザからユーザー名やパスワードを入力してログインするとPowerShellからAzureにアクセスできるようになります。
例えば下記のようなコマンドでサブスクリプションを表示することができます。

サブスクリプションの一覧を表示
Get-AzSubscription

Azureからのログアウト

PowerShellからAzureへの接続を切断するために下記のコマンドを実行します。

Azureからのログアウト
Disconnect-AzAccount

PowerShellでできること

PowerShellで試しにVMを作成して、消すまでのサンプルスクリプトです。前提としてログインしているユーザーアカウントが権限を持っている必要はあります。

PowerShellでできること
# 東日本リージョンに新しいリソースグループを作成
New-AzResourceGroup -Name rg-test -Location japaneast
# 上記リソースグループ内に新しいVMを作成(VMのユーザー名とパスワードを求められます)
New-AzVM -ResourceGroupName rg-test -Name vmtest01 -SubnetName sn-test
# 作成したVMの状態を確認
Get-AzVM -Name vmtest01 -status
# 作成したリソースグループごと全て消去
Remove-AzResourceGroup -Name rg-test

おわりに

PowerShellはとっつきにくい印象を持っていましたが、意外と簡単にAzureの操作が自動化できそうです。これらのスクリプトはAzure Portalのクラウドシェルからも実行できるので、コピペするだけでお手軽に再現性の高い環境構築ができるので便利に感じました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?