From 0cca9be107fa97189304fd98816e4879b6a2ce19 Mon Sep 17 00:00:00 2001 From: Howard Jones Date: Wed, 5 May 2021 10:04:44 +0100 Subject: [PATCH] Make CreateAzureVMFromPackerTemplate friendlier (#3209) * Don't assume JSON output Specify JSON output, since it's required. The default can be changed. * Make actions easier to track To make cleanup easier, use the same GUID for all components and report at the end on what was created. --- helpers/CreateAzureVMFromPackerTemplate.ps1 | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/helpers/CreateAzureVMFromPackerTemplate.ps1 b/helpers/CreateAzureVMFromPackerTemplate.ps1 index 683f2cf51..3206216e5 100644 --- a/helpers/CreateAzureVMFromPackerTemplate.ps1 +++ b/helpers/CreateAzureVMFromPackerTemplate.ps1 @@ -48,21 +48,22 @@ Function CreateAzureVMFromPackerTemplate { ) $vmSize = "Standard_DS2_v2" - $vnetName = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper() - $subnetName = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper() - $nicName = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper() - $publicIpName = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper() + $guid = [System.GUID]::NewGuid().ToString().ToUpper() + $vnetName = $env:UserName + "vnet-" + $guid + $subnetName = $env:UserName + "subnet-" + $guid + $nicName = $env:UserName + "nic-" + $guid + $publicIpName = $env:UserName + "pip-" + $guid Write-Host "Creating a virtual network and subnet" - ($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) + ($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) $subnetId = ($vnet | ConvertFrom-Json).newVNet.subnets[0].id Write-Host "`nCreating a network interface controller (NIC)" - ($nic = az network nic create -g $ResourceGroupName -l $AzureLocation -n $nicName --subnet $subnetId --subscription $subscriptionId) + ($nic = az network nic create -g $ResourceGroupName -l $AzureLocation -n $nicName --subnet $subnetId --subscription $subscriptionId -o json) $networkId = ($nic | ConvertFrom-Json).NewNIC.id Write-Host "`nCreating a public IP address" - ($publicIp = az network public-ip create -g $ResourceGroupName -l $AzureLocation -n $publicIpName --allocation-method Static --sku Standard --version IPv4 --subscription $subscriptionId) + ($publicIp = az network public-ip create -g $ResourceGroupName -l $AzureLocation -n $publicIpName --allocation-method Static --sku Standard --version IPv4 --subscription $subscriptionId -o json) $publicIpId = ($publicIp | ConvertFrom-Json).publicIp.id Write-Host "`nAdding the public IP to the NIC" @@ -70,4 +71,6 @@ Function CreateAzureVMFromPackerTemplate { Write-Host "`nCreating the VM" az group deployment create -g $ResourceGroupName -n $VirtualMachineName --subscription $subscriptionId --template-file $templateFilePath --parameters vmSize=$vmSize vmName=$VirtualMachineName adminUserName=$AdminUsername adminPassword=$AdminPassword networkInterfaceId=$networkId + + Write-Host "`nCreated in $(ResourceGroupName):`n vnet $(vnetName)`n subnet $(subnetName)`n nic $(nicName)`n publicip $(publicIpName)`n vm $(VirtualMachineName)" }