[英]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.
无法将索引编入null数组将对您大有帮助。 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. Set-AzureRmNetworkSecurityGroup:无法将参数绑定到参数“ NetworkSecurityGroup”,因为它为null。 At line:29 char:59
在线:29字符: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. 因此,请检查$ vm变量,以及是否存在带有这些参数的虚拟机。
I test in my lab, there are some mistakes in your script. 我在实验室测试,您的脚本中有一些错误。 Use your script, I could not get
$nic
and $nsg
value. 使用您的脚本,我无法获得
$nic
和$nsg
值。 $vm
does not have the attribute NetworkInterfaceIDs[0]
, so you could not use like this. $vm
没有属性NetworkInterfaceIDs[0]
,因此不能这样使用。 The line $nameAndIPArray
loses "
. The correct usage should be like below: $nameAndIPArray
行会丢失"
。正确的用法应如下所示:
$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
. 我修改您的脚本,使用
resource group name
和nsg name
获得$nsg
。 You could find them on Portal, it works for me. 您可以在Portal上找到它们,对我有用。
$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. 将正确的值替换为您的脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.