1

I want to create a VM Scale set and use a snapshot as base for my windows VMs. As the Set-AzureRmVmssStorageProfile only accepts images my first try was to convert the snapshot to an image by use:

$rgName = #...
$location = #...
$snapshotName = "mySnapshot"
$imageName = "myImage"

$snapshot = Get-AzureRmSnapshot -ResourceGroupName $rgName -SnapshotName $snapshotName

$imageConfig = New-AzureRmImageConfig -Location $location
$imageConfig = Set-AzureRmImageOsDisk -Image $imageConfig -OsState Generalized -OsType Windows -SnapshotId $snapshot.Id

New-AzureRmImage -ImageName $imageName -ResourceGroupName $rgName -Image $imageConfig

But in this case the image that is created has no Source Blob URI:

enter image description here

what gives me the error:

New-AzureRmVmss : The URI Microsoft.Azure.Commands.Compute.Automation.Models.PSImage does not look to be correct blob URI.

On my deploy commands for azure:

$vmss = New-AzureRmVmssConfig -Location $loc -SkuCapacity 2 -SkuName "Standard_DS1_v2" -UpgradePolicyMode "manual" -ErrorAction Stop

Add-AzureRmVmssNetworkInterfaceConfiguration -VirtualMachineScaleSet $vmss -Name "vmssNetwork" -Primary $true -IPConfiguration $ipConfig 
Set-AzureRmVmssStorageProfile -VirtualMachineScaleSet $vmss -OsDiskCreateOption "FromImage" -OsDiskCaching "None" `
            -Image $ImgRef -OsDiskOsType Windows -OsDiskName "C"
Set-AzureRmVmssOSProfile -ComputerNamePrefix $vmNamePrefix -AdminUsername $adminUsername -AdminPassword $adminPassword -VirtualMachineScaleSet $vmss

New-AzureRmVmss -ResourceGroupName $currentrg -Name $vmssName -VirtualMachineScaleSet $vmss -Verbose -ErrorAction Stop;

Is there a other way to create the image or set a source blob uri? Or is it possible to use a snapshot for creating an VM Scale Set?

-- Edit 1 --

Afer the hint from Charles Xu I changhed the image creation to first create a dik, but I still get the same error. New Code is:

$rgName = #...
$location = #...
$snapshotName = "mySnapshot"
$imageName = "myImage"
$storageType = 'Standard_LRS'
$diskName = "myDisk"

$snapshot = Get-AzureRmSnapshot -ResourceGroupName $rgName -SnapshotName $snapshotName

$diskConfig = New-AzureRmDiskConfig -AccountType $storageType -Location $location -CreateOption Copy -SourceResourceId $snapshot.Id
$disk = New-AzureRmDisk -Disk $diskConfig -ResourceGroupName $rgName -DiskName $diskName

$imageConfig = New-AzureRmImageConfig -Location $location
$imageConfig = Set-AzureRmImageOsDisk -Image $imageConfig -OsState Generalized -OsType Windows -ManagedDiskId $disk.Id     

New-AzureRmImage -ImageName $imageName -ResourceGroupName $rgName -Image $imageConfig
Daniel W.
  • 938
  • 8
  • 21

2 Answers2

1

You can create an image from an Azure VM. For example, you can create a windows image from the windows VM through PowerShell, see Create and use a custom image for virtual machine scale sets with Azure PowerShell. When the image is OK, Just create the VMSS like this:

New-AzureRmVmss `
  -ResourceGroupName "myResourceGroup" `
  -Location "EastUS" `
  -VMScaleSetName "myScaleSet" `
  -VirtualNetworkName "myVnet" `
  -SubnetName "mySubnet" `
  -PublicIpAddressName "myPublicIPAddress" `
  -LoadBalancerName "myLoadBalancer" `
  -UpgradePolicyMode "Automatic" `
  -ImageName "yourImage"

Also, the snapshot is OK, but you should create the image from the snapshot first. And then create the VMSS from the image. With the command New-AzureRmImage, the image should be a managed image, so you cannot see the URI. Just use the managed image Id in the command like this:

Set-AzureRmVmssStorageProfile -VirtualMachineScaleSet $vmss -OsDiskCreateOption "FromImage" -OsDiskCaching "None" -ImageReferenceId yourImageId -OsDiskOsType Windows -OsDiskName "C"

And just talk about how to create images, I would suggest you the Packer and there is an example here.

Update

I assume that your custom image is prepared. And the PowerShell script like this:

#Get the custom image 
$image = Get-AzureRmImage -ResourceGroupName charlesTerraform -ImageName myPackerImage

# Get the existing Vnet
$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName charlesTerraform -Name pakcerVnet

#IP configuration
$ipName = "ipConfig"

