2017-06-27 71 views
0

有問題,讓這個腳本運行。呃。不能索引到一個空數組 任何想法將是一個很大的幫助。我看過詳細的日誌記錄,但我不知道如何輸出計算方法來查找內容。顯然它似乎是空的,但出於調查的目的,至少這將是一個開始。PowerShell中無法通過陣列IPS添加到一個安全組

$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

回答

0

我在實驗室中測試,也有你的腳本一些錯誤。使用您的腳本,我無法獲得$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 

} 

將正確的值替換爲您的腳本。

0

我認爲這是一個空行,這樣你就不會得到任何虛擬機回:

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

所以,檢查$ VM變量,如果存在一些虛擬機的與這些參數。

相關問題