This article is more than 1 year old

Want to deploy virtual machines in a hurry? PowerShell is your friend

Improve your automation

Deploying to Azure

These scripts are great and I love them to bits. They automate quite a lot of the manual tasks I do when testing in my lab and save a huge amount of time.

There is a rather large elephant in the room, though. I am regularly asked to test deployments and migrations to Azure as well. Luckily, I happen to have a PowerShell script for that too.

Param(      [Parameter(Mandatory=$true)][String]$subscriptionName,     [Parameter(Mandatory=$true)][String]$storageAccountName,     [Parameter(Mandatory=$False)][String]$region,     [Parameter(Mandatory=$False)][String]$instanceSize,     [Parameter(Mandatory=$False)][String]$serviceName,     [Parameter(Mandatory=$False)][String]$vmname,     [Parameter(Mandatory=$False)][String]$localosvhdname,     [Parameter(Mandatory=$False)][String]$localdatavhdname      #Add additional params such as memory settings ) # NOTES:   # Retrieve $region with Get-AzureLocation if you are unsure $region Options: West US, Central US, East US, North Central US, South Central US, North Europe, West Europe, East Asia, Southeast Asia, Japan East, Japan West, Brazil South, Australia East, Australia Southeast

# $instanceSize Options: ExtraSmall, Small, Medium, Large, ExtraLarge   # $serviceName has to be a unique name. Verify with Test-AzureService #>   # Specify the storage account location to store the newly created VHDs Set-AzureSubscription -SubscriptionName $subscriptionName -CurrentStorageAccount $storageAccountName

#Set Source VHD Filepaths $vhdfilepath = 'E:\VMs\MigrateToAzure\' + $vmname + '\' + $localosvhdname + '_Disk.vhdx' $datavhdfilepath = 'E:\VMs\MigrateToAzure\' + $vmname + '\' + $localdatavhdname + '_Disk.vhdx'

# Set Target Upload Location $destinationOS = 'http://' + $storageAccountName + '.blob.core.windows.net/uploads/' + $($localosvhdname) + '_Disk.vhdx' $destinationData = 'http://' + $storageAccountName + '.blob.core.windows.net/uploads/' + $($localdatavhdname) + '_Disk.vhdx'   Add-AzureVhd -LocalFilePath $vhdfilepath -Destination $destinationOS Add-AzureVhd -LocalFilePath $datavhdfilepath -Destination $destinationData   Add-AzureDisk -OS Windows -MediaLocation $destinationOS -DiskName $localosvhdname Add-AzureDisk -MediaLocation $destinationData -DiskName $localdatavhdname   $migratedVM = New-AzureVMConfig -Name $vmname -DiskName "$($localosvhdname).VHDX" -InstanceSize $instanceSize | Add-AzureDataDisk -Import -DiskName "$($localdatavhdname).vhdx" -LUN 0 | Add-AzureEndpoint -Name 'Remote Desktop' -LocalPort 3389 -Protocol tcp   New-AzureVM -ServiceName $serviceName -Location $location -VMs $migratedVM

For this script to function successfully you will need to have already signed up an Azure account, installed and configured Azure PowerShell (details here) and be prepared to wait while your VHDXs upload to Azure.

The workflow for this script is different as well. With our local scripts we were working with golden image virtual machines we had created specifically for rapid deployment. Azure already has rapid deployment golden images ready to go.

During testing, the longest I have ever had to wait for Azure to provision one of its own golden images was about 30 minutes. It takes far less time for Azure to provision one of its own vanilla golden images and for us to then customise it than to upload our own as needed.

Much like local Hyper-V, on Azure we can also save our own customised versions of the preconfigured Azure golden images for use instead of the vanilla ones.

Given that you would probably not use the Azure migration script uploading your own golden images this script doesn’t feature in a rapid deployment scenario. I envision using it in my test lab for a monthly backup of infrastructure virtual machines or as part of a large-scale migration project.  

Conclusion

PowerShell is a very powerful tool that many administrators are not using to anywhere near its full potential.

Using PowerShell Scripting to automate rapid virtual machine deployment in Hyper-V is really quite simple to set up. Once you have got it going, you will wonder why you ever did it any other way. ®

More about

TIP US OFF

Send us news


Other stories you might like