From 1989cb28bc52a5d4a50b5dee5e232487f2ecada2 Mon Sep 17 00:00:00 2001 From: Damon Barry Date: Fri, 14 Aug 2020 04:14:28 -0700 Subject: [PATCH] Use full resource IDs and clean up output (#1370) --- helpers/CreateAzureVMFromPackerTemplate.ps1 | 25 +++++++++++---------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/helpers/CreateAzureVMFromPackerTemplate.ps1 b/helpers/CreateAzureVMFromPackerTemplate.ps1 index 8c3f1ccb..0aa95b04 100644 --- a/helpers/CreateAzureVMFromPackerTemplate.ps1 +++ b/helpers/CreateAzureVMFromPackerTemplate.ps1 @@ -17,7 +17,7 @@ Function CreateAzureVMFromPackerTemplate { .PARAMETER VirtualMachineName The name of the virtual machine to be generated. - + .PARAMETER AdminUserName The administrator username for the virtual machine to be created. @@ -53,20 +53,21 @@ Function CreateAzureVMFromPackerTemplate { $nicName = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper() $publicIpName = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper() - Write-Host "Creating a Vnet and a Subnet" - az network vnet create -g $ResourceGroupName -l $AzureLocation --name $vnetName --address-prefix 10.0.0.0/16 --subscription $subscriptionId - az network vnet subnet create -g $ResourceGroupName --vnet-name $vnetName -n $subnetName --address-prefix 10.0.1.0/24 --subscription $subscriptionId + 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) + $subnetId = ($vnet | ConvertFrom-Json).newVNet.subnets[0].id - Write-Host "Creating a network interface card (NIC)." - $nic = az network nic create -g $ResourceGroupName --vnet-name $vnetName --subnet $subnetName -n $nicName --subscription $subscriptionId + Write-Host "`nCreating a network interface controller (NIC)" + ($nic = az network nic create -g $ResourceGroupName -l $AzureLocation -n $nicName --subnet $subnetId --subscription $subscriptionId) $networkId = ($nic | ConvertFrom-Json).NewNIC.id - Write-Host "create public IP." - az network public-ip create -g $ResourceGroupName -n $publicIpName --subscription $subscriptionId --allocation-method Static --location $AzureLocation --sku Standard --version IPv4 + 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) + $publicIpId = ($publicIp | ConvertFrom-Json).publicIp.id - Write-Host "Adding the public IP to the NIC." - az network nic ip-config update --name ipconfig1 --nic-name $nicName --resource-group $ResourceGroupName --subscription $subscriptionId --public-ip-address $publicIpName + Write-Host "`nAdding 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 - Write-Host "Creating the VM" - az group deployment create --resource-group $ResourceGroupName --subscription $subscriptionId --name $VirtualMachineName --template-file $templateFilePath --parameters vmSize=$vmSize vmName=$VirtualMachineName adminUserName=$AdminUsername adminPassword=$AdminPassword networkInterfaceId=$networkId + 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 }