title: powershell unable to add ips into a security group via array
body: <p>Having issues getting this script running. Err. Cannot index into a null array
  any ideas would be a great help. I've looked at verbose logging but I'm not sure how to output compute methods to find the contents. Obviously it appears to be empty but for investigation purposes at least it would be a start. </p>
  
  <pre><code>    $rgname = "xxxxxx" 
  $subscriptionname = "xxxxxx"
  $vmname = "xxxxxx"
  
  # Get the VM we need to configure
  $vm = Get-AzureRmVM -ResourceGroupName $rgname -Name $vmname
  Write-host "$vm"
  
  # Get the name of the first NIC in the VM
  $nic = Get-AzureRmNetworkInterface -ResourceGroupName $rgname -Name (Get-AzureRmResource -ResourceId $vm.NetworkInterfaceIDs[0]).ResourceName
  
  $nsg = Get-AzureRmNetworkSecurityGroup  -ResourceGroupName $rgname  -Name (Get-AzureRmResource -ResourceId $nic.NetworkSecurityGroup.Id).Name 
  
  
  $nameAndIPArray = @(("ipname1","ipname2","ipname3","ipname4",ipname5"),
  ("ip1,"ip2","ip3","ip4","ip5"))
  
  #LOOP THE ARRAY AND SET DESCRIPTION AND IP VARIABLE FOR COMMAND
  $priority = 1010
  for ($i=0;$i -lt $nameAndIPArray[0].length; $i++) {
  
  
      $nameAndIPArray[0][$i] + " " + $nameAndIPArray[1][$i]
      $nsg | Add-AzureRmNetworkSecurityRuleConfig -Name $nameAndIPArray[0][$i] -Description $nameAndIPArray[0][$i] -Access Allow -Protocol Tcp -Direction Inbound -Priority $priority -SourceAddressPrefix $nameAndIPArray[1][$i] -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 443
      Set-AzureRmNetworkSecurityGroup -NetworkSecurityGroup $nsg
  
      $priority = $priority + 10
  
  }
  
  Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine
  Cannot index into a null array.
  At line:14 char:1
  Get-AzureRmResource : Cannot validate argument on parameter 'ResourceId'. The argument is null or empty. Provide an argument that is not null or 
  empty, and then try the command again.
  Add-AzureRmNetworkSecurityRuleConfig : Cannot bind argument to parameter 'NetworkSecurityGroup' because it is null.
  At line:28 char:12
  </code></pre>
  
  <p>Set-AzureRmNetworkSecurityGroup : Cannot bind argument to parameter 'NetworkSecurityGroup' because it is null.
      At line:29 char:59</p>