4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Runbookで毎日20時にVMを停止

4
Last updated at Posted at 2026-04-22

Azure Automation × Runbook によるVM自動停止

自分用にまとめたメモです。(画像無しです)
仕様として、AutoShutdownタグがfalseの場合は停止しません。

1. 前提条件

  • Azure Automation Account が作成済みであること
  • 対象VMが存在していること
  • Automation Account に VM 操作権限(Contributor など)が付与されていること

2. Runbookの作成

2.1 Runbook作成

  1. Azure Portal → Automation Accounts を開く
  2. 対象Automation Accountを選択
  3. 「Runbooks」→「Create a runbook」

設定:

  • Name:Stop-VM-Runbook
  • Runbook type:PowerShell 7.2
  • Description:Stop VM daily schedule

2.2 Runbookスクリプト

Connect-AzAccount -Identity
Set-AzContext -SubscriptionId (Get-AzSubscription | Select-Object -First 1).Id
# 意図しないサブスクリプションに切り替わらないために、SubscriptionIdはパラメータ化が推奨

# ===== VM =====
Get-AzVM | ForEach-Object {
    $tag = $_.Tags["AutoShutdown"]

    if ($tag -ne "false") {
        Stop-AzVM -Name $_.Name `
                  -ResourceGroupName $_.ResourceGroupName `
                  -Force
    }
}

※(再掲)仕様として、AutoShutdownタグがfalseの場合は停止しません

2.3 Runbookの保存・公開

  1. Runbookエディタで「保存」をクリック
  2. 「公開」をクリック

公開されていないRunbookはスケジュールに紐づけできない

3. スケジュール作成

3.1 スケジュール設定

  1. Azure Portal → Automation Account を開く
  2. 「Schedules」→「Add a schedule」

設定:

  • Name:Stop-VM-Daily-20
  • Recurrence:Recurring
  • Frequency:Daily
  • Time:20:00
  • Time zone:Japan Standard Time

4. Runbookとスケジュールの紐付け

4.1 Link to schedule

  1. Runbook(Stop-VM-Runbook)を開く
  2. 「Link to schedule」をクリック
  3. 作成したスケジュールを選択

4.2 パラメータ設定

今回はなし(空欄)

5. 動作確認

5.1 手動テスト

Runbook → 「Start」
→ VMが停止するか確認

5.2 スケジュール確認

次回実行時間が20:00になっているか確認

6. 権限設定(重要)

Automation Account または Managed Identity に以下を付与:

  • Virtual Machine Contributor(推奨)
  • または Contributor

注意事項

本ブログに掲載している内容は、私個人の見解であり、
所属する組織の立場や戦略、意見を代表するものではありません。​
あくまでエンジニアとしての経験や考えを発信していますので、ご了承ください。

4
0
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
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?