简体   繁体   中英

powershell unable to add ips into a security group via array

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.

    $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

Set-AzureRmNetworkSecurityGroup : Cannot bind argument to parameter 'NetworkSecurityGroup' because it is null. At line:29 char:59

I assume this is the line that is empty, so you are not getting any vms back:

$vm = Get-AzureRmVM -ResourceGroupName $rgname -Name $vmname

so, check the $vm variable and if some vm's exist with those parameters.

I test in my lab, there are some mistakes in your script. Use your script, I could not get $nic and $nsg value. $vm does not have the attribute NetworkInterfaceIDs[0] , so you could not use like this. The line $nameAndIPArray loses " . The correct usage should be like below:

$nameAndIPArray = @(("ipname1","ipname2","ipname3","ipname4","ipname5"),
("ip1","ip2","ip3","ip4","ip5"))

I modify your script, I get $nsg by using resource group name and nsg name . You could find them on Portal, it works for me.

$nsg= Get-AzureRmNetworkSecurityGroup -ResourceGroupName <resource group name> -Name "<NSG name>"

$nameAndIPArray = @(("ipname1","ipname2","ipname3","ipname4","ipname5"),
("10.0.0.4","10.0.0.5","10.0.0.6","10.0.0.7","10.0.0.8"))

$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

}

Replace correct value to your script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM