normalian blog

Let's talk about Microsoft Azure, ASP.NET and Java!

Microsoft Azure PowerShell で Windows Server の最新 ImangeName を取得する方法

今回は簡単に備忘録だけ記載するが、もっといい方法があれば突っ込みを期待する。Microsoft Azure の IaaS を利用する際、作成する仮想マシンインスタンスのイメージ名を選択するため、Get-AzureVMImage のコマンドを叩く人がいるだろう。

PS C:\temp> Get-AzureVMImage
ImageName            : 03f55de797f546a1b29d1b8d66be687a__Team-Foundation-Server-2013-Update4-WS2012R2
OS                   : Windows
MediaLink            : 
LogicalSizeInGB      : 128
AffinityGroup        : 
Category             : Public
Location             : East Asia;Southeast Asia;Australia East;Australia Southeast;Brazil South;North Europe;West Europe;Japan East;Japan West;Central US;East US;East US 2;North Central US;South Central US;West US
Label                : Team Foundation Server 2013 Update 4 on Windows Server 2012 R2
Description          : Microsoft Team Foundation Server 2013 Trial on Windows Server 2012 R2 Update. Virtual Machines created with this trial image will require a product key for Team Foundation Server (such as from an 
                       MSDN Subscription). This image includes a complete installation of Team Foundation Server 2013 Update 4. Some components require additional setup and configuration. You can configure SQL Server us
                       ing SQL Server Express included in this image, by downloading and installing SQL Server Standard edition (from an MSDN Subscription), or by connect to a pre-existing SQL Server. Minimum virtual ma
                       chine size for this image is Medium. For more details on TFS server setup please see the [Team Foundation Server install guide|http://msdn.microsoft.com/en-us/library/dd631902.aspx].
Eula                 : http://www.microsoft.com/en-us/download/details.aspx?id=13350
ImageFamily          : Team Foundation Server 2013 Update 4 on Windows Server 2012 R2
PublishedDate        : 2014/11/12 17:00:00
IsPremium            : False
IconUri              : VisualStudio2013_45.png
SmallIconUri         : VisualStudio2013_45.png
PrivacyUri           : http://go.microsoft.com/fwlink/?LinkID=286720
RecommendedVMSize    : Medium
PublisherName        : Microsoft Visual Studio Group
IOType               : Standard_LRS
OperationDescription : Get-AzureVMImage
OperationId          : e2084b70-8858-3951-89f3-dfe2b1334a11
OperationStatus      : Succeeded

ImageName            : 03f55de797f546a1b29d1b8d66be687a__Visual-Studio-2013-Community-12.0.31101.0-AzureSDK-2.5-WS2012R2
OS                   : Windows
MediaLink            : 
LogicalSizeInGB      : 128
AffinityGroup        : 
Category             : Public
Location             : East Asia;Southeast Asia;Australia East;Australia Southeast;Brazil South;North Europe;West Europe;Japan East;Japan West;Central US;East US;East US 2;North Central US;South Central US;West US
Label                : Visual Studio Community 2013 on Windows Server 2012 R2
Description          : The Visual Studio Community 2013 image enables you to unleash the full power of Visual Studio to develop cross-platform solutions. Create apps in one unified IDE, and incorporate new languages, fe
                       atures, and development tools into them with Visual Studio Extensions (available in the Visual Studio Gallery).
Eula                 : http://go.microsoft.com/fwlink/?LinkId=430755
ImageFamily          : Visual Studio Community 2013 on Windows Server 2012 R2
PublishedDate        : 2014/11/12 17:00:00
IsPremium            : False
IconUri              : VisualStudio2013_45.png
SmallIconUri         : VisualStudio2013_45.png



<中略>

だが、Get-AzureVMImageの実行結果は上記の様に膨大な出力結果になる。知っている人なら何の問題もないと思うが、今回は最新の Windows Server 2012 R2(他のイメージでも良いが)のイメージ名を取得するための方法を記載する。

結論から言うと大体こんな感じ。イメージ一覧を取ってきて、イメージ名でフィルタして、更新日付でソートして最初のイメージを持ってくる。

PS C:\temp> Get-AzureVMImage | Where-Object {$_.label -like “Windows Server 2012 R2*”} | Sort-Object -Descending PublishedDate | Select-Object -Index 0

ImageName            : a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-R2-201411.01-en.us-127GB.vhd
OS                   : Windows
MediaLink            : 
LogicalSizeInGB      : 128
AffinityGroup        : 
Category             : Public
Location             : East Asia;Southeast Asia;Australia East;Australia Southeast;Brazil South;North Europe;West Europe;Japan East;Japan West;Central US;East US;East US 2;North Central US;Sou
                       th Central US;West US
Label                : Windows Server 2012 R2 Datacenter, November 2014
Description          : At the heart of the Microsoft Cloud OS vision, Windows Server 2012 R2 brings Microsoft's experience delivering global-scale cloud services into your infrastructure. It o
                       ffers enterprise-class performance, flexibility for your applications and excellent economics for your datacenter and hybrid cloud environment. This image includes Windo
                       ws Server 2012 R2 Update.
Eula                 : 
ImageFamily          : Windows Server 2012 R2 Datacenter
PublishedDate        : 2014/11/14 17:00:00
IsPremium            : False
IconUri              : WindowsServer2012R2_45.png
SmallIconUri         : WindowsServer2012R2_45.png
PrivacyUri           : 
RecommendedVMSize    : 
PublisherName        : Microsoft Windows Server Group
IOType               : Standard_LRS
OperationDescription : Get-AzureVMImage
OperationId          : 23e313f9-87f5-3457-bc5a-ca5946801382
OperationStatus      : Succeeded

仮想マシンの作成までやってみる

上記のスクリプトを利用して仮想マシンインスタンスを作成するまでのスクリプト

# 変数全般
$cloudServiceName = 'eastasia-dmz01'
$vnetName = 'vnet01'
$imageFilterName = 'Windows Server 2012 R2*'
$instanceName = 'WebVM'
$instanceSize = 'Small'
$location = 'East Asia'
$staticIP = '10.0.0.10'
$subnetName = 'Subnet-DMZ'

# 最新版の Windows Server 2012 R2 のイメージ名を取得する
$image = Get-AzureVMImage -Verbose:$false | Where-Object {$_.label -like $imageFilterName } | Sort-Object -Descending PublishedDate | Select-Object -Index 0

# 必要な場合、利用するストレージサービスを設定
# Set-AzureSubscription -CurrentStorageAccountName "<ストレージサービス名>" -SubscriptionName "<サブスクリプション名>"

# 仮想マシン・インスタンスのプロビジョニング
$vm = New-AzureVMConfig -Name $instanceName -InstanceSize $instanceSize -ImageName $image.ImageName
Add-AzureProvisioningConfig -VM $vm -Windows -AdminUsername "<ユーザ名>" -Password "<パスワード>"

# サブネットの設定
Set-AzureSubnet -SubnetNames $subnetName -VM $vm

# 静的IPの割り振り
Set-AzureStaticVNetIP -IPAddress $staticIP -VM $vm

# 仮想マシン・インスタンスを入れるクラウドサービスを作成する
New-AzureService -ServiceName $cloudServiceName -Location $location

# 仮想マシン・インスタンスの作成
New-AzureVM -ServiceName $cloudServiceName -VNetName $vnetName -VM $vm