Azure VM creation through Powershell and Jenkins

We have discussed Azure Image creation in our last article on Azure VM Image creation through Powershell

Now, lets create the Azure VM with Azure Image ….

Azure-Jenkins-and-Powershell

I am providing you my Powershell script which you can first edit according to your configuration and then configure it as a Jenkins job to create a Azure VM through Jenkins

# Powershell script to create Windows VM  R2 Datacenter-2016 through Azure Custom Generalized VM Image - RV


# Provide the Image URI (as discussed in last article - found through Storage Locator of the Image VM)
$imageURI = "https://winvmresgpdiskstorage.blob.core.windows.net/system/Microsoft.Compute/Images/vhds/Masterclonevhd-osDisk.c2b7665a-3613-4613-8ac0-3c70f8bc47ee.vhd"

 # Name of the resource group name
 $rgName =  "win-vm-resource-gp"
 

 $subnetName = "default" # You can define yours
 
 $singleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix 10.1.0.0/24

 $location = "UK West" # You can use yours

 $vnetName = "win-vm-resource-gp-vnet"  # You can define yours
 
 $vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName -Location $location -AddressPrefix 10.1.0.0/16 -Subnet $singleSubnet -Force


 $ipName = "winvm04-IP" # You can define yours


$pip = New-AzureRmPublicIpAddress -Name $ipName -ResourceGroupName $rgName -Location $location -AllocationMethod Static -Force


$nicName = "winvm04-nic" # You can define yours

$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -Force

# Automation for getting the user credentials
    $username = "win-admin" # You can define yours
    # You need to export your password in one file and then convert here, if you want you can use plaintext as well but that is not secure
    $password = cat C:\vm_secure_string_do_not_delete.txt | convertto-securestring 

    $creden = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

    $cred = Get-Credential -Credential $creden

    # Name of the storage account where the VHD is located. This example sets the 
    # storage account name as "myStorageAccount"
    $storageAccName = "winvmresgpdiskstorage" # You can define yours

    # Name of the virtual machine. This example sets the VM name as "myVM".
    $vmName = "winvm04" # You can define yours

    # Size of the virtual machine. This example creates "Standard_D2_v2" sized VM. 
    # See the VM sizes documentation for more information: 
    # https://azure.microsoft.com/documentation/articles/virtual-machines-windows-sizes/
    $vmSize = "Standard_DS2_v2_Promo"

    # Computer name for the VM. This examples sets the computer name as "myComputer", You can define yours
    $computerName = "winvm04" 

    # Name of the disk that holds the OS. This example sets the 
    # OS disk name as "myOsDisk", you can define yours
    $osDiskName = "WinOsDisk" 

    # Assign a SKU name. This example sets the SKU name as "Standard_LRS"
    # Valid values for -SkuName are: Standard_LRS - locally redundant storage, Standard_ZRS - zone redundant
    # storage, Standard_GRS - geo redundant storage, Standard_RAGRS - read access geo redundant storage,
    # Premium_LRS - premium locally redundant storage. 
    $skuName = "Standard_LRS"

    # Get the storage account where the uploaded image is stored
    $storageAcc = Get-AzureRmStorageAccount -ResourceGroupName $rgName -AccountName $storageAccName

    # Set the VM name and size
    $vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize

    #Set the Windows operating system configuration and add the NIC
    $vm = Set-AzureRmVMOperatingSystem -VM $vmConfig -Windows -ComputerName $computerName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate

    $vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id

    # Create the OS disk URI
    $osDiskUri = '{0}vhds/{1}-{2}.vhd' -f $storageAcc.PrimaryEndpoints.Blob.ToString(), $vmName.ToLower(), $osDiskName

    # Configure the OS disk to be created from the existing VHD image (-CreateOption fromImage).
    $vm = Set-AzureRmVMOSDisk -VM $vm -Name $osDiskName -VhdUri $osDiskUri -CreateOption fromImage -SourceImageUri $imageURI -Windows

    # Create the new VM
    New-AzureRmVM  -ResourceGroupName $rgName -Location $location -VM $vm


    echo "VM Provisioning Successfully Completed !!"
    echo ""
    echo "VMNAME is $vmName"
    echo ""
    echo "IP Address is" $pip.IpAddress

 

You can configure the Jenkins Job by adding a “Build” step and choose “Windows Powershell

Provide your script name along with full path in Command box

And then trigger the Jenkins Job …..

Jenkins-Powershell-Azure-VM-Creation-3

Once triggered, You can then verify the same in Azure portal as well.

Jenkins-Powershell-Azure-VM-Creation-1

 

And check the Jenkins Console Output

Jenkins-Powershell-Azure-VM-Creation

Once Jenkins job is completed, you can cross-verify in Azure portal.

Jenkins-Powershell-Azure-VM-Creation4

That’s all. You have now successfully created an Azure VM through Jenkins 🙂

We will shortly back with AWS stuff, till then stay tuned ….

3 thoughts on “Azure VM creation through Powershell and Jenkins”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Scroll to Top