2016-02-10 45 views
5

現在是否有人如何從新的Azure門戶中的現有VHD創建虛擬機?從現有VHD創建虛擬機:預覽門戶

我可以在manage.windowsazure.com上找到關於如何做到這一點的很多信息,但是在portal.azure.com上沒有任何關於此功能的信息。

回答

5

它不能在門戶網站上直接完成。你將不得不使用PowerShell。

  1. 創建一個storageaccount。例如在門戶中。

  2. 將VHD上傳到天藍色。要做到這一點,在PowerShell中運行以下符合Login-AzureRmAccount登錄後(改變參數<>和之間的路徑VHD你的硬盤上):

    Add-AzurermVhd -Destination "https://<StorageAccountName>.blob.core.windows.net/<containerName>/<vhdname>.vhd" -LocalFilePath "D:\Virtual Machines\myharddisk.vhd" -ResourceGroupName "<ResourceGroupName" -Overwrite 
    
  3. 創建一個ARM模板。 多種可能性你可以做什麼。 例如選擇從Azure Quickstart templates一個模板,例如101

我所做的是:

  • 創建在Visual Studio中的新項目2015年
  • 選擇以下項目:雲 - > Azure Resource Group
  • 選擇以下模板:Windows虛擬機

  • 更改了一些參數並刪除了所有不必要的東西。 它現在的功能是:使用上傳的vhd作爲硬盤創建Windows虛擬機。 現在它正在使用參數json文件,並且還必須在WindowsVirtualMachine.json中設置一些變量。這可能是重構的。但現在它會做需要的。

對於這個示例,你必須有以下目錄結構(就像Visual Studio中創建它)

ProjectDirectory/Scripts/Deploy-AzureResourceGroup.ps1 
ProjectDirectory/Templates/WindowsVirtualMachine.json 
ProjectDirectory/Templates/WindowsVirtualMachine.parameters.json 

部署,AzureResourceGroup.ps1

#Requires -Version 3.0 
#Requires -Module AzureRM.Resources 
#Requires -Module Azure.Storage 

Param(
    [string] [Parameter(Mandatory=$true)] $ResourceGroupLocation, 
    [string] $ResourceGroupName = 'CreateImage',  
    [string] $TemplateFile = '..\Templates\WindowsVirtualMachine.json', 
    [string] $TemplateParametersFile = '..\Templates\WindowsVirtualMachine.parameters.json' 
) 

Import-Module Azure -ErrorAction SilentlyContinue 

try { 
    [Microsoft.Azure.Common.Authentication.AzureSession]::ClientFactory.AddUserAgent("VSAzureTools-$UI$($host.name)".replace(" ","_"), "2.8") 
} catch { } 

Set-StrictMode -Version 3 

$OptionalParameters = New-Object -TypeName Hashtable 
$TemplateFile = [System.IO.Path]::Combine($PSScriptRoot, $TemplateFile) 
$TemplateParametersFile = [System.IO.Path]::Combine($PSScriptRoot, $TemplateParametersFile) 


# Create or update the resource group using the specified template file and template parameters file 
New-AzureRmResourceGroup -Name $ResourceGroupName -Location $ResourceGroupLocation -Verbose -Force -ErrorAction Stop 

