How to create a VM scale set from a Snapshot - azure

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:
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

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

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 ?

Related

Azure VMs fails as public ip is allocated to other resource

I am using a powershell script to create multiple Vms based on an image. The first Vm is ok but when attempting the second Vm I get an error saying that :
| Resource /subscriptions/....../networkInterfaces/xxxxx/ipConfigurations/xxxxx is referencing public IP address
| /subscriptions/xxxxxxxxx/providers/Microsoft.Network/publicIPAddresses/Microsoft.Azure.Commands.Network.Models.PSPublicIpAddress that is already allocated to
| resource /subscriptions/......./networkInterfaces/xxxxx/ipConfigurations/xxxxx.
Here is the script I am using:
param(
[string] $WeekNo="NoWeek",
[int] $VmCount=0
)
#$cred = Get-Credential -Message "Enter a username and password for the virtual machine."
## VM Account
# Credentials for Local Admin account you created in the sysprepped (generalized) vhd image
$VMLocalAdminUser = "xxxxx"
$VMLocalAdminSecurePassword = ConvertTo-SecureString "xxxxxxx" -AsPlainText -Force
$image = "/subscriptions/xxxxxxx/resourceGroups/xxxxxx/providers/Microsoft.Compute/images/xxxxxxxxx"
## Azure Account
$LocationName = "SwedenCentral"
$ResourceGroupName = "xxxx_" + $WeekNo
if( -Not( Get-AzureRmResourceGroup -Name $ResourceGroupName -Location $LocationName -ErrorAction Ignore)) {
New-AzureRmResourceGroup -Name $ResourceGroupName -Location $LocationName
Write-Host "ResourceGroup" $ResourceGroupName "created"
$VMSize = "Standard_B2ms"
## Networking
$NetworkName = "xxxxxx_" + $WeekNo + "_net" # "MyNet"
$SubnetName = "MySubnet"
$SubnetAddressPrefix = "10.0.0.0/24"
$VnetAddressPrefix = "10.0.0.0/16"
$SingleSubnet = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $SubnetAddressPrefix
$Vnet = New-AzVirtualNetwork -Name $NetworkName -ResourceGroupName $ResourceGroupName -Location $LocationName -AddressPrefix $VnetAddressPrefix -Subnet $SingleSubnet
}
$Credential = New-Object System.Management.Automation.PSCredential ($VMLocalAdminUser, $VMLocalAdminSecurePassword);
$VMName = "xxxx" + $WeekNo
##New-AzVM -ResourceGroupName $ResourceGroupName -Location $LocationName -VM $VirtualMachine -Verbose -Image $image
for($i=1; $i -le $VmCount; $i++){
$VMBaseName = "iCPSEDU" + $WeekNo + $i
$StorageAccount = "xxxxx" + $WeekNo + $i
$PublicIPAddressName = $VMBaseName + "PIP$(Get-Random)"
$NICName = $VMBaseName + "NIC"
$DNSNameLabel = "xxxx" + $WeekNo + $i + "dns" # mydnsname.westus.cloudapp.azure.com
$PIP = New-AzPublicIpAddress -Name $PublicIPAddressName -DomainNameLabel $DNSNameLabel -ResourceGroupName $ResourceGroupName -Location $LocationName -AllocationMethod Dynamic
$NIC = New-AzNetworkInterface -Name $NICName -ResourceGroupName $ResourceGroupName -Location $LocationName -SubnetId $Vnet.Subnets[0].Id -PublicIpAddressId $PIP.Id
Write-Host "Creating VM " $VMBaseName
New-AzVm `
-ResourceGroupName $ResourceGroupName `
-Name $VMBaseName `
-ImageName $image `
-Location $LocationName `
-VirtualNetworkName $Vnet `
-SubnetName $SubnetName `
-SecurityGroupName "myImageNSG" `
-PublicIpAddressName $PIP -Credential $Credential -Size $VMSize -PublicIpSku Standard
Write-Host "VM " $VMBaseName " Created"
Stop-AzVM -ResourceGroupName $ResourceGroupName $VMBaseName -Force -NoWait
Write-Host "VM " $VMBaseName " Stopped"
}
Write-Host "Done."`
To me it seems that the variable used for the PIP is not "flushed" properly between the executions but I have no idea on how to do this?
Or is there something else causing the error?
I have tried adding some delays but without effect.
Create a public IP address and specify a DNS name
Create a NSG
Create a NIC and associate with created pub IP address and NSG
Create a virtual machine configuration and assign the NIC
Create the VM with the config
https://github.com/Azure/azure-docs-powershell-samples/blob/master/virtual-machine/create-vm-detailed/create-windows-vm-detailed.ps1
rough summary of important steps:
$pip = New-AzPublicIpAddress -ResourceGroupName $resourceGroup -Location $location `
-Name "mypublicdns$(Get-Random)" -AllocationMethod Static -IdleTimeoutInMinutes 4
$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $resourceGroup -Location $location `
-Name myNetworkSecurityGroup -SecurityRules $nsgRuleRDP
$nic = New-AzNetworkInterface -Name myNic -ResourceGroupName $resourceGroup -Location $location `
-SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id
$vmConfig = New-AzVMConfig -VMName $vmName -VMSize Standard_D1 | `
Set-AzVMOperatingSystem -Windows -ComputerName $vmName -Credential $cred | `
Set-AzVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2016-Datacenter -Version latest | `
Add-AzVMNetworkInterface -Id $nic.Id
New-AzVM -ResourceGroupName $resourceGroup -Location $location -VM $vmConfig
MS is providing well tested powershell code for various tasks:
I prefer the github samples https://github.com/Azure/azure-docs-powershell-samples over the steps in learn and doc.microsoft.com
also have a deeper look at the Azure CLI examples and template based deployments. It seems to me that MS is abandoning PS a bit.

Azure Funtion: Powershell command New-AzureRmVM never returns

I've got a powershell script that I use to build a VM.
# Variables for common values
$resourceGroup = "AAA-Production3"
$location = "West US 2"
$vmName = "AAA-Prod-SVR1"
$SubnetName = "AAA-PROD-SUBNET01"
$NamevNET = "AAA-PROD-VNET"
$Namepublicdns = "AAA-PROD-ADF01-IP01"
$NameNetworkSecurityGroupRuleRDP = 'Default-allow-rdp'
$NameNetworkSecurityGroup = 'AAA-SVR1-NSG'
$NameVNic = "AAA-PROD-VNIC01"
$VMSize = 'Standard_D1_v2'
# Create user object
$cred = Get-Credential -Message "Enter a username and password for the virtual machine."
New-AzureRmResourceGroup -Name $resourceGroup -Location $location
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix 10.50.1.0/24
$vnet = New-AzureRmVirtualNetwork -ResourceGroupName $resourceGroup -Location $location `
-Name $NamevNET -AddressPrefix 10.50.1.0/24 -Subnet $subnetConfig
# Create a public IP address and specify a DNS name
$pip = New-AzureRmPublicIpAddress -ResourceGroupName $resourceGroup -Location $location `
-Name "$Namepublicdns$(Get-Random)" -AllocationMethod Static -IdleTimeoutInMinutes 4
# Create an inbound network security group rule for port 3389
$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig -Name $NameNetworkSecurityGroupRuleRDP -Protocol Tcp `
-Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 3389 -Access Allow
# Create a network security group
$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $resourceGroup -Location $location `
-Name $NameNetworkSecurityGroup -SecurityRules $nsgRuleRDP
# Create a virtual network card and associate with public IP address and NSG
$nic = New-AzureRmNetworkInterface -Name $NameVNic -ResourceGroupName $resourceGroup -Location $location `
-SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id
# Create a virtual machine configuration
$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize $VMSize | `
Set-AzureRmVMOperatingSystem -Windows -ComputerName $vmName -Credential $cred | `
Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2016-Datacenter -Version latest | `
Add-AzureRmVMNetworkInterface -Id $nic.Id
# Create a virtual machine
New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vmConfig
Everything runs fine, but when it gets to the end, the New-AzureRmVM script never returns or exits. If I disconnect my machine from the net, I'll get a message about a long running process, but other than that, the shell appears to be locked up.
Any ideas how to get the command to exit so I can reuse the shell?

How to copy custom images in Azure from one subscription to another

I am running my virtual machines on Azure which consists of multiple directories and each directory has two-three subscriptions in it. I made a custom image for my infrastructure environment under my default subscription (say "Visual Studio Enterprise: BizSpark").
I want to copy this custom image to other subscriptions under different directories so that this image is easily accessible throughout my Azure account.
I was following this link : Copy Managed Images by Michael S. Collier
but I got stuck at this snippet:
diskName=$(az vm show --resource-group linux -n vm --query "storageProfile.osDisk.name" -o tsv)
Error:
The Resource 'Microsoft.Compute/virtualMachines/vm' under resource group 'linux' was not found.
Any lead on this will be appreciated.
For a managed disk or managed image, you can not directly move it from one subscription to another subscription which is existed in a different tenant. The error may indicate that you have connected to a different tenant.
If the original managed disk exists, you could refer this blog to move Azure Managed Disk between Tenants
You can create a snapshot of the managed disk and move it to a storage
account in the source subscription, then copy the snapshot to a
storage account in the destination subscription and create a managed
disk out of the snapshot and a VM with the managed disk afterward.
Here are the main steps in PowerShell.
A: Download the disk snapshot to a storage account
$sourceSubscriptionId = ''
$sourceStorageAccountName = "SourceStorageAccount"
$sourceStorageAccountKey = "9O1...Kg=="
$sourceStorageAccountContainer = "containername"
# path of the download URL of the snapshot
$VHDDownloadUri = "https://....blob.core.windows.net/..."
$targetSnapshotName = "snapshot.vhd"
#download snapshot to StorageAccount-Source (the storage account is located in the source subscription)
Select-AzureRmSubscription -SubscriptionId $sourceSubscriptionId
$sourceStorageAccountContext = New-AzureStorageContext –StorageAccountName $sourceStorageAccountName -StorageAccountKey $sourceStorageAccountKey
Start-AzureStorageBlobCopy -AbsoluteUri $VHDDownloadUri -DestContainer $sourceStorageAccountContainer -DestContext $sourceStorageAccountContext -DestBlob $targetSnapshotName
B: Copy the snapshot to a storage account in the destination subscription of an other tenant:
$destSubscriptionId = ''
$destStorageAccount = "DestStorageAccount"
$destStorageAccountKey = "Pqn.../Q=="
$destStorageAccountContainer = "container"
Select-AzureRmSubscription -SubscriptionId $destSubscriptionId
$destStorageAccountContext = New-AzureStorageContext –StorageAccountName $destStorageAccount -StorageAccountKey $destStorageAccountKey
Get-AzureStorageBlobCopyState -Context $destStorageAccountContext -Blob $targetSnapshotName
$blobCopy = Start-AzureStorageBlobCopy -DestContainer $destStorageAccountContainer -DestContext $destStorageAccountContext -SrcBlob $targetSnapshotName -Context $sourceStorageAccountContext -SrcContainer $sourceStorageAccountContainer
Write-Host ($blobCopy | Get-AzureStorageBlobCopyState).CopyId
Write-Host ($blobCopy | Get-AzureStorageBlobCopyState).TotalBytes
Write-Host ($blobCopy | Get-AzureStorageBlobCopyState).BytesCopied
while(($blobCopy | Get-AzureStorageBlobCopyState).Status -eq "Pending")
{
Start-Sleep -s 5
#$blobCopy | Get-AzureStorageBlobCopyState
$output = "`r" + ($blobCopy | Get-AzureStorageBlobCopyState).BytesCopied
Write-Host $output -NoNewline
}
The copy process runs asynchronous. If you need to stop the copy process, get the CopyId and use the Stop-AzureStorageBlogCopy command: Stop-AzureStorageBlobCopy -Container $destStorageAccountContainer -Blob $targetSnapshotName -CopyId "<GUID>" -Context $destStorageAccountContext
C: Create a new VM and use the snapshot.vhd from the DestStorageAccount as base image for the managed disk:
$rgName = "DestResourceGroup"
$location = "northeurope"
$storageName = "MyVMstorage"
$storageType = "Standard_LRS"
$nicname = "MyVM-nic"
$subnet1Name = "MyVM-subnet"
$vnetName = "MyVM-vnet"
$vnetAddressPrefix = "10.0.0.0/16"
$vnetSubnetAddressPrefix = "10.0.0.0/24"
$vmName = "MyVM"
$vmSize = "Standard_D2s_v3"
$osDiskName = $vmName + "osDisk"
$osDiskUri = "https://deststorageaccount.blob.core.windows.net/container/snapshot.vhd"
$storageacc = New-AzureRmStorageAccount -ResourceGroupName $rgName -Name $storageName -Type $storageType -Location $location
$pip = New-AzureRmPublicIpAddress -Name $nicname -ResourceGroupName $rgName -Location $location -AllocationMethod Dynamic
$subnetconfig = New-AzureRmVirtualNetworkSubnetConfig -Name $subnet1Name -AddressPrefix $vnetSubnetAddressPrefix
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName -Location $location -AddressPrefix $vnetAddressPrefix -Subnet $subnetconfig
$nic = New-AzureRmNetworkInterface -Name $nicname -ResourceGroupName $rgName -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$discStorageAcc = Get-AzureRmStorageAccount -ResourceGroupName $ResourceGroup -Name $destStorageAccount
$diskConfig = New-AzureRmDiskConfig -AccountType 'PremiumLRS' -Location $location -CreateOption Import -StorageAccountId ($discStorageAcc.Id) -SourceUri $osDiskUri
$disk = New-AzureRmDisk -Disk $diskConfig -ResourceGroupName $rgName -DiskName "managedsnapshot"
$vm = Set-AzureRmVMOSDisk -VM $vm -ManagedDiskId $disk.Id -CreateOption Attach -Windows
New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vm -Verbose
first command just retrieves disk name, so if you know disk name you dont have to run it. just do:
diskName="diskname_goes_here"

