簡體   English   中英

Azure NSG-從輸入csv篩選某些IP

[英]Azure NSG - Filter certain IPs from input csv

我正在使用PowerShell創建Azure NSG,它將使用帶有安全規則的.csv文件中的輸入。 我正在使用以下腳本。

$NSG = Get-AzureRmNetworkSecurityGroup -Name test -ResourceGroupName RG-VM-QTY

foreach($rule in import-csv "SystemPath\inputfile.csv") 
{ 
$NSG | Add-AzureRmNetworkSecurityRuleConfig -Name $rule.name -Access Allow -Protocol $rule.protocol -Direction $rule.direction -Priority $rule.priority 
-SourceAddressPrefix $rule.source -SourcePortRange * 
-DestinationAddressPrefix $rule.destination -DestinationPortRange $rule.port 
}

$NSG | Set-AzureRmNetworkSecurityGroup

想要檢查是否存在限制添加特定IP的方法,可以說將127.0.0.1添加為任何規則中的源或目標。 如果.csv中存在IP 127.0.0.1,我可以進行任何檢查以避免完全創建NSG?

在此先感謝大家!! 干杯。

這是修改后的PowerShell腳本,其中添加了一個簡單的if條件,以檢查SourceAddressPrefix和DestinationAddressPrefix是否不完全是127.0.0.1

$NSG = Get-AzureRmNetworkSecurityGroup -Name test -ResourceGroupName RG-VM-QTY 

foreach($rule in import-csv "SystemPath\inputfile.csv") 
{
   # additional if condition to check that source or destination address prefix should not be 127.0.0.1
   if($rule.SourceAddressPrefix -ne "127.0.0.1" -And $rule.DestinationAddressPrefix -ne "127.0.0.1")
   { 
          $NSG | Add-AzureRmNetworkSecurityRuleConfig -Name $rule.name -Access Allow -Protocol $rule.protocol -Direction $rule.direction -Priority $rule.priority 
             -SourceAddressPrefix $rule.source -SourcePortRange * -DestinationAddressPrefix $rule.destination -DestinationPortRange $rule.port 
   }
} 

$NSG | Set-AzureRmNetworkSecurityGroup

現在,您的狀況非常容易檢查127.0.0.1,因此狀況是否足夠好。

如果您遇到更復雜的邏輯,可以考慮創建一個單獨的函數,例如ValidateRule(),該函數可以封裝所有條件並調用該函數以檢查是否應添加規則。

暫無
暫無

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

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