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

独行侠

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

    $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”,因为它为空。在行:29 字符:59

Shui shengbao

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

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

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

}

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用AWS CLI将当前PC IP添加到安全组中

使用Powershell将电子邮件地址添加到AD安全组

PowerShell:如何将1个用户添加到多个Active Directory安全组-具有写权限的安全组的“安全”选项卡

将安全组添加到Openstack中的实例

Powershell将O365用户添加到具有“代理发送”权限的邮箱并将其添加到具有完全访问权限的安全组

无法将安全组添加到RDS实例

PowerShell将错误添加到阵列

Boto3将入站规则添加到非默认VPC中的安全组

将OU的所有用户也添加到安全组中

我想识别terraform执行环境的公共ip并将其添加到安全组

Powershell:将用户添加到阵列中的组

将安全组声明添加到自定义Azure Active Directory应用程序的SAML令牌中

如何通过Ansible将标签添加到AWS安全组?

将包含自我和安全组ID的aws_security_group_rule添加到安全组

需要使用Powershell将最后两个八位位组添加到IP范围列表中

将cwd / pwd /(。)添加到powershell中的路径

将HTML代码添加到Powershell数组中

AWS:将正确的入站安全组添加到RDS和EC2实例

将应用程序安全组添加到Azure VM

使用AWS CDK将入口规则添加到安全组

使用Terraform将OpenStack安全组添加到不受Terraform管理的实例

如何将安全组添加到 Azure 应用服务?

如何使用 nodejs 将安全组添加到已经运行的 EC2 实例?

解析Powershell阵列中的RegEx IP匹配项

将IP白名单安全性添加到API网关

通过Powershell添加到AD。失误

将超链接添加到通过PowerShell生成的电子邮件中

如何通过 Powershell 将变量添加到特定的注册表项路径中?

无法阻止将重复项添加到阵列中