Ask your technical questions on forums or here :
ASP.NET or MVC | C# | Windows Phone
Microsoft Technology Journals by Abhimanyu K Vatsa
HOME ABOUT RAZOR BOOK SPEAKING MVC ASP.NET JQUERY VIDEOS EBOOK ARCHIVE
Showing posts with label Virtual Machine. Show all posts
Showing posts with label Virtual Machine. Show all posts

15 Dec 2017

Create virtual machine using PowerShell command

Just run this PowerShell script to create virtual machine, you can change setting in order to have other operating system and pricing.

# Login
Login-AzureRmAccount

# Variables for common values
$resourceGroup = "demo-resgroup"
$location = "westeurope"
$vmName = "demovm0"
$publicIP = "spublicip0"
$subnet = "subnet0"
$vnet = "vnet0"
$nsg = "nsg0"
$nsgrdp = "nsgrdp0"
$nsgwww = "nsgwww0"
$nsgsp = "nsgps"
$nic = "nic0"

# Create resource group
New-AzureRmResourceGroup -ResourceGroupName $resourceGroup -Location $location

# Get vm credentials
$cred = Get-Credential

# Create a subnet configuration
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig `
    -Name $subnet `
    -AddressPrefix 192.168.1.0/24

# Create a virtual network
$vnet = New-AzureRmVirtualNetwork `
    -ResourceGroupName $resourceGroup `
    -Location $location `
    -Name $vnet `
    -AddressPrefix 192.168.0.0/16 `
    -Subnet $subnetConfig

# Create a public IP address and specify a DNS name
$publicIP = New-AzureRmPublicIpAddress `
    -ResourceGroupName $resourceGroup `
    -Location $location `
    -AllocationMethod Static `
    -IdleTimeoutInMinutes 4 `
    -Name $publicIP

# Create an inbound network security group rule for port 3389
$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig `
    -Name $nsgrdp  `
    -Protocol Tcp `
    -Direction Inbound `
    -Priority 1000 `
    -SourceAddressPrefix * `
    -SourcePortRange * `
    -DestinationAddressPrefix * `
    -DestinationPortRange 3389 `
    -Access Allow

# Create an inbound network security group rule for port 80
$nsgRuleWeb = New-AzureRmNetworkSecurityRuleConfig `
    -Name $nsgwww  `
    -Protocol Tcp `
    -Direction Inbound `
    -Priority 1001 `
    -SourceAddressPrefix * `
    -SourcePortRange * `
    -DestinationAddressPrefix * `
    -DestinationPortRange 80 `
    -Access Allow

# Create a network security group
$nsg = New-AzureRmNetworkSecurityGroup `
    -ResourceGroupName $resourceGroup `
    -Location $location `
    -Name $nsg `
    -SecurityRules $nsgRuleRDP,$nsgRuleWeb

# Create a virtual network card and associate with public IP address and NSG
$nic = New-AzureRmNetworkInterface `
    -Name $nic `
    -ResourceGroupName $resourceGroup `
    -Location $location `
    -SubnetId $vnet.Subnets[0].Id `
    -PublicIpAddressId $publicIP.Id `
    -NetworkSecurityGroupId $nsg.Id

# Create a virtual machine configuration Standard_A1 (4112 INR Per month), Standard_DS2 (11902 INR Per month) https://azure.microsoft.com/en-in/pricing/calculator
$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize Standard_D1 | `
Set-AzureRmVMOperatingSystem -Windows -ComputerName $vmName -Credential $cred | `
Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer `
    -Offer WindowsServer -Skus 2016-Datacenter -Version latest | `
Add-AzureRmVMNetworkInterface -Id $nic.Id

# Create a virtual machine using the configuration
New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vmConfig

# Install IIS, .NET Framework
Set-AzureRmVMExtension -ResourceGroupName $resourceGroup `
    -ExtensionName IIS `
    -VMName $vmName `
    -Publisher Microsoft.Compute `
    -ExtensionType CustomScriptExtension `
    -TypeHandlerVersion 1.4 `
    -SettingString '{"commandToExecute":"powershell Add-WindowsFeature Web-Server,Web-Asp-Net45,NET-Framework-Features;powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $($env:computername)"}' `
    -Location $location

# Get public ip address of the machine
Get-AzureRmPublicIpAddress -ResourceGroupName $resourceGroup

5 May 2017

Migrating Azure VMs on VNet from Azure Service Management (Classic or ASM) to Azure Resource Manager (Resource Manager or ARM)

In this post you will find code snippet to migrate your classic Azure VMs that is part of virtual networks. That is, you will learn Migrating Azure VMs on VNet from Azure Service Management (Classic or ASM) to Azure Resource Manager (Resource Manager or ARM). Without too much discussion, let's start step by step.

You need to have PowerShell installed. In the process of migration you first need to sign in your subscription using modern way that is ARM to prepare migration and then sign in your same subscription using classic approach that is ASM to perform migration and commit.

Step 1.
Let's login ARM

Login-AzureRmAccount

Step 2.
Store subscription id into a variable

$SubID = "1345e4-4561-1bd7-55c1-e3848012qw4r"

Step 3.
Select subscription using above variable

Select-AzureRmSubscription -SubscriptionID $SubID

Step 4.
Now, let's prepare the migration on ARM, this operation may take few minutes.

Register-AzureRmResourceProvider -ProviderNamespace Microsoft.ClassicInfrastructureMigrate

Step 5.
Now, in order to know the progress just use below command, you should see RegistrationState to Registered. Once you see Registered, you are good to go ahead.

Get-AzureRmResourceProvider -ProviderNamespace Microsoft.ClassicInfrastructureMigrate

Step 6.
Let's login ASM now

Add-AzureAccount

Step 7.
Then, select the subscription using subscription id that we stored in variable above.

Select-AzureSubscription -SubscriptionID $SubID

Step 8.
Now, let's get virtual network name that we will in next step.

Get-AzureVNetConfig -ExportToFile "C:\Users\Abhimanyu K Vatsa\Desktop\VNetConfig.xml"

Now you copy the value of VirtualNetworkSite name="demo-vnet-name"

Step 9.
Store the virtual network name in a variable. Remember, we are migrating virtual network that will automatically migration everything that is associated with this network, including network itself, vms, storage, load-balancer, subnet, nic etc.

$VNetName = "demo-vnet-name"

Step 10.
Let's run most powerful command that prepares the move and it may take few minutes.

Move-AzureVirtualNetwork -Prepare -VirtualNetworkName $VNetName

Step 11.
If you login to portal now, you will notice a new resource group is create with same name but -Migrated as post-fix. Don't worry, if you see everything doubled. Because these are exact same copy of your resources that was in classic. Once you run commit command (given below) your all classic resources will be removed and you will have migrated resources on ARM and every thing working as it was working earlier. Note, this operation may also take some time.

Move-AzureVirtualNetwork -Commit -VirtualNetworkName $VNetName

Or if you feel something going wrong, you can abort it using below command:

Move-AzureVirtualNetwork -Abort -VirtualNetworkName $VNetName

I used these commands several times and never faced any challenges, but still recommend you to run it yourself before your migrate production resources.

Hope this helps.