Cannot index into a null array.

This topic contains 4 replies, has 4 voices, and was last updated by  Blake 3 months ago.

  • Author
    Posts
  • #98910

    Blake
    Participant

    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

    [PRE]
    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

    }

    }

    }
    [/PRE]

  • #98934

    Don Jones
    Keymaster

    That's a lotta code :).

    It's telling you that $VMNIC doesn't contain anything. So you need to add some code, like a debug breakpoint or Write-Debug line, after where you populate $VMNIC. When you hit the break, see what's in $VMNIC.

  • #98937

    4Man
    Participant

    I just looked through it quickly but $VNET looks like it never becomes the object you're using in some situations, just stays a string as created when you cast it from read-host.

    — Attempting to use VNET as an object —
    New-AzureRmNetworkInterface -Name $VMNIC -ResourceGroupName $ResourceGroup `
    -Location $Location -SubnetId $VNET.Subnets[0].Id -PublicIpAddressId $PublicIp.Id

    — Situation where VNET is created but only contains a string from read-host —
    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

    }

    So in the above $VNET the string is used to create something, but $VNET never gets recast as an object that you can use $VNET.Subnets[0].Id. Maybe I missed where you recast $VNET as the object you're looking for. This could explain why the array was null.

  • #98982

    Alex Franco
    Participant

    Agreed with 4Man. It is trying Add-AzureRmVMNetworkInterface -Id 'string'. $VMNIC is not an object, so trying to get properties out of it with $VMNIC.Id is not going to work. $VMNIC is still just a string. Need to either get the object that has the Id parameter you want and pipe it to that Add-AzureRmVMNetworkInterface -Id $_.Id or you could do another Read-Host to manually set an Id. I am not familiar with Azure, so I would take those examples as pseudocode just in case I am off base on what the -Id parameter is looking for.

    Good luck

  • #99130

    Blake
    Participant

    Hi Guys,

    Thanks for your help. That was true 4Man thank you for pointing to it. 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 guys. You rock.

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

You must be logged in to reply to this topic.