[英]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 name
和nsg 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.