Skip to content

Instantly share code, notes, and snippets.

@Ercenk
Created May 7, 2015 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ercenk/2bdf1bde9b70e5ebe10b to your computer and use it in GitHub Desktop.
Save Ercenk/2bdf1bde9b70e5ebe10b to your computer and use it in GitHub Desktop.
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