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

Azure PowerShellで仮想マシンV2を作成する (Sysprepしていない既存のVHD)

More than 3 years have passed since last update.

Sysprepされていない既存のVHDから仮想マシンV2を作成するサンプルスクリプトになります。

# コピーしたVHDから仮想マシンを作成する

$subscriptionId =  '[サブスクリプション名]'
$storageAccountName = '[ストレージアカウント名]'
$vhdUri = '[VHDが格納されているストレージURI https://xxxx.blob.core.windows.net/vhds/base.vhd]'

# ログイン
Login-AzureRmAccount

# サブスクリプション 確定
Select-AzureRmSubscription -SubscriptionId $subscriptionId

# ストレージアカウント 取得
$storageAccount = Get-AzureRmStorageAccount | ? StorageAccountName -EQ $storageAccountName
if(-not $storageAccount) {  
    throw "ストレージアカウント '$storageAccountName' が存在しません。"
}

$VerbosePreference = 'Continue'
$ErrorActionPreference = 'Stop'

$resourceGroupName = '[リソースグループ名]'
$location = $storageAccount.Location

$vmName = '[VM名]'
$vmSize = '[VMサイズ Standard_D1]'
$nicName = '[NetworkInterface名]'
$ipName = '[PublicIpAddress名]'
$vnetName = '[Virtual Network名]'
$diskName = '[OSディスク名]'

# Virtual Network 作成
Write-Verbose 'Creating Virtual Network'  
$vnetDef = New-AzureRmVirtualNetwork -ResourceGroupName $resourceGroupName -Location $location -Name $vnetName -AddressPrefix '10.0.0.0/16'
Write-Verbose 'Adding subnet to Virtual Network'  
$vnet = $vnetDef | Add-AzureRmVirtualNetworkSubnetConfig -Name 'Subnet-1' -AddressPrefix '10.0.0.0/24' | Set-AzureRmVirtualNetwork

# Virtual Network がすでに構築済みの場合はこちら
#Write-Verbose 'Get Existing Virtual Network'
#$vnet = Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroupName
#if(-not $vnet) {  
#    throw "Unable to find virtual network '$vnetName'. Cannot continue."
#}

# Public IP Address 作成
Write-Verbose 'Creating Public IP'  
$pip = New-AzureRmPublicIpAddress -ResourceGroupName $resourceGroupName -Location $location -Name $ipName -AllocationMethod Dynamic

# Network Interface 作成
Write-Verbose 'Creating NIC'  
$nic = New-AzureRmNetworkInterface -ResourceGroupName $resourceGroupName -Location $location -Name $nicName -PublicIpAddressId $pip.Id -SubnetId $vnet.Subnets[0].Id 

# VM名とサイズの決定
Write-Verbose 'Creating VM Config'  
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize

# VMにNetwork Interfaceを関連付け
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id

# OSディスク設定
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $vhdUri -CreateOption Attach -Windows

# VM 作成
Write-Verbose 'Creating VM...'  
$result = New-AzureRmVM -ResourceGroupName $resourceGroupName -Location $location -VM $vm

if($result.Status -eq 'OK') {  
    $result
    Write-Verbose ('仮想マシン ''{0}'' は正常に作成されました。' -f $vmName)
} else {
    Write-Error '仮想マシンは正常に作成されませんでした。'
}
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
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