Cannot index into a null array?

More PowerShell Questions

Hi

Ive got this

#Create NIC (called MyNicName)
$nicName = "myNicName"
$nic = New-AzureRmNetworkInterface -Name $nicName `
   -ResourceGroupName $rgName `
   -Location $location -SubnetId $azvnetworks.Subnets[0].Id `
   -PublicIpAddressId $pip.Id `
   -NetworkSecurityGroupId $aznsg.Id

Open in new window


However when I run it I get

Cannot index into a null array.
At line:1 char:1
+ $nic = New-AzureRmNetworkInterface -Name $nicName `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Open in new window


That means nothing to me (sorry)

What have I done wrong?

Pete
LVL 59
Pete LongTechnical ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technology NinjaCommented:
Hi Pete,

Any decent programming language will not let you perform operation with a null object.
$azvnetworks.Subnets[0].Id
You see [0] (square brackets and a number within), it is a way for programming language to inspect members of an array.

I think your variable, $azvnetworks.Subnets is not populated with a value and as it is a null indexing operation cannot be performed and hence the error.

How are you getting $azvnetworks? Can you share the complete script?

Regards,
Chinmay.
nociSoftware EngineerCommented:
Here is an array: $azvnetworks.Subnets[0].Id
where you want element 0, (using index 0).
If the array is empty then there are no elements at all so trying to get one will fail.

First check if $azvnetworks.Subnets does hold data before using it.
Pete LongTechnical ConsultantAuthor Commented:
Thank guys

I'm using these instructions

https://docs.microsoft.com/en-us/azure/virtual-machines/windows/create-vm-specialized

On THAT example that string is called $vnet

in my example its called $azvnetworks

Which I populated earlier by doing this;
$azvnetworks = Read-Host -Prompt 'Enter Virtual Netwotk NAME (from above);'

Open in new window



Pete
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Pete LongTechnical ConsultantAuthor Commented:
Ah OK I think I see the problem that is populated with this

$vnet = New-AzureRmVirtualNetwork `
   -Name $vnetName -ResourceGroupName $destinationResourceGroup `
   -Location $location `
   -AddressPrefix 10.0.0.0/16 `
   -Subnet $singleSubnet

Open in new window


I dont need to create one because I already have one!! (called Main-LAN)

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-AzureRmVirtualNetwork
WARNING: Breaking changes in the cmdlet 'Get-AzureRmVirtualNetwork' :
WARNING:  - "The output type 'Microsoft.Azure.Commands.Network.Models.PSVirtualNetwork' is changing"
 - The following properties in the output type are being deprecated :
 'EnableVmProtection'


ResourceGroupName Name     Location ProvisioningState EnableDdosProtection EnableVmProtection
----------------- ----     -------- ----------------- -------------------- ------------------
RG-Test-Lab       Main-LAN uksouth  Succeeded         False                False

Open in new window


SO I need to put that information into the Array?

If so How Do I do that (sorry Im at the limit of my PowerShell here)
Pete LongTechnical ConsultantAuthor Commented:
So I would need to GET my Virtual Network (already in my script called $azvnetworks )

And feed in

-Name $azvnetworks
-Location $location (already populated)
-AddressPrefix 10.0.0.0/16 (is the same)
-Subnet $azsubnet (already populated)
oBdACommented:
Assuming the other variables in your first script are already populated, try this:
# $rgName = ...
# $location = ...
# $aznsg = ...
# $pip = ...
$azvnetworks = Get-AzureRmVirtualNetwork -Name 'Main-LAN'
#Create NIC (called MyNicName)
$nicName = "myNicName"
$nic = New-AzureRmNetworkInterface `
	-Name $nicName `
	-ResourceGroupName $rgName `
	-Location $location `
	-SubnetId $azvnetworks.Subnets[0].Id `
	-PublicIpAddressId $pip.Id `
	-NetworkSecurityGroupId $aznsg.Id

Open in new window

Pete LongTechnical ConsultantAuthor Commented:
Hi oBda

$azvnetworks already contains 'Main-LAN"

I'm trying to write this so that it can be used for many different scenarios - having the name of my Virtual Test network in the script will negate that

(background: This script connects the VMware Center locally via PowerCLI - downloads a selected VM converts it from VMDK to VHD then uploads it into Aure and creates a VM) it's the creating the VM part I'm struggling with, everything else works ok, for each piece of information required, it lists all the options and asks you to input the one you want, thats why $aznetworks is already populated, the person running the script picked and entered it)

Would it not be better to do something like

$azvnetworkTEMP = Get-AzureRmVirtualNetwork {where name is equal to $azvnetwork}

Whatever the code would be for that?

Does that make sense?

</P>
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Of course you can do that. You can either use
$azvnetworks = Get-AzureRmVirtualNetwork (Read-Host -Prompt 'Enter Virtual Netwotk NAME (from above);')

Open in new window

(and not change anything on the other lines) if you do not need the name again, or if you do
$azvnetworks = Read-Host -Prompt 'Enter Virtual Netwotk NAME (from above);'
$azvnetworksTemp = Get-AzureRmVirtualNetwork $azvnetworks

Open in new window

and then refer to that new var below that lines.
Pete LongTechnical ConsultantAuthor Commented:
Hi Qlemo

Thanks for the follow up

If I do that then all the other pieces wont go into the array? will they?

-ResourceGroupName $rgName `
-Location $location `
-SubnetId $azvnetworks.Subnets[0].Id `
-PublicIpAddressId $pip.Id `
-NetworkSecurityGroupId $aznsg.Id

Forgive me Im a firewall engineer script/code is not my thing?

P
oBdACommented:
PowerShell works with objects with multiple properties and methods.
The $vnet from the MS page is not a string. It's an object representing the virtual network.
The $azvnetworks you get from Read-Host is a plain old string.
Since you already have a Virtual Network, you don't need to create a new one, but you still need the object that represents said network, not just the object's "Name" property as a string variable. The string variable containing 'Main-LAN' "knows" exactly nothing about your Azure infrastructure, so it doesn't have a property "Subnets".
The "Get-AzureRmVirtualNetwork -Name $azvnetworks" will return this object.
The "-ResourceGroupName $rgName ` ..." list is not an array - these are the arguments for New-AzureRmNetworkInterface; the backtick is the line continuation character in PS.
# $rgName = ...
# $location = ...
# $aznsg = ...
# $pip = ...
$azvnetworks = Read-Host -Prompt 'Enter Virtual Netwotk NAME (from above)'
$vnet = Get-AzureRmVirtualNetwork -Name $azvnetworks
#Create NIC (called MyNicName)
$nicName = "myNicName"
$nic = New-AzureRmNetworkInterface `
	-Name $nicName `
	-ResourceGroupName $rgName `
	-Location $location `
	-SubnetId $vnet.Subnets[0].Id `
	-PublicIpAddressId $pip.Id `
	-NetworkSecurityGroupId $aznsg.Id

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pete LongTechnical ConsultantAuthor Commented:
Thanks for putting me straight :)

$vnet = Get-AzureRmVirtualNetwork -Name $azvnetworks

Threw an error, it needs the ResourceGroupName as well

$vnet = Get-AzureRmVirtualNetwork -Name $azvnetworks -ResourceGroupName $aznsg

Thanks again

P
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.