New-AzureRmResourceGroupDeployment -Name ((Get-ChildItem $TemplateFile).BaseName + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')) ` 
            -ResourceGroupName $ResourceGroupName ` 
            -TemplateFile $TemplateFile ` 
            -TemplateParameterFile $TemplateParametersFile ` 
            @OptionalParameters ` 
            -Force -Verbose 

WindowsVirtualMachine.json

{ 
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 
    "contentVersion": "1.0.0.0", 
    "parameters": {  
    "dnsNameForPublicIP": { 
     "type": "string", 
     "minLength": 1, 
     "metadata": { 
     "description": "Globally unique DNS Name for the Public IP used to access the Virtual Machine." 
     } 
    } 
    }, 
    "variables": { 
    "OSDiskName": "<vhdNameWithoutExtension>", 
    "vhdStorageContainerName": "<containerName>", 
    "storageAccountName": "<StorageAccountName>", 
    "nicName": "myVMNic", 
    "addressPrefix": "10.0.0.0/16", 
    "subnetName": "Subnet", 
    "subnetPrefix": "10.0.0.0/24", 
    "vhdStorageType": "Standard_LRS", 
    "publicIPAddressName": "myPublicIP", 
    "publicIPAddressType": "Dynamic", 
    "vhdStorageName": "[concat('vhdstorage', uniqueString(resourceGroup().id))]", 
    "vmName": "MyWindowsVM", 
    "vmSize": "Standard_A2", 
    "virtualNetworkName": "MyVNET", 
    "vnetId": "[resourceId('Microsoft.Network/virtualNetworks', variables('virtualNetworkName'))]", 
    "subnetRef": "[concat(variables('vnetId'), '/subnets/', variables('subnetName'))]" 
    }, 
    "resources": [ 
    { 
     "type": "Microsoft.Storage/storageAccounts", 
     "name": "[variables('vhdStorageName')]", 
     "apiVersion": "2015-06-15", 
     "location": "[resourceGroup().location]", 
     "tags": { 
     "displayName": "StorageAccount" 
     }, 
     "properties": { 
     "accountType": "[variables('vhdStorageType')]" 
     } 
    }, 
    { 
     "apiVersion": "2015-06-15", 
     "type": "Microsoft.Network/publicIPAddresses", 
     "name": "[variables('publicIPAddressName')]", 
     "location": "[resourceGroup().location]", 
     "tags": { 
     "displayName": "PublicIPAddress" 
     }, 
     "properties": { 
     "publicIPAllocationMethod": "[variables('publicIPAddressType')]", 
     "dnsSettings": { 
      "domainNameLabel": "[parameters('dnsNameForPublicIP')]" 
     } 
     } 
    }, 
    { 
     "apiVersion": "2015-06-15", 
     "type": "Microsoft.Network/virtualNetworks", 
     "name": "[variables('virtualNetworkName')]", 
     "location": "[resourceGroup().location]", 
     "tags": { 
     "displayName": "VirtualNetwork" 
     }, 
     "properties": { 
     "addressSpace": { 
      "addressPrefixes": [ 
      "[variables('addressPrefix')]" 
      ] 
     }, 
     "subnets": [ 
      { 
      "name": "[variables('subnetName')]", 
      "properties": { 
       "addressPrefix": "[variables('subnetPrefix')]" 
      } 
      } 
     ] 
     } 
    }, 
    { 
     "apiVersion": "2015-06-15", 
     "type": "Microsoft.Network/networkInterfaces", 
     "name": "[variables('nicName')]", 
     "location": "[resourceGroup().location]", 
     "tags": { 
     "displayName": "NetworkInterface" 
     }, 
     "dependsOn": [ 
     "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]", 
     "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]" 
     ], 
     "properties": { 
     "ipConfigurations": [ 
      { 
      "name": "ipconfig1", 
      "properties": { 
       "privateIPAllocationMethod": "Dynamic", 
       "publicIPAddress": { 
       "id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]" 
       }, 
       "subnet": { 
       "id": "[variables('subnetRef')]" 
       } 
      } 
      } 
     ] 
     } 
    }, 
    { 
     "apiVersion": "2015-06-15", 
     "type": "Microsoft.Compute/virtualMachines", 
     "name": "[variables('vmName')]", 
     "location": "[resourceGroup().location]", 
     "tags": { 
     "displayName": "VirtualMachine" 
     }, 
     "dependsOn": [ 
     "[concat('Microsoft.Storage/storageAccounts/', variables('vhdStorageName'))]", 
     "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]" 
     ], 
     "properties": { 
     "hardwareProfile": { 
      "vmSize": "[variables('vmSize')]" 
     },  
     "storageProfile": {   
      "osDisk": { 
      "name": "osdisk", 
      "osType": "Windows", 
      "vhd": { 
       "uri": "[concat('http://', variables('storageAccountName'), '.blob.core.windows.net/', variables('vhdStorageContainerName'), '/', variables('OSDiskName'), '.vhd')]" 
      }, 
      "caching": "ReadWrite", 
      "createOption": "Attach" 
      } 
     }, 
     "networkProfile": { 
      "networkInterfaces": [ 
      { 
       "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]" 
      } 
      ] 
     } 
     }  
    } 
    ] 
} 

WindowsVirtualMachine.parameters.json

{ 
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#", 
    "contentVersion": "1.0.0.0", 
    "parameters": {  
     "dnsNameForPublicIP": { 
      "value": "<someUniqueNameForYourDnsName>" 
     } 
    } 
} 
  • 執行的powershell腳本 打開一個powershell命令並執行腳本PS1。你只需要通過在那裏你要像創建虛擬機的位置:(你應該已經通過Login-AzureRmAccount登錄)

    在這兩個JSON文件運行變化的參數之前!
    .\Deploy-AzureResourceGroup.ps1 "West Europe"

  • 的記錄應該告訴你,VM創建成功。

    +0

    完美地完成了這項工作。非常感謝 – naylormat

    +0

    耶穌,在視覺工作室創建一個項目,在蔚藍的天空部署一個虛擬現實,你真的有很多時間在你的手中 – 4c74356b41

    3

    今天(2016年10月)它仍然無法在新門戶中完成。

    但對於完整性:你能做到這一點在老門戶網站(https://manage.windowsazure.com):

    單擊新建 - 計算 - 虛擬機 - 從畫廊。 在左側選擇「我的圖像」或「我的磁盤」,然後選擇要使用的VHD。 照常按照說明操作。

    相關問題