Deploy VM on Azure using created (generalized/sysprep'ed) snapshot stored on Blob

I need someone more experienced with VM's on Azure. I have prepared image with Win2016 Server and few additional configurations. I have done sysprep and created image and snapshot after that. When I create a new machine using this image, machine fails and don't event start. So I have found another solution: snapshot taken from this machine and transferred to blob. A new machine after below code execution starts but i am not able to connect RDP...
error: VM deployed with error: New-AzureRmVM : Long running operation failed with status 'Failed'
$resourceGroupName="<resource-group>"
$subscriptionId = "<subs>"
$tenantId = "<tenant>"
$location = "West Europe"
# sign in
Write-Host "Logging in...";
Login-AzureRmAccount -TenantID $tenantID;
# select subscription
Write-Host "Selecting subscription '$subscriptionId'";
Select-AzureRmSubscription -SubscriptionID $subscriptionId;
# Create a Resource Group
New-AzureRmResourceGroup -Name $resourceGroupName -Location $location
#Prepare the VM parameters
$rgName = $resourceGroupName
# Get the Azure location and storage account names
$locName=(Get-AzureRmResourceGroup -Name $rgName).Location
$exSubnet=New-AzureRMVirtualNetworkSubnetConfig -Name EX2016Subnet -AddressPrefix 10.0.0.0/24
New-AzureRMVirtualNetwork -Name EX2016Vnet -ResourceGroupName $rgName -Location $locName -AddressPrefix 10.0.0.0/16 -Subnet $exSubnet -DNSServer 10.0.0.5
$rule1 = New-AzureRMNetworkSecurityRuleConfig -Name "RDPTraffic" -Description "Allow RDP to all VMs on the subnet" -Access Allow -Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389
$rule2 = New-AzureRMNetworkSecurityRuleConfig -Name "ExchangeSecureWebTraffic" -Description "Allow HTTPS to the Exchange server" -Access Allow -Protocol Tcp -Direction Inbound -Priority 101 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix "10.0.0.5/32" -DestinationPortRange 443
$rule3 = New-AzureRMNetworkSecurityRuleConfig -Name "ExchangeSecurePortExternal" -Description "Allow access port 25 to the Exchange server" -Access Allow -Protocol Tcp -Direction Inbound -Priority 102 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix "10.0.0.5/32" -DestinationPortRange 25
New-AzureRMNetworkSecurityGroup -Name EX2016Subnet -ResourceGroupName $rgName -Location $locName -SecurityRules $rule1, $rule2,$rule3
$vnet=Get-AzureRMVirtualNetwork -ResourceGroupName $rgName -Name EX2016Vnet
$nsg=Get-AzureRMNetworkSecurityGroup -Name EX2016Subnet -ResourceGroupName $rgName
Set-AzureRMVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name EX2016Subnet -AddressPrefix "10.0.0.0/24" -NetworkSecurityGroup $nsg
# Specify the virtual machine name and size
$vmName="exVM"
$vmSize="Standard_D3_v2"
$vnet=Get-AzureRMVirtualNetwork -Name "EX2016Vnet" -ResourceGroupName $rgName
$vm=New-AzureRMVMConfig -VMName $vmName -VMSize $vmSize
# Create the NIC for the virtual machine
$nicName=$vmName + "-NIC"
$pipName=$vmName + "-PublicIP"
$pip=New-AzureRMPublicIpAddress -Name $pipName -ResourceGroupName $rgName -DomainNameLabel $vmDNSName -Location $locName -AllocationMethod Dynamic
$nic=New-AzureRMNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $locName -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -PrivateIpAddress "10.0.0.5"
# Specify the image and local administrator account, and then add the NIC
$cred=Get-Credential -Message "Type the name and password of the local administrator account for exVM."
$osDiskName = "VM01-OSDisk"
$osDiskUri = "https://xxxxxx.blob.core.windows.net/images/Image.vhd"
$storageAccountType = "StandardLRS"
$vm=Add-AzureRMVMNetworkInterface -VM $vm -Id $nic.Id
$osDisk = New-AzureRmDisk -DiskName $osDiskName -Disk (New-AzureRmDiskConfig -AccountType $storageAccountType -Location $location -CreateOption Import -SourceUri $osDiskUri) -ResourceGroupName $rgName
$vm = Set-AzureRmVMOSDisk -VM $vm -ManagedDiskId $osDisk.Id -StorageAccountType $storageAccountType -DiskSizeInGB 128 -CreateOption Attach -Windows
$vm = Set-AzureRmVMBootDiagnostics -VM $vm -disable
#Create the new VM
New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vm
You could use the blob to create a snapshot and use the snapshot to create a new VM, you could check this example.
$snapshot = Get-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName
$diskConfig = New-AzureRmDiskConfig -AccountType $storageType -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy
$disk = New-AzureRmDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $osDiskName
#Initialize virtual machine configuration
$VirtualMachine = New-AzureRmVMConfig -VMName $virtualMachineName -VMSize $virtualMachineSize
#Use the Managed Disk Resource Id to attach it to the virtual machine. Please change the OS type to linux if OS disk has linux OS
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -ManagedDiskId $disk.Id -CreateOption Attach -Linux
#Create a public IP for the VM
$publicIp = New-AzureRmPublicIpAddress -Name ($VirtualMachineName.ToLower()+'_ip') -ResourceGroupName $resourceGroupName -Location $snapshot.Location -AllocationMethod Dynamic
#Get the virtual network where virtual machine will be hosted
$vnet = Get-AzureRmVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $resourceGroupName
# Create NIC in the first subnet of the virtual network
$nic = New-AzureRmNetworkInterface -Name ($VirtualMachineName.ToLower()+'_nic') -ResourceGroupName $resourceGroupName -Location $snapshot.Location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIp.Id
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $nic.Id
#Create the virtual machine with Managed Disk
New-AzureRmVM -VM $VirtualMachine -ResourceGroupName $resourceGroupName -Location $snapshot.Location
You also could check this link to create snapshot from a blob.

how to create multiple vms in azure resourcemanager portal with same NIC using powershell

I am trying to create multiple vms in azure resourcemanager portal with same NIC using powershell. But single VM alone getting created. when I use array for this exception occurs.
$i = 1;
[System.Collections.ArrayList]$vmArray1=#()
Do
{
$i;
switch($i){
{$vmName="Namenode"+$i}
{$vmName="Namenode"+$i}
default {$vmName="Datanode"+($i-2)}
}
$vmconfig=New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$vmArray1.Add($vmconfig)
$i +=1
} Until ($i -gt $NumberOfVM)
$vm=Set-AzureRmVMOperatingSystem -VM $vmconfig -Windows -ComputerName $vmArray1 -Credential $credvm -ProvisionVMAgent -EnableAutoUpdate
But an exception occurs. Please let me know how to resolve this.
I am not sure whether it's your typing error or not. Your Set-AzureRmVMOperatingSystem Command is not inside the loop. That means it will always run only once. Beside of Operating System, you also need to provide Source Image, OS Disk, and Network Interface.
I have written something for you, and I have tested it at my end. It will create a set of VMs in one resource group and one Virtual Network. If you want the VMs in different resource groups or VNet, you can move the creation commands of resource groups or Vnet into the loop.
$credvm = Get-Credential
$NumberOfVM = <the number of VM you want to create>;
$ResourceGroupName = "<your resource group name>"
$Location = "East Asia"
## Storage
$StorageName = "<your storage account name>"
$StorageType = "Standard_GRS"
# Resource Group, if resource group has been created comment this out.
New-AzureRmResourceGroup -Name $ResourceGroupName -Location $Location
## Network
$InterfaceName = "<your interface name>"
$Subnet1Name = "Subnet1"
$VNetName = "<your vnet name>"
$VNetAddressPrefix = "10.0.0.0/16"
$VNetSubnetAddressPrefix = "10.0.0.0/24"
## Compute
$vmSize = "Standard_A2"
# Network
$SubnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name $Subnet1Name -AddressPrefix $VNetSubnetAddressPrefix
$VNet = New-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroupName -Location $Location -AddressPrefix $VNetAddressPrefix -Subnet $SubnetConfig
$i = 1;
Do
{
$i;
$vmName="Namenode"+$i
$vmconfig=New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$vm=Set-AzureRmVMOperatingSystem -VM $vmconfig -Windows -ComputerName $vmName -Credential $credvm -ProvisionVMAgent -EnableAutoUpdate
$OSDiskName = $vmName + "osDisk"
# Storage
$StorageAccount = New-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageName$i -Type $StorageType -Location $Location
## Setup local VM object
$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2012-R2-Datacenter -Version "latest"
$PIp = New-AzureRmPublicIpAddress -Name $InterfaceName$i -ResourceGroupName $ResourceGroupName -Location $Location -AllocationMethod Dynamic
$Interface = New-AzureRmNetworkInterface -Name $InterfaceName$i -ResourceGroupName $ResourceGroupName -Location $Location -SubnetId $VNet.Subnets[0].Id -PublicIpAddressId $PIp.Id
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $vm -Id $Interface.Id
$OSDiskUri = $StorageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/" + $OSDiskName + ".vhd"
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -Name $OSDiskName -VhdUri $OSDiskUri -CreateOption FromImage
## Create the VM in Azure
New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $VirtualMachine
$i +=1
}
Until ($i -gt $NumberOfVM)

Resources