New-AzVM: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

Sheetal Jain 5 Reputation points
2025-01-21T19:07:05.0466667+00:00

Hello I am trying to setup a Exchange VM following this article https://learn.microsoft.com/en-us/exchange/plan-and-deploy/deploy-new-installations/create-azure-test-environments?view=exchserver-2019 but getting the following error

Create an availability set for domain controller virtual machines

New-AZAvailabilitySet -ResourceGroupName $rgName -Name dcAvailabilitySet -Location $locName -Sku Aligned -PlatformUpdateDomainCount 5 -PlatformFaultDomainCount 2

Create the domain controller virtual machine

$vnet = Get-AZVirtualNetwork -Name EXSrvrVnet -ResourceGroupName $rgName

$pip = New-AZPublicIpAddress -Name adVM-NIC -ResourceGroupName $rgName -Location $locName -AllocationMethod Dynamic

$nic = New-AZNetworkInterface -Name adVM-NIC -ResourceGroupName $rgName -Location $locName -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -PrivateIpAddress 10.0.0.4

$avSet=Get-AZAvailabilitySet -Name dcAvailabilitySet -ResourceGroupName $rgName

$vm=New-AZVMConfig -VMName adVM -VMSize Standard_D1_v2 -AvailabilitySetId $avSet.Id

$vm=Set-AZVMOSDisk -VM $vm -Name adVM-OS -DiskSizeInGB 128 -CreateOption FromImage -StorageAccountType "Standard_LRS"

$diskConfig=New-AZDiskConfig -AccountType "Standard_LRS" -Location $locName -CreateOption Empty -DiskSizeGB 20

$dataDisk1=New-AZDisk -DiskName adVM-DataDisk1 -Disk $diskConfig -ResourceGroupName $rgName

$vm=Add-AZVMDataDisk -VM $vm -Name adVM-DataDisk1 -CreateOption Attach -ManagedDiskId $dataDisk1.Id -Lun 1

$cred=Get-Credential -Message "Type the name and password of the local administrator account for adVM."

$vm=Set-AZVMOperatingSystem -VM $vm -Windows -ComputerName adVM -Credential $cred -ProvisionVMAgent -EnableAutoUpdate

$vm=Set-AZVMSourceImage -VM $vm -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2012-R2-Datacenter -Version "latest"

$vm=Add-AZVMNetworkInterface -VM $vm -Id $nic.Id

New-AZVM -ResourceGroupName $rgName -Location $locName -VM $vm

The last command is throwing this error

New-AzVM: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

Any help will be appreciated

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
8,283 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Akshay kumar Mandha 2,185 Reputation points Microsoft Vendor
    2025-01-21T22:27:51.2733333+00:00

    Hi Sheetal Jain,
    Welcome to the Microsoft Q&A Platform! Thank you for asking your question here
    Based on your query,
    When you see the error
    "Index was out of range. Must be non-negative and less than the size of the collection",
    To understand the scenario elaborating little bit for more to address the issue for you in future as well
    Example Scenario

    When you're working with a list or array, the number you use to access an element must be zero or a positive number. Negative numbers won't work. Additionally, the number you use must be within the range of the list. For example, if your list has 5 items, you can use numbers 0 through 4. Trying to use a number outside this range, like 5 or higher, will cause an error because those positions don't exist in the list
    If you know all these things already means that would be great.!

    Troubleshooting
    To address this issue make try to check make sure the virtual network named EXSrvrVnet and its subnets are set up properly and being referenced correctly. You need to confirm that the first subnet in the list ($vnet.Subnets[0].Id) exists and isn't empty. Also, ensure that the availability set called dcAvailabilitySet has been created and can be retrieved without issues. Finally, verify that the network interface ($nic) is being created with the correct subnet ID. By checking these key points,
    use some debug output line to verify the resource exist or not by using below commands
    Example:

    $nic = New-AZNetworkInterface -Name adVM-NIC -ResourceGroupName $rgName -Location $locName -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -PrivateIpAddress 10.0.0.4 Write-Output "Network Interface: $($nic)"
    

    And also handle this case use the exception case get to know where the issue cause handling in power shell techniques to understand Please refer the below documentation
    Everything you wanted to know about exceptions
    about_Try_Catch_Finally

    Please let me know if you have any further questions or if you are still experiencing the issue. Tag me in a comment, and I will be happy to help. Feel free to ask if you need any additional information. I am always here to assist you as needed.

    If you found this information helpful, please click an accepting the answer and 👍"Upvote" on my post for other community member's reference
    User's image


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.