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
の非管理対象ディスク編です。

################################################################
##パラメータの設定
################################################################
##HardCoding
$subscriptionId = "[サブスクリプションID]"
#$OrgResourceGroupName = "org-RG"
#$orgNsgName = "org-NSG"
##Param-String
$vmSize = "Standard_DS1_v2"
$location = "uksouth"
$addressPrefix = "192.168.7.0/24"
$categoryName = "unman"
$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.7.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

################################################################
##ディスク用ストレージアカウントの作成
################################################################
$diskStorageAccountName = $categoryName + "disk392"
New-AzureRmStorageAccount -Location $location `
-Name $diskStorageAccountName -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

$newNsgName = $categoryName + "-nsg"
$nsg = New-AzureRmNetworkSecurityGroup -Location $location -ResourceGroupName $resourceGroupName -Name $newNsgName
$nsg |Add-AzureRmNetworkSecurityRuleConfig -Name rdp-rule -Description "Allow RDP" -Access `
    Allow -Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix Internet `
    -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389 `
    |Set-AzureRmNetworkSecurityGroup
$nsg |Add-AzureRmNetworkSecurityRuleConfig -Name ssh-rule -Description "Allow SSH" -Access `
    Allow -Protocol Tcp -Direction Inbound -Priority 101 -SourceAddressPrefix Internet `
    -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 22 `
    |Set-AzureRmNetworkSecurityGroup

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

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
$STA = Get-AzureRmStorageAccount -ResourceGroupName $resourceGroupName -Name $diskStorageAccountName
$diskName = $vmName + "-disk"
$OSDiskUri = $OSDiskUri = $STA.PrimaryEndpoints.Blob.ToString() + "vhds/" + $diskName + ".vhd"


#オブジェクトに仮想マシンの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-AzureRmVMOSDisk -VM $config -Name $diskName -VhdUri $OSDiskUri -CreateOption fromImage 

#オブジェクトに仮想マシンのイメージを指定
$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 do not you register as a user and use Qiita more conveniently?
  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
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