Individual deployments

Every resource can also be deployed and managed with the Azure PowerShell cmdlets, for the different resource types. While resource group templates have many advantages and should be used when possible, they are hard to create from scratch without an editor that can insert new resources in a wizard-like fashion, such as VSCode or Visual Studio.

So, for a script-based workflow, building your workloads from scratch is likely the easier solution. However, it requires you to check for the existence of each of your resources before creating them, whereas an incremental template deployment simply adds the new resources.

In order to deploy a simple virtual machine, you need many components—a resource group, a storage account for your disks, a network adapter, a public IP address, and lastly, the virtual machine. Review the following code sample for the basic components and parameters for an ad-hoc deployment:

# Parameters
$resourceGroupName = 'Blau'
$storageAccountName = "contoso$((1..8 | ForEach-Object { [char[]](97..122) | Get-Random }) -join '')"
$location = 'westeurope'
$vmName = 'MyFirstVm'
$roleSize = 'Standard_DS2'
$cred = Get-Credential

# Resource group and storage
New-AzureRmResourceGroup -Name $resourceGroupName -Location $location
New-AzureRmStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName -SkuName Standard_LRS -Location $location
$storageContext = (Get-AzureRmStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroupName).Context
$container = New-AzureStorageContainer -Name disks -Context $storageContext
$rnd = (Get-Random -Minimum 1 -Maximum 1000).ToString('0000')

After initializing the resource group and storage account, the network settings can be configured. We will create a new virtual network and add the subnet configuration to it. After adding a subnet configuration, it is necessary to update the resource, as you can see in the following code sample.

New-AzureRmVirtualNetwork -Name $resourceGroupName -ResourceGroupName $resourceGroupName -Location $location -AddressPrefix "10.0.0.0/16" 
Get-AzureRmVirtualNetwork -Name $resourceGroupName -ResourceGroupName $resourceGroupName |
Add-AzureRmVirtualNetworkSubnetConfig -Name someSubnet -AddressPrefix '10.0.0.0/24' |
Set-AzureRmVirtualNetwork

$subnet = Get-AzureRmVirtualNetwork -Name $resourceGroupName -ResourceGroupName $resourceGroupName |
Get-AzureRmVirtualNetworkSubnetConfig

Once the network configuration is done, the VM configuration can follow, and we can use properties that have been set previously. After assigning the desired image SKU and network interface to the VM, the VM creation can begin. Take a look at the next code sample, where we create all necessary resources. The sample culminates in New-AzureRmVm, which was the main reason why we did all this work.

$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $RoleSize -ErrorAction Stop -WarningAction SilentlyContinue
$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate -ErrorAction Stop -WinRMHttp
$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName 'MicrosoftWindowsServer' -Offer WindowsServer -Skus 2016-Datacenter -Version "latest" -ErrorAction Stop -WarningAction SilentlyContinue

$networkInterface = New-AzureRmNetworkInterface -Name VmNic -ResourceGroupName $resourceGroupName -Location $location -Subnet $subnet
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $networkInterface.Id -ErrorAction Stop -WarningAction SilentlyContinue

$DiskName = "$($vmName)_os"
$OSDiskUri = "$($StorageContext.BlobEndpoint)disks/$DiskName.vhd"
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $DiskName -VhdUri $OSDiskUri -CreateOption fromImage -ErrorAction Stop -WarningAction SilentlyContinue

$vmParameters = @{
ResourceGroupName = $ResourceGroupName
Location = $Location
VM = $vm
ErrorAction = 'Stop'
WarningAction = 'SilentlyContinue'
}
New-AzureRmVM @vmParameters

# Examine your VM, add a public IP, allow WinRM traffic, ...

# Once finished, clean up
Remove-AzureRmResourceGroup $resourceGroupName -Force
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset