Help us understand the problem. What is going on with this article?

Azure PowerShellで検証用の仮想マシン(Windows/Linux)(管理ディスク)をたくさん作る

More than 1 year has passed since last update.

非管理対象ディスク編はこちら
Azure PowerShellで検証用の仮想マシン(Windows/Linux)(非管理対象ディスク)をたくさん作る - Qiita

【作った動機】
検証用に色々な種類のまっさらなOSが必要だった。せっかくだしPowerShellの練習にもと思い作成。
NSGは一個コピー元のものを作っておいてばら撒くようなものにしました。

WindowsとLinuxで少しだけ作り方が違うため、名前で分岐するようにしてみました。(名前の戦闘が"W"ならWindowsという安直な分岐)

################################################################
##パラメータの設定
################################################################
##HardCoding
$subscriptionId = "[サブスクリプションID]"
$OrgResourceGroupName = "org-RG"
$orgNsgName = "org-NSG"
##Param-String
$vmSize = "Standard_DS1_v2"
$location = "ukwest"
$addressPrefix = "192.168.100.0/24"
$categoryName = "man"
$arrayParam = @("R68","R73","C69","C74","U16","W16","W12")
$publisherArray = @("RedHat","RedHat","OpenLogic","OpenLogic","Canonical","MicrosoftWindowsServer","MicrosoftWindowsServer")
$offerArray = @("RHEL","RHEL","CentOS","CentOS","UbuntuServer","WindowsServer","WindowsServer")
$skuArray = @("6.8","7.3","6.9","7.4","16.04-LTS","2016-Datacenter","2012-R2-Datacenter")
$subnetName = @("firstSubnet")
$subnetPrefix = @("192.168.100.0/24")

##Option
#$subnetName = @("firstSubnet","secondSubnet")
#$subnetPrefix = @("192.168.1.0/27","192.168.1.32/27")


################################################################
##ログイン、サブスクリプションの選択、Credentialの作成
################################################################
$cred = Get-Credential
Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionId $subscriptionId


################################################################
##リソースグループの作成
################################################################
$resourceGroupName = $categoryName + "-RG"
New-AzureRmResourceGroup -Location $location -Name $resourceGroupName
Get-AzureRmResourceGroup -Name $resourceGroupName

################################################################
##vNetの作成
################################################################
$virtualNetworkName = $categoryName + "-vnet"
$vnet = New-AzureRmVirtualNetwork -AddressPrefix $addressPrefix -Location $location `
-Name $virtualNetworkName -ResourceGroupName $resourceGroupName

#サブネットの作成
$vnet = Get-AzureRmVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $resourceGroupName
for($i=0;$i -le $subnetName.Length -1; $i++){
Add-AzureRmVirtualNetworkSubnetConfig -AddressPrefix $subnetPrefix[$i] -Name $subnetName[$i] -VirtualNetwork $vnet
Set-AzureRmVirtualNetworkSubnetConfig -AddressPrefix $subnetPrefix[$i] -Name $subnetName[$i] -VirtualNetwork $vnet
}
$vnet= Set-AzureRmVirtualNetwork -VirtualNetwork $vnet

################################################################
##起動診断用ストレージアカウントの作成
################################################################
$diagStorageAccountName = $categoryName + "diag392"
New-AzureRmStorageAccount -Location $location `
-Name $diagStorageAccountName -ResourceGroupName $resourceGroupName -SkuName Standard_LRS

################################################################
##NSGの作成
################################################################
#オリジナルのNSGルールを取得
$orgNsgRule = (Get-AzureRmNetworkSecurityGroup -Name $orgNsgName -ResourceGroupName $OrgResourceGroupName).SecurityRules

#ルールをもとにNSGの作成
$newNsgName = $categoryName + "-nsg"
$nsg = New-AzureRmNetworkSecurityGroup -Location $location -Name $newNsgName `
-ResourceGroupName $ResourceGroupName -SecurityRules $orgNsgRule

################################################################
################################################################
##以降は名前ごとにループ処理
################################################################
################################################################

for($i=0;$i -le $arrayParam.Length -1; $i++){
################################################################
##NICの作成
################################################################
$PublicIpAddressName = $categoryName + "-" + $arrayParam[$i] + "-" + "ip"
$pip = New-AzureRmPublicIpAddress -AllocationMethod Dynamic `
-ResourceGroupName $resourceGroupName `
-IpAddressVersion IPv4 -Location $location -Name $PublicIpAddressName

#NICを配置する仮想ネットワークとサブネットの取得
$vnet = Get-AzureRmVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $resourceGroupName
$subnet = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name $subnetName[0]

#NICの作成
$nicName = $categoryName + "-" + $arrayParam[$i] + "-" + "nic"
$nic = New-AzureRmNetworkInterface -Location $location `
-Name $nicName -ResourceGroupName $resourceGroupName -Subnet $subnet `
-PublicIpAddress $pip -NetworkSecurityGroup $nsg

################################################################
##VMの作成
################################################################
#仮想マシン構成オブジェクトを作成
$vmName = $categoryName + "-" + $arrayParam[$i]
$config = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize

#オブジェクトに仮想マシンのOS情報を設定
if($arrayParam[$i] -like "W*"){
$config = Set-AzureRmVMOperatingSystem -ComputerName $vmName `
-Credential $cred -VM $config -Windows
}else{
$config = Set-AzureRmVMOperatingSystem -ComputerName $vmName `
-Credential $cred -VM $config -Linux
}

#ソースイメージの取得
#$orgImageName = "img-" + $arrayParam[$i]
#$image = Get-AzureRmImage -ResourceGroupName $OrgResourceGroupName -ImageName $orgImageName

#オブジェクトに仮想マシンのソースイメージの設定
#$config = Set-AzureRmVMSourceImage -VM $config -Id $image.id

#オブジェクトに仮想マシンのイメージを指定
$config = Set-AzureRmVMSourceImage -PublisherName $publisherArray[$i] -Offer $offerArray[$i] -Skus $skuArray[$i] -Version "latest" -vm $config

#オブジェクトに仮想マシンのNICを設定
$config = Add-AzureRmVMNetworkInterface -NetworkInterface $nic -VM $config

#オブジェクトに起動診断を設定
$config = Set-AzureRmVMBootDiagnostics -VM $config -Enable -ResourceGroupName $resourceGroupName -StorageAccountName $diagStorageAccountName

#オブジェクトをもとに仮想マシンを作成
New-AzureRmVM -Location $location -ResourceGroupName $ResourceGroupName -VM $config
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした