Archivi tag: Azure Resource Manager

Creare una VM in Azure tramite Powershell e ARM

AzureIl seguente sript crea una Vitual Machine in Azure tramite Powershell e Azure Resource Manager (ARM).

Elementi creati dallo script
  • Resource Group
  • Availability Set
  • Virtual Network e subnet
  • Network interface e Public IP
Caratteristiche VM

Sistema Operativo: Windows Server 2012 R2 DataCenter
Size: Standard A1

Prerequisiti

Powershell v. 5
Modulo Azure 3.0.0 (Installazione e configurazione di Azure Powershell)

#Variable
$rgName = Read-Host -Prompt 'Resource Group name';
$locName = 'West Europe';

#Create Resource Group
New-AzureRmResourceGroup -Name $rgName -Location $locName ;

#Create Availability Set
$asName = Read-Host -Prompt 'Availability Set name'
New-AzureRmAvailabilitySet -ResourceGroupName $rgName -Name $asName -Location $locName -PlatformUpdateDomainCount 2 -PlatformFaultDomainCount 2
$AvID = (Get-AzureRmAvailabilitySet -ResourceGroupName $rgName -Name $AsName).id

#Create subnet
$newSubnetParams = @{
    'Name' = Read-Host -Prompt 'Subnet name'
    'AddressPrefix' = '10.0.0.0/24'
}
$subnet = New-AzureRmVirtualNetworkSubnetConfig @newSubnetParams

#Create VirtualNet
$newVNetParams = @{
    'Name' = Read-Host -Prompt 'Network Name'
    'ResourceGroupName' = $rgName
    'Location' = $locName
    'AddressPrefix' = '10.0.0.0/16'
}
$vNet = New-AzureRmVirtualNetwork @newVNetParams -Subnet $subnet

#Create Storage Account
$newStorageAcctParams = @{
    'Name' = Read-Host -Prompt 'Storage Account name (lowercase)' 
    'ResourceGroupName' = $rgName
    'Type' = 'Standard_LRS'
    'Location' = $locName
}
$storageAccount = New-AzureRmStorageAccount @newStorageAcctParams

#Create Public IP
$newPublicIpParams = @{
    'Name' = Read-Host -Prompt 'Label Public IP'
    'ResourceGroupName' = $rgName
    'AllocationMethod' = 'Dynamic' ## Dynamic or Static
    'DomainNameLabel' = Read-Host -Prompt 'DNS name'
    'Location' = $locName
}
$publicIp = New-AzureRmPublicIpAddress @newPublicIpParams

#Create NIC
$newVNicParams = @{
    'Name' = Read-Host -Prompt 'NIC Name'
    'ResourceGroupName' = $rgName
    'Location' = $locName
}
$vNic = New-AzureRmNetworkInterface @newVNicParams -SubnetId $vNet.Subnets[0].Id -PublicIpAddressId $publicIp.Id

#Config VM - General
$newConfigParams = @{
    'VMName' = Read-Host -Prompt 'VM name'
    'VMSize' = 'Standard_A1'
    'AvailabilitySetId' = $AvID
}
$vmConfig = New-AzureRmVMConfig @newConfigParams

#Config VM - Operating System
$newVmOsParams = @{
    'Windows' = $true
    'ComputerName' = $newConfigParams.VmName
    'Credential' = (Get-Credential -Message 'Type the name and password of the local administrator account.')
    'ProvisionVMAgent' = $true
    'EnableAutoUpdate' = $true
}
$vm = Set-AzureRmVMOperatingSystem @newVmOsParams -VM $vmConfig

#Config VM - Image
$newSourceImageParams = @{
    'PublisherName' = 'MicrosoftWindowsServer'
    'Version' = 'latest'
    'Skus' = '2012-R2-Datacenter'
}
 
$offer = Get-AzureRmVMImageOffer -Location $locName -PublisherName MicrosoftWindowsServer
$vm = Set-AzureRmVMSourceImage @newSourceImageParams -VM $vm -Offer $offer.Offer

#Attach NIC to VM
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $vNic.Id

#Config VM - Disk
$Saname = $newStorageAcctParams.name
$VmName = $newConfigParams.VmName
$osDiskName = $vmName+'_os'
$osDiskUri = 'https://'+$saname+'.blob.core.windows.net/vhds/'+$osDiskName+'.vhd'
 
$newOsDiskParams = @{
    'Name' = $osDiskName
    'CreateOption' = 'fromImage'
}
 
$vm = Set-AzureRmVMOSDisk @newOsDiskParams -VM $vm -VhdUri $osDiskUri

#Create Vm
New-AzureRmVM -VM $vm -ResourceGroupName $rgName -Location $locName

Disclaimer: Nessuna garanzia è fornita con questo script. Lo scrivente non è responsabile di qualunque danno possa eventualmente derivarne – lo utilizzate a vostro rischio.

Migrare VM da VmWare ad Azure Resource Manager (ARM)

E’ possibile migrare VM da VmWare ad Azure Resource Manager (ARS) in due modi:

  • AzureTramite MVMC
  • Manuale

In entrambi i casi, è richiesto Azure  Powershell (https://azure.microsoft.com/en-us/documentation/articles/powershell-install-configure/)

  • Tramite MVMC
    Utilizzando Microsoft Virtual Machine Converter (MVMC). Download QUA.
    La migrazione sarà semi-automatica in quanto MVMC 3.1 non supporta ASR
  1. Installare MVMC in una macchina configurata sulla stessa rete del vCenter/server ESX
  2. Eseguire la migrazione tramite il wizard o tramite cmdlet Powershell 3.0
  3. Copiare i VHD nello storage account in ARM tramite il cmdlet Powershell Start-AzureStorageBlobCopy.
    Qua gli script preconfigurati: https://www.opsgility.com/blog/windows-azure-powershell-reference-guide/copying-vhds-blobs-between-storage-accounts/
  4. Creare la nuova VM tramite Powershell Azure con i dischi copiati (https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-ps-create-preconfigure-windows-resource-manager-vms/)
  5. Eliminare i dischi dallo storage account “classico”
  • Manuale
    Verranno creati i VHD tramite il software Disk2VHD. Download QUA
  1. Installare Disk2VHD nella macchina da convertire
  2. Convertire i dischi da uploadare
  3. Eseguire l’upload dei dischi tramite il seguente comando Powershell:
     Add-AzureRmVhd -ResourceGroupName YourResourceGroup -Destination “<StorageAccountURL>/<BlobContainer>/<TargetVHDName>.vhd” -LocalFilePath <LocalPathOfVHDFile>
  4. Creare la nuova VM tramite Powershell Azure con i dischi copiati (https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-ps-create-preconfigure-windows-resource-manager-vms/)