#create the IP configuration
$ipConfig = New-AzureRmVmssIpConfig -Name $ipName -LoadBalancerBackendAddressPoolsId $null -SubnetId $vnet.Subnets[0].Id

#create vmss configuration
$vmss = New-AzureRmVmssConfig -Location "East US" -SkuCapacity 2 -SkuName "Standard_DS1_v2" -UpgradePolicyMode "manual" -ErrorAction Stop

##Add the network interface configuration to the scale set configuration
Add-AzureRmVmssNetworkInterfaceConfiguration -VirtualMachineScaleSet $vmss -Name "vmssNetwork" -Primary $true -IPConfiguration $ipConfig 

# set the stroage profile 
Set-AzureRmVmssStorageProfile -VirtualMachineScaleSet $vmss -OsDiskCreateOption "FromImage" -OsDiskCaching "None" -ImageReferenceId $image.Id -OsDiskOsType Linux 

#set the os profile
Set-AzureRmVmssOSProfile -ComputerNamePrefix "Test" -AdminUsername "azureuser" -AdminPassword "azureuser@2018" -VirtualMachineScaleSet $vmss

#create the vmss
New-AzureRmVmss -ResourceGroupName charlesTerraform -Name TestVmss -VirtualMachineScaleSet $vmss
Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • Use a Image direkt from AzureVm is not a real option for me. First create Disk does not work, see edit in the post. – Daniel W. Nov 26 '18 at 08:32
  • The disk I said in the answer means a managed image. It's impossible if you want to create VMSS from a snapshot directly. You must create an image from a snapshot through the command `New-AzureRmImage`. Then use the command `New-AzureRmVmss` with the parameter `ImageName` to use the image. You could take a look at the link about custom image. – Charles Xu Nov 26 '18 at 08:41
  • Ok sad but like my sample code shows, I try create the image from the snapshot. But I can not use New-AzureRmVmss with ImageName because I need to go by use of New-AzureRmVmssConfig because I need to use a existing Network. And in the Parameter Set I can not use ImageName if I set VirtualMachineScaleSet parameter. So I have to use Set-AzureRmVmssStorageProfile. – Daniel W. Nov 26 '18 at 09:40
  • Maybe you can try the command `Set-AzureRmVmssStorageProfile` with the parameter `-ImageReferenceId`, without other image parameters. – Charles Xu Nov 26 '18 at 09:45
  • For example, `Set-AzureRmVmssStorageProfile -VirtualMachineScaleSet $vmss -OsDiskCreateOption "FromImage" -OsDiskCaching "None" -ImageReferenceId yourImageId -OsDiskOsType Windows -OsDiskName "C"`. – Charles Xu Nov 26 '18 at 09:49
  • In this Case I get the error: New-AzureRmVmss : VM created from Image cannot have blob based disks. All disks have to be managed disks. Dis I miss something in my create script from Edit 1? – Daniel W. Nov 26 '18 at 11:27
  • I said in the answer. You cannot see the URI of a managed image in the Azure portal. You can just create the VMSS with the image ID. – Charles Xu Nov 26 '18 at 13:49
  • It is ok, to not see the URI, but I can not use it for create the Vmss because I get the error New-AzureRmVmss : VM created from Image cannot have blob based disks. All disks have to be managed disks. – Daniel W. Nov 26 '18 at 14:06
  • You must not understand my answer and comment clearly. Please read them carefully again. – Charles Xu Nov 27 '18 at 00:54
  • It seams the Problem was that I used -ImageReferenceId and -Image parallel, if I only use -ImageReferenceId I raun into a timeout but I think that has nothing to do with the Image. I'm chcking the generalisation to see if it works correct. – Daniel W. Nov 28 '18 at 06:55
  • @DanielW. I did the test and it works well without any error. You can take a look. – Charles Xu Nov 28 '18 at 08:51
  • Do you solve the problem with the answer? Or need more help? – Charles Xu Nov 29 '18 at 13:13
  • Still getting error `New-AzureRmVmss : Long running operation failed with status 'Failed'. Additional Info:'OS Provisioning for VM 'haufevmssgreenbg_0' did not finish in the allotted time. This error occurred too many times consecutively from image 'myImage'. Make sure the image has been properly prepared (generalized).` But I think that has only something to do with my snapshop. (I used sysprep to generalize) – Daniel W. Nov 30 '18 at 11:24
  • Soved, I needed to update to newest azure rm powershell module – Daniel W. Dec 18 '18 at 07:47
0

folks. So what about create a "Virtual Machine in Scale Set" from a customized snapshot. Is this possible or one scale set could only have one base image and all vms under this scale set could only reimaged to the same baseline ?

dave zhou
  • 59
  • 2
  • 2
  • 5
  • I only know the workaround with first creating an image from the snapshot, I would also find it nice to have a direct way. – Daniel W. Dec 18 '18 at 07:48