Monday, 24 October 2016

Deploying Windows VM from custom image using Powershell.




How you can deploy a Windows Machine from your source VM image. We have plenty of methods using that we can deploy the VM. Like CLI, Powershell, using Json template.

Here we use Powershell Method to deploy the machine. First thing upload your source VHD image to the cloud. Microsoft Azure support only VHD, If you have image in the form of VHDX or any other format then you need to convert it before uploading.

Convert VHDX to VHD Using Powershell.
Copy paste the below commands in your powershell ISE.

Convert-VHD -Path "d:\sourcecimage.vhdx" -DestinationPath "Sourceimage.vhd"

Once the conversion completed. Upload it to your azure cloud.

Then Now we start the deployment from source image. Copy past the below command to you powershell ISE for deploy. Modify the Global variable as per your wish.

#Deploy New VM using Your custom VM
# Sign-in with Azure account credentials
Login-AzureRmAccount
# Select your Azure Subscription.
#Select your subscription ID
$subscriptionId =
    (Get-AzureRmSubscription |
     Out-GridView `
        -Title "Select an Azure Subscription ..." `
        -PassThru).SubscriptionId
Select-AzureRmSubscription `
    -SubscriptionId $subscriptionId
## Global Variables
$ResourceGroupName = "dstresourcegrp"
$location = "Southeastasia"
## Storage
$storageName = "dststorage"
# Machine Type you want to create.
$storageType = "Standard_A1"
## Network
$nicname = "dstnic"
$subnetName = "dstsubnet"
$vnetName = "dstvnet"
$vnetAddressPrefix = "10.0.0.0/16"
$vnetSubnetAddressPrefix = "10.0.0.0/24"
$publicIPName = "dstpip"
## Compute
$vmName = "dstvm"
$computerName = "dstcomputer"
$vmSize = "Standard_A1"
$osDiskName = $vmName + "osDisk"

#Create New Resouce Group
New-AzureRmResourceGroup -Name $ResourceGroupName -Location $location
#Create New Storage and put in variable
$storageAcc = New-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $storageName -SkuName "Standard_LRS" -Kind "Storage" -Location $location
#Create a virtual network
$singleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix $vnetSubnetAddressPrefix
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $ResourceGroupName -Location $location -AddressPrefix $vnetAddressPrefix -Subnet $singleSubnet
#Create a public IP address and network interface
$pip = New-AzureRmPublicIpAddress -Name $publicIPName -ResourceGroupName $ResourceGroupName -Location $location -AllocationMethod Dynamic
$nic = New-AzureRmNetworkInterface -Name $nicname -ResourceGroupName $ResourceGroupName -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
#Create a virtual machine
#The password must be at 12-123 characters long and have at least one lower case character, one upper case character, one number, and one special character.
$cred = Get-Credential -Message "Type the name and password of the local administrator account."
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $computerName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
#Create New Os Disk uri
$osDiskUri = "https://srcstorage.blob.core.windows.net/vhds/newosdisk.vhd"
#Image URI you can Find Storage group --> Blob --> System -->Microsoft.Compute -->Images --> your conntainer Name --> vhd name given while caputure VM.
$imageUri = "https://srcstorage.blob.core.windows.net/system/Microsoft.Compute/Images/customvhd/Lovlyimg-osDisk.a2d499b8-42b4-4fca-83cf-8403d759435b.vhd"
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $osDiskName -VhdUri $osDiskUri -CreateOption FromImage -SourceImageUri $imageUri  -Caching ReadWrite -Windows
#running below command you can see your VM attached parameters.
$vm
#Create the new VM
New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $location -VM $vm

Saturday, 22 October 2016

Create image from existing VM in Azure using Powershell.




If you want to create an image from you existing VM in azure. First thing you need to do is Login to you VM and syspre it.
Normally we need to create multiple copies of same set of installed softwares and other system setting we do imaging. Your first VM will be your model image for all VMs which you will be creating using one image.
Sysprep
Go to RUN type “Sysprep” without quotation. 
 




 
Double click on sysprep. 



Make sure Enter System Out-of-Box Experience (OOBE) Selected Generalize Check box checked and Shutdown option is selected. As mentioned above.
This will Clear all you SIDs and generalize your system and shutdown the VM.

Note: Once the system shutdown you can’t use it again that VM.



Now Open your Powershell ISE in Administrator mode. And past the below command.
Modify according to yourself in Global Variable session.
 ################################# Start####################################
# Capture VM from existing syspreped vm.
# Sign-in with Azure account credentials
Login-AzureRmAccount

# Select your Azure Subscription.
$subscriptionId =
    (Get-AzureRmSubscription |
     Out-GridView `
        -Title "Select an Azure Subscription ..." `
        -PassThru).SubscriptionId

Select-AzureRmSubscription `
    -SubscriptionId $subscriptionId

       
# Global Variables.

#Give resource grou name where your sysprep VM reside.
$ResourceGroupName = "srcresourcegrp"

## Computer
#Give your sysprep VM Name.
$vmName = "srcvm"
#Give Container Name where your image will be storing. any name you can give it will create its own.
$continerName = "customvhd"

#######################################################################################

# Deallocate the virtual machine
Stop-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $vmName

# Set the Generalized state to the virtual machine
Set-AzureRmVm -ResourceGroupName $ResourceGroupName -Name $vmName -Generalized

# Check the status.
$vm = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $vmName -status
$vm.Statuses
# Now capture the VM.
Save-AzureRmVMImage -ResourceGroupName $ResourceGroupName -VMName $vmName -DestinationContainerName $continerName -VHDNamePrefix 'Captured image' -Path C:\srcvm.json

################################### End #####################################


Get SSL Certificate Expiry Notification on Mail.

 There are multiple ways to get SSL Certification expiry details/alert. We all know if our SSL certificate get expire, how critical situatio...