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(),它可以封装所有条件并调用该函数来检查是否应添加规则。
我正在使用 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(),它可以封装所有条件并调用该函数来检查是否应添加规则。