2019-12-13 09:48:00 -05:00
Function CreateAzureVMFromPackerTemplate {
<#
. SYNOPSIS
2020-08-24 12:31:51 +03:00
A helper function to deploy a VM from a generated image .
2019-12-13 09:48:00 -05:00
. DESCRIPTION
2020-08-24 12:31:51 +03:00
Creates an Azure VM from a template . Also generates network resources in Azure to make the VM accessible .
2019-12-13 09:48:00 -05:00
. PARAMETER SubscriptionId
The Azure subscription Id where resources will be created .
. PARAMETER ResourceGroupName
The Azure resource group name where the Azure virtual machine will be created .
2023-08-31 16:21:15 +02:00
. PARAMETER ManagedImageName
The name of the managed image to be used to create the virtual machine .
2019-12-13 09:48:00 -05:00
. PARAMETER VirtualMachineName
The name of the virtual machine to be generated .
2020-08-14 04:14:28 -07:00
2019-12-13 09:48:00 -05:00
. PARAMETER AdminUserName
The administrator username for the virtual machine to be created .
. PARAMETER AdminPassword
The administrator password for the virtual machine to be created .
. PARAMETER AzureLocation
The location where the Azure virtual machine will be provisioned . Example: "eastus"
. EXAMPLE
2024-01-15 16:40:23 +01:00
CreateAzureVMFromPackerTemplate -SubscriptionId {SubscriptionId} -ResourceGroupName {ResourceGroupName} -VirtualMachineName "testvm1" -ManagedImageName {ManagedImageName} -AdminUsername "shady1" -AdminPassword "SomeSecurePassword1" -AzureLocation "eastus"
2019-12-13 09:48:00 -05:00
#>
param (
[ Parameter ( Mandatory = $True ) ]
[ string ] $SubscriptionId ,
[ Parameter ( Mandatory = $True ) ]
[ string ] $ResourceGroupName ,
[ Parameter ( Mandatory = $True ) ]
2023-08-31 16:21:15 +02:00
[ string ] $ManagedImageName ,
2019-12-13 09:48:00 -05:00
[ Parameter ( Mandatory = $True ) ]
[ string ] $VirtualMachineName ,
[ Parameter ( Mandatory = $True ) ]
[ string ] $AdminUsername ,
[ Parameter ( Mandatory = $True ) ]
[ string ] $AdminPassword ,
[ Parameter ( Mandatory = $True ) ]
[ string ] $AzureLocation
)
$vmSize = " Standard_DS2_v2 "
2021-05-05 10:04:44 +01:00
$guid = [ System.GUID ] :: NewGuid ( ) . ToString ( ) . ToUpper ( )
$vnetName = $env:UserName + " vnet- " + $guid
$subnetName = $env:UserName + " subnet- " + $guid
$nicName = $env:UserName + " nic- " + $guid
2023-08-31 16:21:15 +02:00
$publicIpName = $env:UserName + " pip- " + $guid
2019-12-13 09:48:00 -05:00
2020-08-14 04:14:28 -07:00
Write-Host " Creating a virtual network and subnet "
2021-05-05 10:04:44 +01:00
( $vnet = az network vnet create -g $ResourceGroupName -l $AzureLocation -n $vnetName - -address -prefixes 10.0 . 0 . 0 / 16 - -subnet -name $subnetName - -subnet -prefixes 10.0 . 1 . 0 / 24 - -subscription $subscriptionId -o json )
2020-08-14 04:14:28 -07:00
$subnetId = ( $vnet | ConvertFrom-Json ) . newVNet . subnets [ 0 ] . id
2019-12-13 09:48:00 -05:00
2020-08-14 04:14:28 -07:00
Write-Host " `n Creating a network interface controller (NIC) "
2021-05-05 10:04:44 +01:00
( $nic = az network nic create -g $ResourceGroupName -l $AzureLocation -n $nicName - -subnet $subnetId - -subscription $subscriptionId -o json )
2019-12-13 09:48:00 -05:00
$networkId = ( $nic | ConvertFrom-Json ) . NewNIC . id
2020-08-14 04:14:28 -07:00
Write-Host " `n Creating a public IP address "
2022-06-23 12:58:16 +02:00
( $publicIp = az network public-ip create -g $ResourceGroupName -l $AzureLocation -n $publicIpName - -allocation -method Static - -sku Basic - -version IPv4 - -subscription $subscriptionId -o json )
2020-08-14 04:14:28 -07:00
$publicIpId = ( $publicIp | ConvertFrom-Json ) . publicIp . id
2019-12-13 09:48:00 -05:00
2020-08-14 04:14:28 -07:00
Write-Host " `n Adding the public IP to the NIC "
az network nic ip-config update -g $ResourceGroupName -n ipconfig1 - -nic -name $nicName - -public -ip -address $publicIpId - -subscription $subscriptionId
2019-12-13 09:48:00 -05:00
2020-08-14 04:14:28 -07:00
Write-Host " `n Creating the VM "
2023-08-31 16:21:15 +02:00
az vm create `
- -resource -group $ResourceGroupName `
- -name $VirtualMachineName `
- -image $ManagedImageName `
- -size $vmSize `
- -admin -username $AdminUsername `
- -admin -password $AdminPassword `
- -nics $networkId `
- -subscription $subscriptionId `
- -location $AzureLocation
2022-06-23 12:58:16 +02:00
2022-02-18 08:22:33 +01:00
Write-Host " `n Created in ${ResourceGroupName} : `n vnet ${vnetName} `n subnet ${subnetName} `n nic ${nicName} `n publicip ${publicIpName} `n vm ${VirtualMachineName} "
2019-12-13 09:48:00 -05:00
}