Cannot index into a null array.

Welcome Forums General PowerShell Q&A Cannot index into a null array.

This topic contains 3 replies, has 2 voices, and was last updated by

 
Inactive
6 months, 1 week ago.

  • Author
    Posts
  • #98904

    Inactive
    Points: 0
    Rank: Member

    Hi Guys,

    I am trying to create a script with task and one of the tasks is to create new azure vm. Everything is good until it comes to the part when it needs to create VMConfig. This is the error messsage I receive and under that I posted a code.

    Cannot index into a null array.
    + New-AzureRmNetworkInterface -Name $VMNIC -ResourceGroupName $ ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

    Add-AzureRmVMNetworkInterface : Cannot validate argument on parameter 'Id'. The argument is null or empty. Provide an argument that
    is not null or empty, and then try the command again.
    + Add-AzureRmVMNetworkInterface -Id $VMNIC.Id
    + ~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Add-AzureRmVMNetworkInterface], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Compute.AddAzureVMNetworkInterfaceCommand

    New-AzureRmVM : Cannot validate argument on parameter 'VM'. The argument is null or empty. Provide an argument that is not null or
    empty, and then try the command again.

    + ... M -ResourceGroupName $ResourceGroup -Location $Location -VM $VMConfig
    + ~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [New-AzureRmVM], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Compute.NewAzureVMCommand

    –> CAN SOMEONE PLEASE HELP ME WITH THIS ARRAY. VARIABLES ARE NOT EMPTY. HERE IS THE CODE

    CODE
    25 { $subscription = Read-Host "Specify subscription where you want to create new Resource Group"
    Select-AzureRmSubscription -Subscription $subscription

    $ResourceGroup = Read-Host 'Enter the existing Resource Group or create a new one'
    $Location = Read-Host 'Enter your location'
    $vmcred = Get-Credential -Message "Type the name and password of the VM local administrator account."
    $Publisher = Read-Host "Enter the publisher name (Example : MicrosoftWindowsServer)"
    $Offer = Read-Host " Specify Offer (Example: WindowsServer) "
    $SKU = Read-Host " Specify SKU (Example: 2016-Datacenter) "
    $VMName = Read-Host " Specify VM Name"
    $VMSize = Read-Host " Specify VM Size (Example: Standard_DS_v2)"
    $subnet = Read-Host "Enter the existing subnet or create a new one"
    $VNET = Read-Host "Enter the existing VNET or create a new one"

    if(!$resourceGroup){
    Write-Host "Resource group '$resourceGroupName' does not exist. To create a new resource group, please enter a location.";
    if(!$Location) {
    $Location = Read-Host "resourceGroupLocation";
    }
    Write-Host "Creating resource group '$ResourceGroup' in location '$Location'";
    New-AzureRmResourceGroup -Name $ResourceGroup -Location $Location
    }

    else{
    Write-Host "Using existing resource group '$resourceGroupName'";
    }

    if(!$subnet){
    $subnet = Read-Host 'Enter the existing subnet or create a new one'
    if(!$subnet) {
    Write-Host "Subnet '$subnet' does not exist. To create a new one, please press enter.";
    $subnet = Read-Host "Subnet";
    }
    Write-Host "Creating new subnet '$subnet'";
    New-AzureRmVirtualNetworkSubnetConfig -Name $subnet
    }

    else{
    Write-Host "Using existing subnet config '$subnet'";
    }

    if(!$VNET){
    $VNET = Read-Host 'Enter the existing VNET or create a new one'
    if(!$VNET) {
    Write-Host "VNET '$VNET' does not exist. To create a new one, please press enter.";
    $VNET = Read-Host "VNET NAME";
    }
    Write-Host "Creating new VNET '$VNET'";

    New-AzureRmVirtualNetwork -Name $VNET -ResourceGroupName $ResourceGroup `
    -Location $Location -AddressPrefix -Subnet $subnet
    }

    else{
    Write-Host "Using existing VNET '$VNET'";
    }

    $PublicIp = Read-Host "Enter the Public IP Name"
    New-AzureRmPublicIpAddress -Name $PublicIp -ResourceGroupName $ResourceGroup `
    -Location $Location -AllocationMethod Dynamic
     
    ————(HERE IT STARTS TO FAIL.EVERYTHING ABOVE IS WORKING AS IT SHOULD———————–)

    $VMNIC = Read-Host 'Enter the NIC Name'
    New-AzureRmNetworkInterface -Name $VMNIC -ResourceGroupName $ResourceGroup `
    -Location $Location -SubnetId $VNET.Subnets[0].Id -PublicIpAddressId $PublicIp.Id

    $VMConfig = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize | `

    Set-AzureRmVMOperatingSystem -Windows -ComputerName $VMname -Credential $VMCred | `
     
    Set-AzureRmVMSourceImage -PublisherName $Publisher -Offer $Offer -Skus $SKU -Version 'latest' | `
     
    Add-AzureRmVMNetworkInterface -Id $VMNIC.Id

    # Create the virtual machine
     
    New-AzureRmVM -ResourceGroupName $ResourceGroup -Location $Location -VM $VMConfig

    }

    }

    }

  • #98907

    Inactive
    Points: 0
    Rank: Member

    Edited

  • #98916

    Participant
    Points: 37
    Helping Hand
    Rank: Member

    Your issue is your use of this value:

    $VNET.Subnets[0].Id

    $VNET as defined above is just a string, read from the console input with Read-Host. It has no properties. You may want to be using some kind of Get-* cmdlet to retrieve an object with that name instead and access its properties, but as it is this object has no .Subnets property, much less any child members.

    Additionally, I'd recommend looking into using well-defined parameters for a function like this and supporting ShouldProcess, especially as it seems this is intended to be environment-altering.

    Check out the help pages for ShouldProcess, Parameters, and Advanced Functions. 🙂

  • #99133

    Inactive
    Points: 0
    Rank: Member

    Hi Joel,

    Thanks for your help. That was true. After I looked the $VNET was just a string and I do this to make it work. Once the checking for VNET was done I did this to create a variable $VNET populate with info. Thank you so much for your help.

    Get-AzureRmVirtualNetwork -Name $VNET -ResourceGroupName $ResourceGroup -OutVariable VNET | Out-Null
    I did the same for the PublicIP address

    I choose to run on this way instead of define parameters to make it like a task list. If you want to do this press 1 and if you want to check for that press 2 and so on. I am doing a DO loop for this.

The topic ‘Cannot index into a null array.’ is closed to new replies.