Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Poor mans Azure IaaS templating-Deploy
# Read the contents
$pathToFile = "d:\somedir\somefile"
$vms = ConvertFrom-Json (Get-Content -Path $pathToFile | Out-String)
$vmsToBeCreated = @()
# Now loop over the objects and build the provisioning congs...
foreach ($vm in $vms)
{
$serviceName = $vm.ServiceName + "somenewsuffix"
$service = Get-AzureService -ServiceName $serviceName -ErrorAction SilentlyContinue
if ($service -eq $null)
{
New-AzureService -ServiceName $serviceName -Location $targetRegion
}
$vmName = $vm.Name
$instanceSize = $vm.InstanceSize
# Just to differentiate the availability set name with a naming convention
$availabilitySetName = $vm.AvailabilitySetName + "dr"
# Get the configuration set type of networkconfiguration.
$networkConfiguration = $vm.VM.ConfigurationSets | Where-Object {$_.ConfigurationSetType -eq "NetworkConfiguration"}
$subnetName = $null
$staticvnetIpAddress = ""
if ($networkConfiguration) {
$subnetName = $networkConfiguration.SubnetNames[0]
$staticvnetIpAddress = $networkConfiguration.StaticVirtualNetworkIPAddress
}
if ($availabilitySetName)
{
$vmConfig = New-AzureVMConfig -Name $vmName -InstanceSize $instanceSize -ImageName $imageName -AvailabilitySetName $availabilitySetName
}
else
{
$vmConfig = New-AzureVMConfig -Name $vmName -InstanceSize $instanceSize -ImageName $imageName
}
# Using the disableSSH flag here makes sure the default SSH endpoint does not get created
$vmConfig = $vmConfig |
Add-AzureProvisioningConfig -Linux -LinuxUser $userName -Password $password -DisableSSH
if ($subnetName)
{
$vmConfig = $vmConfig | Set-AzureSubnet -SubnetNames $subnetName
}
if ($staticvnetIpAddress)
{
$vmConfig = $vmConfig | Set-AzureStaticVNetIP -IPAddress $staticvnetIpAddress
}
foreach ($disk in $vm.VM.DataVirtualHardDisks)
{
if (!$disk.Disklabel)
{
$disk.Disklabel = "Data"
}
$vmConfig = $vmConfig | Add-AzureDataDisk -CreateNew -DiskSizeInGB $disk.LogicalDiskSizeInGB -DiskLabel $disk.Disklabel -LUN $disk.Lun
}
$vmsToBeCreated += $vmConfig
}
# At this point you have a set of VMs in the array $vmsToBeCreated and you can deploy them any place you want
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment