首页 文章

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

提问于
浏览
0

将本地自定义.vhd文件部署到Azure并使用新的Resource Manager格式运行VM所需的PowerShell命令是什么?

那里有很多指南,但没有一个是完整的端到端,所有似乎都是针对“经典”模式 .

1 回答

  • 1

    以下PowerShell脚本将:

    • 登录

    • 创建资源组 .

    • 创建存储帐户 .

    • 创建存储帐户容器 .

    • 将本地VHD文件部署到创建的存储帐户/容器/等 .

    • 使用公共IP和内部IP创建虚拟网络设备 .

    • 使用网络设备创建安装更新的磁盘映像的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.'
    }
    

相关问题