Ipswitch Blogs

PowerShell を使用して、Azure Automation 変数を管理

Adam Bertram | Posted on | IT技術情報

Azure Automation には、変数という便利なものがあります。変数は、様々な値を設定したりクエリしたりすることのできる Azure Automation の資産です。

変数には、文字列、ブール値、日付/時刻、整数など、様々な異なるタイプを指定できます。一般的な変数と少し違う点は、それらを暗号化することも選択できる点です。

いったん作成すると、変数は Azure Automation の Runbook や DSC 設定でアクセスできます。変数は存続し、明示的に行われない限り変更はされません。つまり、一度作成された変数は、好きなだけずっと、すべての Runbook と DSC 設定で一貫して使用できます。

Azure Automation 変数の管理

変数は、Azure Portal の Automation アカウントの下にある GUI を介して、[Variables] をクリックして管理できます。

ただし、このブログでは、PowerShell でこれらの変数を管理する方法を説明します。スクリプトでこのプロセスを自動化したり、コマンドラインを使用して変数を管理することができます。

PowerShell を使って変数を管理するには、Install-Module AzureRm を実行して AzureRM PowerShell モジュールをインストールする必要があります。このモジュールをインストールし、Connect-AzureRmAccount を使用して Azure への認証が終了したら、Automation のアカウントが既に作成されていることを確認してください。これらの前提条件が整ったら、準備は完了です。

Automation 変数の作成

まず、PowerShell で新しい Automation 変数を作成しましょう。New-AzureRmAutomationVariable コマンドが使えます。変数を作成して、使用可能などんなプロパティも割り当てることができます。

たとえば、Azure に、種々の Runbook で使用するリソースグループを作成したとします。このリソースグループの名前を多くの Runbook 内にハードコーディングする代わりに、リソースグループ変数として作成し、それを参照するようにすることができます。こうすることで、何度も同じことを繰り返す必要がなくなり、変更もより簡単に行うことができます。

下の例では、MySharedResourceGroup という変数を作成し、それに TechSnipsBackend リソースグループ内の techsnips という Automation アカウントに関連付けられた RG1 という値を割り当てます。

PS> New-AzureRmAutomationVariable -AutomationAccountName techsnips -Name 'MySharedResourceGroup' -Description 'A resource group that is used across many different runbooks' -Value 'RG1' -ResourceGroupName TechSnipsBackend -Encrypted $false

Value : RG1
Encrypted : False
ResourceGroupName : TechSnipsBackend
AutomationAccountName : techsnips
Name : MySharedResourceGroup
CreationTime : 9/28/2018 9:08:54 AM -05:00
LastModifiedTime : 9/28/2018 9:08:54 AM -05:00
Description : A resource group that is used across many different runbooks

Get-AzureRmAutomation コマンドを使用して、変数名とリソースグループの名前、Automation アカウントの名前を指定することで、この新しい変数を参照できるようになりました。

PS> Get-AzureRmAutomationVariable -Name MySharedResourceGroup -ResourceGroupName TechSnipsBackEnd -AutomationAccountName techsnips

Value : RG1
Encrypted : False
ResourceGroupName : TechSnipsBackEnd
AutomationAccountName : techsnips
Name : MySharedResourceGroup
CreationTime : 9/28/2018 9:08:54 AM -05:00
LastModifiedTime : 9/28/2018 9:08:54 AM -05:00
Description : A resource group that is used across many different runbooks

Automation 変数の変更

変数を作成したら、Set-AzureRmAutomationVariable コマンドを使用して変数の値または記述を変更できます。Set-AzureAutomationVariable コマンドはパイプライン入力が可能なので、変更したい Value パラメータまたは Description パラメータを指定して、Get-AzureRmAutomationVariable からの出力を直接パイプライン処理するのが簡単です。値と記述の両方を変更する場合は、個別に行う必要があります。

変数が作成されたときの、暗号化の属性は変更することができません。

PS> Get-AzureRmAutomationVariable -Name MySharedResourceGroup -ResourceGroupName TechSnipsBackEnd -AutomationAccountName techsnips | Set-AzureRmAutomationVariable -Value 'RG2'
PS> Get-AzureRmAutomationVariable -Name MySharedResourceGroup -ResourceGroupName TechSnipsBackEnd -AutomationAccountName techsnips | Set-AzureRmAutomationVariable -Description 'changed'

Automation 変数の削除

最後に、変更の場合と同様、変数を直接 Remove-AzureRmAutomationVariable にパイプライン処理することによって Automation 変数を削除できます。ただし、これはプロンプトされないので、削除コマンドに正しい変数を送っていることをよく確認してください。

PS> Get-AzureRmAutomationVariable -Name MySharedResourceGroup -ResourceGroupName TechSnipsBackEnd -AutomationAccountName techsnips | Remove-AzureRmAutomationVariable

まとめ

Azure Automation の変数の管理は、PowerShell を使った簡単なプロセスで実施できます。変数は Runbook で使用するのに最適な資産であり、PowerShell で変数を管理するスキルを持っていると重宝すると思います。

 

Get Started with Ipswitch

Free Trial Price Quote Live Demo

Subscribe to our mailing list

Get our latest blog posts delivered in a weekly email.

Subscribe