简体   繁体   English

如何通过PowerShell使用新的资源管理器将自定义虚拟机部署到Azure?

[英]How to deploy a custom virtual machine to Azure utilizing the new Resource Manager via PowerShell?

What are the PowerShell commands you need to deploy a local custom .vhd file to Azure and get a VM running utilizing the new Resource Manager format? 将本地自定义.vhd文件部署到Azure并使用新的Resource Manager格式运行VM所需的PowerShell命令是什么?

There are a lot of guides out there, but none of them are complete end-to-end, and all seem to be aimed towards "Classic" mode. 那里有很多指南,但是没有一个是完整的端到端指南,而且似乎都是针对“经典”模式的。

The following PowerShell Script will: 以下PowerShell脚本将:

  1. Login. 登录。
  2. Create a Resource Group. 创建一个资源组。
  3. Create a Storage Account. 创建一个存储帐户。
  4. Create a Storage Account Container. 创建一个存储帐户容器。
  5. Deploy a local VHD file to the created Storage Account / Container / etc. 将本地VHD文件部署到创建的存储帐户/容器/等。
  6. Create a virtual networking device with a public IP & internal IP. 创建具有公共IP和内部IP的虚拟网络设备。
  7. Create a VM that mounts the disk image updated, utilizing the network device. 使用网络设备创建安装已更新磁盘映像的VM。
#LOGIN
Login-AzureRmAccount

# Enable verbose output and stop on error
#$VerbosePreference = 'Continue'
$ErrorActionPreference = 'Stop'

################## Upload Custom VM to Storage Account Details ##################################
Write-Host "The following is a list of available locations:"
((Get-AzureRmResourceProvider -ProviderNamespace  Microsoft.Storage).ResourceTypes).Locations | Select-Object -unique
$locationName = Read-Host 'Which location do you want to create things at?'

Write-Host "The following is a list of available Subscriptions:"
Get-AzureRmSubscription | format-Table -Property SubscriptionName
$subscriptionName = Read-Host 'What is the subscription name you want?'

Write-Host "Getting the Subscription..."
Get-AzureRmSubscription –SubscriptionName $subscriptionName | Select-AzureRmSubscription

$resourceGroupName = Read-Host 'What do you want to call the Resource Group?'
New-AzureRmResourceGroup -Name $resourceGroupName -Location $locationName

$storageAccountName = Read-Host 'What do you want to call the storage account?'
Write-Host "Creating the Storage Account..."
New-AzureRmStorageAccount –StorageAccountName $storageAccountName -Location $locationName -ResourceGroupName $resourceGroupName -Type "Standard_LRS"
$storageAccount = Get-AzureRmStorageAccount -StorageAccountName $storageAccountName

$storageContainerName = Read-Host 'What do you want to call the storage container?'
Write-Host "Getting the Storage Container..."
Get-AzureRmStorageAccount | New-AzureStorageContainer -Name $storageContainerName

Write-Host "Getting the Container Endpoint URI..."
$containerURI = (Get-AzureRmStorageAccount | Get-AzureStorageContainer -Name $storageContainerName).CloudBlobContainer.StorageUri.PrimaryUri.AbsoluteUri

$localFileName = Read-Host 'What is the absolute local file path for the source .vhd file?'
$storageFileName = Read-Host 'What do you wan to name the destination file on the storage account? (NOTE: Do NOT include .vhd on the end)'

$osDiskUri = $containerUri + '/' + $storageFileName + '.vhd'
#Upload to URL specified, with container name at end of path, the local fle specified.
Add-AzureRmVhd -Destination $osDiskUri -LocalFilePath $localFileName -ResourceGroupName $resourceGroupName


################## Create VM From Storage Account Details ##################################

$vmName = Read-Host 'What do you want to name the Virtual Machine?'

$vmSuffix = Get-Random -Minimum 10000 -Maximum 99999
$vmSize = 'Standard_D3'
$nicName = 'VM{0}-NIC' -f $vmSuffix
$ipName = 'VM{0}-IP' -f $vmSuffix
$domName = 'vm-from-customimage-powershell-{0}' -f $vmSuffix
$vnetName = $vmName + '_network'


# Create the VNET
Write-Verbose 'Creating Virtual Network'  
$vnetDef = New-AzureRmVirtualNetwork -ResourceGroupName $resourceGroupName -Location $locationName -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

# Create the PIP
Write-Verbose 'Creating Public IP'  
$pip = New-AzureRmPublicIpAddress -ResourceGroupName $resourceGroupName -Location $locationName -Name $ipName -DomainNameLabel $domName -AllocationMethod Dynamic
# Create the NIC
Write-Verbose 'Creating NIC'  
$nic = New-AzureRmNetworkInterface -ResourceGroupName $resourceGroupName -Location $locationName -Name $nicName -PublicIpAddressId $pip.Id -SubnetId $vnet.Subnets[0].Id 

# Specify the VM name and size
Write-Verbose 'Creating VM Config'  
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize 
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $storageFileName -VhdUri $osDiskUri -CreateOption Attach -Linux

Write-Verbose 'Creating VM...'  
$result = New-AzureRmVM -ResourceGroupName $resourceGroupName -Location $locationName -VM $vm

if($result.Status -eq 'Succeeded') {  
    $result
    Write-Verbose ('VM named ''{0}'' is now ready')
} else {
    Write-Error 'Virtual machine was not created sucessfully.'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 azure 资源管理器模板在 linux 的虚拟机规模集上创建自定义脚本扩展? - How to create custom script extension on virtual machine scale set of linux using azure resource manager template? 在远程资源管理器中访问Azure虚拟机 - Access azure virtual machine in resource manager in remote 同时使用Azure-Powershell部署虚拟机 - Deploy Virtual Machine with Azure-Powershell concurrently 在Powershell中设置Azure虚拟机资源组 - Set Azure virtual machine resource group in powershell 如何使用基于Azure资源管理器的PowerShell部署Azure Web Apps? - How to deploy Azure Web Apps using Azure Resource Manager-Based PowerShell? 无法登录使用资源管理器创建的Azure虚拟机 - Can't Log into Azure Virtual Machine created w/ Resource Manager 在资源管理器门户中创建的Azure虚拟机中安装exe - Install an exe in azure virtual machine created in resource manager portal 使用资源管理器在虚拟机规模集上设置Azure多个公共IP - Azure Multiple Public IPs on a Virtual Machine Scale Set with Resource Manager 如何为Azure资源管理器虚拟机配置备用远程桌面公用端口 - How to configure an alternate remote desktop public port for an Azure Resource Manager virtual machine 通过 Azure 资源管理器模板部署 Azure 表 - Deploy Azure Table via Azure Resource Manager Template
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM