Azureの仮想マシン作成時に好きなPowerShellスクリプトを呼び出せるようになっていた

仮想マシン作成時に各種設定用のPowerShellスクリプトを流したくて、
少し前に四苦八苦して仮想マシン生成後にリモートでPowerShellスクリプトを流せるようになりました。

Azure上の仮想マシンにPowerShellでリモートコマンドを実行する - YOMON8.NET
 
 

ところが、少し経ってAzureで仮想マシンを作成してみたら、標準機能で、仮想マシン作成後に流したい好きなPowerShell(カスタムスクリプトと呼ばれているようです)を登録できる機能が追加されていました。

しかもBLOBに置いたPowerShellファイルも指定できるみたいです。

Azureポータルでは無く、PowerShellからの仮想マシン作成で同じことしたい場合は、Set-AzureVMCustomScriptExtensionのコマンドレットが利用できます。

名前
Set-AzureVMCustomScriptExtension

概要
Set Azure VM Custom Script Extension Information.


構文
Set-AzureVMCustomScriptExtension -Disable -VM [-ReferenceName ] [-Version ] []

Set-AzureVMCustomScriptExtension -VM [-Argument ] [-ContainerName ] [-FileName >] [-ReferenceName ] [-Run ] [-StorageAccountKey ] [-StorageAccountName ] [-St
orageEndpointSuffix ] [-Version ] []

Set-AzureVMCustomScriptExtension -VM [-Argument ] [-FileUri >] [-ReferenceName ] [-Run ] [-Version ] []


説明
Set Azure VM Custom Script Extension Information.

パラメーター
-Disable []
To Set the Extension State to 'Disable'.

-ReferenceName
The extension's reference name.

-StorageAccountName
The storage account name in the current subscription.

-ContainerName
The container name within the storage account.

-FileName >
The names of the blob files in the specified container.

-FileUri >
The URLs of the blob files.

-Run
The command to run by the extension on the VM, currently only "powershell.exe" is supported.

-Argument
Argument for the command to run on the VM.

-StorageAccountKey
The storage account key

-StorageEndpointSuffix
The storage service endpoint.

-Version
The Extension Version.

-VM
The persistent VM object.


このコマンドレットは、次の共通パラメーターをサポートします: Verbose、
Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、
OutBuffer, PipelineVariable、および OutVariable。詳細については、
about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216)
を参照してください。

-------------------------- EXAMPLE 1 --------------------------

C:\PS>$x = Set-AzureVMCustomScriptExtension -VM $x -ContainerName 'test' -FileName 'script1.ps1','script2.ps1' -Run
'script1.ps1' -Argument 'arg1 arg2';

New-AzureVM -Location 'West US' -ServiceName $svc -VM $x;

Description

-----------





-------------------------- EXAMPLE 2 --------------------------

C:\PS>Set-AzureVMCustomScriptExtension -VM $vm -FileUri 'http://foo1.blob.core.windows.net/bar/script1.ps1','http:/
/foo2.blob.core.windows.net/baz/script2.ps1' -Run 'script1.ps1' -Argument 'arg1 arg2';
Update-AzureVM -ServiceName $svc -Name $name -VM vm;

Description

-----------


これで自動プロビジョニングの可能性もぐんと広がりそうです。