簡體   English   中英

無法使用 az cli 更新 Azure 網絡安全組

[英]Unable to update Azure Network Security Group using az cli

我正在嘗試通過 cli 更新網絡安全組 (SourceAddressPrefixes)。 首先,az cmdlet Set-AzNetworkSecurityRuleConfig似乎已損壞; 命令中的 output 表明它已成功,但 NSG 實際上沒有發生任何變化。 其他人對此有所抱怨,但 MSFT 尚未修復。

話雖如此,我嘗試了一種解決方法,將 NSG 保存在變量中,設置SourceAddressPrefixes ,並在之后更新 NSG:

$SaContext = (Get-AzStorageAccount -ResourceGroupName $RGName -Name $SAName).Context
$table = (Get-AzStorageTable -Name $TableName -Context $SaContext).CloudTable
$IPs = (Get-AzTableRow -Table $table).IP
$IPs = '"{0}"' -f ($IPs -join '","') # Updates the IPs to be double-quoted and separated by commas


$NSG = Get-AzNetworkSecurityGroup -ResourceGroupName $MyResourceGroup -Name $NSGName
($nsg.SecurityRules | Where-Object {$_.Name -eq 'HTTPS'}).SourceAddressPrefix = $IPList
$NSG | Set-AzNetworkSecurityGroup | Get-AzNetworkSecurityRuleConfig -Name $RuleName | Format-Table -AutoSize

上面代碼的問題是 cmdlet Set-AzNetworkSecurityGroup不會排除值類型system.string 它只接受System.Collections.Generic.List[System.String] 因此,我執行以下操作:

$IPList = New-Object System.Collections.Generic.List[string]
$IPList.Add($IPs)

現在,以前的Set-AzNetworkSecurityGroup接受數組,但現在命令失敗,因為數組值沒有用逗號雙引號來分隔它們。 不知道此時該做什么。

下面的實際錯誤消息:

Cannot convert the (ip addresses here) value of type [system.string] to type "Systems.Collections.Generic.IList[System.String]

一旦我轉換我的變量(數組)以匹配該要求,錯誤就是: nsgRule has invalid Address Prefix. Value Provided (ip addresses here) statuscode:400 nsgRule has invalid Address Prefix. Value Provided (ip addresses here) statuscode:400我確定這是因為轉換數組會刪除雙引號和逗號。

如果你想更新一個網絡安全組規則的源地址前綴,它的值應該像

192.162.0.1
192.162.1.1
...

例如


$nsg= Get-AzNetworkSecurityGroup -Name  $NSGName -ResourceGroupName $MyResourceGroup

$IPList = New-Object System.Collections.Generic.List[string]
$IPList.Add("192.162.0.1")
$IPList.Add("192.162.1.1")

($nsg.SecurityRules | Where-Object {$_.Name -eq 'Port_8080'}).SourceAddressPrefix =$IPList

$nsg|Set-AzNetworkSecurityGroup |  Get-AzNetworkSecurityRuleConfig -Name "Port_8080" | Format-Table -AutoSize

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM