繁体   English   中英

Powershell无法通过阵列将ip添加到安全组中

[英]powershell unable to add ips into a security group via array

运行此脚本时遇到问题。 呃。 无法将索引编入null数组将对您大有帮助。 我看过详细的日志记录,但是我不确定如何输出计算方法来查找内容。 显然它似乎是空的,但至少出于调查目的,这只是一个开始。

    $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:无法将参数绑定到参数“ NetworkSecurityGroup”,因为它为null。 在线:29字符:59

我假设这是空行,因此您不会找回任何虚拟机:

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

因此,请检查$ vm变量,以及是否存在带有这些参数的虚拟机。

我在实验室测试,您的脚本中有一些错误。 使用您的脚本,我无法获得$nic$nsg值。 $vm没有属性NetworkInterfaceIDs[0] ,因此不能这样使用。 $nameAndIPArray行会丢失" 。正确的用法应如下所示:

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

我修改您的脚本,使用resource group namensg name获得$nsg 您可以在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

}

将正确的值替换为您的脚本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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