Created
July 17, 2015 23:43
-
-
Save ebibibi/9a0a6830b54f0d3a7400 to your computer and use it in GitHub Desktop.
Azure上にAzure Resource Managerを使ってWindows Server複数台を展開(IISの役割は未追加)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#---------------------------------------------------------- | |
#設定 | |
#---------------------------------------------------------- | |
#スクリプトパス | |
$scriptPath = "" | |
#サブスクリプション名 | |
$subscriptionName = "" | |
#リージョン | |
$location = "japaneast" | |
#デプロイで使用するプレフィックス | |
$prefix = "armdemo" | |
#タグの設定 | |
$tag_project = "demo" | |
$tag_costCenter = "0001" | |
#リソースグループに展開する仮想マシン数 | |
$vmInstances = 2 | |
#仮想マシンの管理者 | |
$adminUserName = "newadmin" | |
$adminPassword = "AzurenoP@ssw0rd" | |
#VMイメージ | |
$publiserName = "MicrosoftWindowsServer" | |
$offerName = "WindowsServer" | |
$skuName = "2012-R2-Datacenter" | |
$version = "latest" | |
#ストレージアカウントタイプ | |
#$(Get-Command -Name New-AzureStorageAccount).Parameters["Type"].Attributes.ValidValues | Out-GridView | |
$storageAccountType = "Standard_LRS" | |
#VMインスタンスサイズ | |
#Get-AzureVMSize -Location $location | Select-Object Name, NumberOfCores, MemoryInMB, MaxDataDiskCount | Out-GridView | |
$vmSize = "Standard_A2" | |
#---------------------------------------------------------- | |
#クレデンシャル | |
$adminPassword = $adminPassword | ConvertTo-SecureString -AsPlainText -Force | |
$vmAdminCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminUserName, $adminPassword | |
#Azureアカウント認証 | |
$azureAccount = Get-AzureAccount -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | |
if ($azureAccount -eq $null) { | |
Write-Output "Azureアカウントの認証を行います。" | |
Add-AzureAccount | |
} | |
#Azure Resorce Managerモードに切り替えます。 | |
Write-Output "Azure Resorce Managerモードに切り替えます。" | |
Switch-AzureMode -Name AzureResourceManager | |
#最新のARMプロバイダーを登録します。 | |
Write-Output "最新のARMプロバイダーを登録します。" | |
Register-AzureProvider -ProviderNamespace Microsoft.Compute -Force | |
Register-AzureProvider -ProviderNamespace Microsoft.Storage -Force | |
Register-AzureProvider -ProviderNamespace Microsoft.Network -Force | |
#Get-AzureProvider | Select-Object -Property ProviderNamespace -ExcludeProperty ResouceTypes | |
#Azureサブスクリプションを選択します。 | |
Write-Output "Azureサブスクリプションを選択します。" | |
$subscriptionId = (Get-AzureSubscription -Name $subscriptionName).SubscriptionId | |
Select-AzureSubscription -SubscriptionId $subscriptionId | |
#デプロイで使用するタグを定義します。 | |
Write-Output "デプロイで使用するタグを定義します。" | |
Write-Output "project : ${tag_project}" | |
Write-Output "costCenter : ${tag_costCenter}" | |
$tags = New-Object System.Collections.ArrayList | |
$tags.Add( @{ Name = "project"; Value = $tag_project } ) | |
$tags.Add( @{ Name = "costCenter"; Value = $tag_costCenter } ) | |
#リソースグループ作成 | |
Write-Output "リソースグループを作成します。既に存在している場合にはそれを利用します。" | |
$rgName = "${prefix}-rg" | |
Write-Output "リソースグループ名 : $rgName" | |
If (!(Test-AzureResourceGroup -ResourceGroupName $rgName)) { | |
Write-Output "新規に作成します。" | |
$rg = New-AzureResourceGroup -Name $rgName -Location $location -Tag $tags | |
} Else { | |
$rg = Get-AzureResourceGroup -Name $rgName | |
} | |
#ストレージアカウント名 | |
$storageAccountName = "${prefix}stor01" | |
Write-Output "ストレージアカウント名 : $storageAccountName" | |
#ストレージアカウント作成 | |
Write-Output "ストレージアカウントを作成します。存在していれば既存のものを利用します。" | |
if (!(Test-AzureResource -ResourceName $storageAccountName -ResourceType "Microsoft.Storage/storageAccounts" -ResourceGroupName $rgName)) { | |
Write-Output "新規に作成します。" | |
$storageAccount = New-AzureStorageAccount ` | |
-Name $storageAccountName ` | |
-ResourceGroupName $rgName ` | |
-Location $location ` | |
-Type $storageAccountType | |
} else { | |
Write-Output "既存のものを利用します。" | |
$storageAccount = Get-AzureStorageAccount ` | |
-ResourceGroupName $rgName ` | |
-Name $storageAccountName | |
} | |
#ストレージアカウントを規定に設定 | |
Write-Output "ストレージアカウントを規定に設定します。" | |
Set-AzureSubscription -SubscriptionId $subscriptionId -CurrentStorageAccountName $storageAccountName | |
#Azure仮想ネットワーク | |
$vnetName = "${prefix}-vnet" | |
Write-Output "Azure仮想ネットワーク名 : $vnetName" | |
#仮想ネットワーク内のサブネット名 | |
$subnet1Name = "${prefix}-subnet01" | |
$subnet2Name = "GatewaySubnet" | |
Write-Output "サブネット1 : $subnet1Name" | |
Write-Output "サブネット2 : $subnet2Name" | |
#仮想ネットワーク作成 | |
Write-Output "仮想ネットワークを作成します。存在していれば既存のものを利用します。" | |
if (!(Test-AzureResource -ResourceGroupName $rgName -ResourceName $vnetName -ResourceType "Microsoft.Network/virtualNetworks" )) { | |
Write-Output "新規に仮想ネットワークを作成します。" | |
$subnet1 = New-AzureVirtualNetworkSubnetConfig -Name $subnet1Name -AddressPrefix "10.0.1.0/24" | |
$subnet2 = New-AzureVirtualNetworkSubnetConfig -Name $subnet2Name -AddressPrefix "10.0.2.0/28" | |
$vnet = New-AzureVirtualNetwork ` | |
-Name $vnetName ` | |
-ResourceGroupName $rgName ` | |
-Location $location ` | |
-AddressPrefix "10.0.0.0/16" ` | |
-Subnet $subnet1,$subnet2 ` | |
-Tag $tags | |
} else { | |
Write-Output "既存の仮想ネットワークを利用します。" | |
$vnet = Get-AzureVirtualNetwork -Name $vnetName -ResourceGroupName $rgName | |
} | |
#VIPリソース | |
$publicVipName = "${prefix}-vip" | |
Write-Output "VIP名 : $publicVipName" | |
#VIPに紐付けるDNS名 | |
$domainName = "${prefix}app" | |
Write-Output "VIPに紐付けるDNS名 : $domainName" | |
#VIP作成 | |
Write-Output "VIPを作成します。存在していれば既存のものを利用します。" | |
if (!(Test-AzureResource -ResourceName $publicVipName -ResourceType "Microsoft.Network/publicIPAddresses" -ResourceGroupName $rgName)) { | |
Write-Output "新規に作成します。" | |
$publicVip = New-AzurePublicIpAddress ` | |
-Name $publicVipName ` | |
-ResourceGroupName $rgName ` | |
-Location $location ` | |
-AllocationMethod Dynamic ` | |
-DomainNameLabel $domainName ` | |
-Tag $tags | |
} else { | |
Write-Output "既存のものを利用します。" | |
$publicVip = Get-AzurePublicIpAddress ` | |
-Name $publicVipName ` | |
-ResourceGroupName $rgName | |
} | |
#ロードバランサー名 | |
$lbName = "${prefix}-lb" | |
Write-Output "ロードバランサー名 : $lbName" | |
#ロードバランサー作成 | |
Write-Output "ロードバランサーを作成します。存在していれば既存のものを利用します。" | |
if (!(Test-AzureResource -ResourceName $lbName -ResourceType "Microsoft.Network/loadBalancers" -ResourceGroupName $rgName)) { | |
Write-Output "新規に作成します。" | |
#Public VIPを利用したFront-end IPの構成 | |
$lbFeIpConfigName = "lb-feip" | |
$lbFeIpConfig = New-AzureLoadBalancerFrontendIpConfig -Name $lbFeIpConfigName -PublicIpAddress $publicVip | |
#VM毎のRDPのためのインバウンドNATルール | |
$lbInboundNatRules = @() | |
for ($count = 1; $count -le $vmInstances; $count++) { | |
$ruleName = "nat-rdp-${count}" | |
$frontEndPort = 3389 + $count | |
$backEndPort = 3389 | |
$lbInboundNatRules += New-AzureLoadBalancerInboundNatRuleConfig ` | |
-Name $ruleName ` | |
-FrontendIpConfigurationId $lbFeIpConfig.Id ` | |
-Protocol Tcp ` | |
-FrontendPort $frontEndPort ` | |
-BackendPort $backEndPort | |
} | |
#バックエンドのIPアドレスプール | |
$lbBeIpPoolName = "lb-be-ip-pool" | |
$lbBeIpPool = New-AzureLoadBalancerBackendAddressPoolConfig -Name $lbBeIpPoolName | |
#HTTPのためのヘルスチェックプローブ | |
$lbProbeName = "lb-probe" | |
$lbProbe = New-AzureLoadBalancerProbeConfig ` | |
-Name $lbProbeName ` | |
-RequestPath "/" ` | |
-Protocol Http ` | |
-Port 80 ` | |
-IntervalInSeconds 15 ` | |
-ProbeCount 2 | |
#HTTPのためのロードバランシングルール | |
$lbRuleName = "lb-http" | |
$lbRule = New-AzureLoadBalancerRuleConfig ` | |
-Name $lbRuleName ` | |
-FrontendIpConfigurationId $lbFeIpConfig.id ` | |
-BackendAddressPoolId $lbBeIpPool.id ` | |
-ProbeId $lbProbe.id ` | |
-Protocol Tcp ` | |
-FrontendPort 80 ` | |
-BackendPort 80 ` | |
-LoadDistribution Default | |
#ロードバランサー作成 | |
$lb = New-AzureLoadBalancer ` | |
-Name $lbName ` | |
-ResourceGroupName $rgName ` | |
-Location $location ` | |
-FrontendIpConfiguration $lbFeIpConfig ` | |
-BackendAddressPool $lbBeIpPool ` | |
-Probe $lbProbe ` | |
-InboundNatRule $lbInboundNatRules ` | |
-LoadBalancingRule $lbRule | |
} else { | |
Write-Output "既存のものを利用します。" | |
$lb = Get-AzureLoadBalancer -Name $lbName -ResourceGroupName $rgName | |
} | |
#ネットワークセキュリティグループ | |
$nsgName = "${prefix}-nsg" | |
Write-Output "ネットワークセキュリティグループ名 : $nsgName" | |
#ネットワークセキュリティグループ作成 | |
Write-Output "ネットワークセキュリティグループを作成します。存在していれば既存のものを利用します。" | |
if (!(Test-AzureResource -ResourceName $nsgName -ResourceType "Microsoft.Network/networkSecurityGroups" -ResourceGroupName $rgName)) { | |
Write-Output "新規に作成します。" | |
#任意の場所からのRDP接続の許可 | |
$nsgRule1 = New-AzureNetworkSecurityRuleConfig ` | |
-Name "allow-rdp-inbound" ` | |
-Description "Allow inbound RDP" ` | |
-SourceAddressPrefix * ` | |
-Protocol Tcp ` | |
-SourcePortRange * ` | |
-DestinationAddressPrefix * ` | |
-DestinationPortRange 3389 ` | |
-Direction Inbound ` | |
-Access Allow ` | |
-Priority 100 | |
#任意の場所からのHTTP接続の許可 | |
$nsgRule2 = New-AzureNetworkSecurityRuleConfig ` | |
-Name "allow-http-inbound" ` | |
-Description "Allow inbound HTTP" ` | |
-SourceAddressPrefix * ` | |
-Protocol Tcp ` | |
-SourcePortRange * ` | |
-DestinationAddressPrefix * ` | |
-DestinationPortRange 80 ` | |
-Direction Inbound ` | |
-Access Allow ` | |
-Priority 110 | |
#ネットワークセキュリティグループの作成 | |
$nsg = New-AzureNetworkSecurityGroup ` | |
-Name $nsgName ` | |
-ResourceGroupName $rgName ` | |
-Location $location ` | |
-SecurityRules $nsgRule1, $nsgRule2 ` | |
-Tag $tags | |
} else { | |
Write-Output "既存のものを利用します。" | |
$nsg = Get-AzureNetworkSecurityGroup ` | |
-Name $nsgName ` | |
-ResourceGroupName $rgName | |
} | |
#NIC設定 | |
$nics = @() | |
#NIC作成 | |
Write-Output "仮想マシン毎のNICを作成します。存在していれば既存のものを利用します。" | |
for ($count = 1; $count -le $vmInstances; $count++) { | |
$nicName = "${prefix}-nic${count}" | |
if (!(Test-AzureResource ` | |
-ResourceName $nicName ` | |
-ResourceType "Microsoft.Network/networkInterfaces" ` | |
-ResourceGroupName $rgName)) { | |
Write-Output "新規に作成します。 : $nicName" | |
$nicIndex = $count -1 | |
#NICをサブネット、ネットワークセキュリティグループ、NATルール、ロードバランサープールに紐づけて作成します。 | |
Write-Output "NICをサブネット、ネットワークセキュリティグループ、NATルール、ロードバランサープールに紐づけて作成します。" | |
$nics += New-AzureNetworkInterface ` | |
-Name $nicName ` | |
-ResourceGroupName $rgName ` | |
-Location $location ` | |
-SubnetId $vnet.Subnets[0].Id ` | |
-NetworkSecurityGroupId $nsg.Id ` | |
-LoadBalancerInboundNatRuleId $lb.InboundNatRules[$nicIndex].Id ` | |
-LoadBalancerBackendAddressPoolId $lb.BackendAddressPools[0].Id | |
} else { | |
Write-Output "既存のものを利用します。" | |
$nics += Get-AzureNetworkInterface ` | |
-Name $nicName ` | |
-ResourceGroupName $rgName | |
} | |
} | |
#可用性セット | |
$avSetName = "${prefix}-as" | |
Write-Output "可用性セット名 : $avSetName" | |
Write-Output "可用性セットを作成します。存在していれば既存のものを利用します。" | |
if (!(Test-AzureResource ` | |
-ResourceName $avSetName ` | |
-ResourceType "Microsoft.Compute/availabilitySets" ` | |
-ResourceGroupName $rgName)) { | |
Write-Output "新規に作成します。" | |
$avSet = New-AzureAvailabilitySet ` | |
-Name $avSetName ` | |
-ResourceGroupName $rgName ` | |
-Location $location | |
} else { | |
Write-Output "既存のものを利用します。" | |
$avSet = Get-AzureAvailabilitySet ` | |
-Name $avSetName ` | |
-ResourceGroupName $rgName | |
} | |
#仮想マシン作成 | |
Write-Output "仮想マシンを作成します。" | |
$vm = @() | |
for($count = 1; $count -le $vmInstances; $count++) { | |
$vmName = "vm${count}" | |
if (!(Test-AzureResource -ResourceName $vmName ` | |
-ResourceType "Microsoft.Compute/virtualMachines" ` | |
-ResourceGroupName $rgName)) { | |
Write-Output "新規に作成します。 : $vmName" | |
$vmIndex = $count -1 | |
$osDiskLabel = "OSDisk" | |
$osDiskName = "${prefix}-${vmName}-osdisk" | |
$osDiskUri = $storageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/${osDiskName}.vhd" | |
$dataDiskSize = 200 #Size in GB | |
$dataDiskLabel = "DataDisk01" | |
$dataDiskName = "${prefix}-${vmName}-datadisk01" | |
$dataDiskUri = $storageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/${dataDiskName}.vhd" | |
$vmConfig = ` | |
New-AzureVMConfig -VMName $vmName -VMSize $vmSize -AvailabilitySetId $avSet.Id | | |
Set-AzureVMOperatingSystem -Windows -ComputerName $vmName -Credential $vmAdminCreds -ProvisionVMAgent -EnableAutoUpdate | | |
Set-AzureVMSourceImage -PublisherName $publiserName -Offer $offerName -Skus $skuName -Version $version | | |
Set-AzureVMSourceImage -PublisherName $publiserName -Offer $offerName -Skus $skuName -Version $version | | |
Set-AzureVMOSDisk -Name $osDiskLabel -VhdUri $osDiskUri -CreateOption fromImage | | |
Add-AzureVMDataDisk -Name $dataDiskLabel -DiskSizeInGB $dataDiskSize -VhdUri $dataDiskUri -CreateOption empty | | |
Add-AzureVMNetworkInterface -Id $nics[$vmIndex].Id -Primary | |
New-AzureVM -VM $vmConfig -ResourceGroupName $rgName -Location $location -Tags $tags | |
} else { | |
Write-Output "既存のVMを利用します。 : $vmName" | |
} | |
$vm += Get-AzureVM ` | |
-Name $vmName ` | |
-ResourceGroupName $rgName | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment