65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 PowerShell 和 CSV 将多个规则添加到 NSG

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (1投票)

2019年7月3日

CPOL

2分钟阅读

viewsIcon

16402

安全规则的解决方案存在无效端口范围问题

引言

有时,在 Azure 中创建 VNET 时,我们不知道要设置的 NSG 规则的确切数量。如果需要向特定的网络安全组添加更多 NSG 规则,可以使用简单的 .CSV 文件(其中包含要应用的规则)和 PowerShell 脚本,而不是进入门户并手动添加规则。

*问题 - 安全规则具有无效的端口范围

本示例解决了在尝试在源或目标中提供多个端口时发生的安全规则具有无效端口范围问题。

背景

以下是完成此练习的先决条件

  1. 对 PowerShell 脚本有基本了解
  2. 一个 Azure 订阅(你可以在 此处 试用免费试用版)和一个在资源组下可用的网络安全组 (NSG)
  3. 请注意,我在下面的文章中使用了 Azure PowerShell Az 模块,如果尚未迁移到此版本,则需要在下面的代码段中用 AzureRM 关键字替换 Az 命令。

Using the Code

创建一个 .CSV 文件,其中定义了所有规则,用于测试,我只添加了一条记录,你可以在你的 .CSV 文件中根据你的要求添加更多行。

CSV 文件中的规则

NSG 名称 NSG 规则名称

NSG
规则说明

NSG 规则访问权限 NSG 规则协议 NSG 规则方向 NSG 规则优先级 NSG 规则源地址前缀 NSG 规则源端口范围 NSG 规则目标-
地址前缀
NSG 规则目标
端口
Range
test-nsg-001 new-rdp-rule 允许 RDP 允许 tcp 入站 1010 互联网 * * 443;5300;1025-5000

现在,我们需要 powershell 脚本将这些规则附加到目标 NSG。

打开 PowerShell ISE(以管理员身份)并将以下代码添加到读取 .CSV 并将更改推送到 Azure。

param(
[Parameter(Mandatory=$True)]
[string]
$subscriptionId,
[Parameter(Mandatory=$True)]
[string]
$resourceGroupName,

[string]
$nsgName,

[string]
$nsgRuleName,
[string]
$nsgRuleDescription,
[string]
$nsgRuleAccess,
[string]
$nsgRuleProtocol,
[string]
$nsgRuleDirection,
[string]
$nsgRulePriority,
[string]
$nsgRuleSourceAddressPrefix,
[string]
$nsgRuleSourcePortRange,
[string]
$nsgRuleDestinationAddressPrefix,
[string]
$nsgRuleDestinationPortRange,
$csvFilePath = "C:\prakash\Add NSG Rules.csv"
)

# sign in
Write-Host "Logging in...";
Connect-AzAccount;

# select subscription
Write-Host "Selecting subscription '$subscriptionId'";
Select-AzSubscription -SubscriptionID $subscriptionId;

Import-Csv $csvFilePath |`
  ForEach-Object {
    $nsgName = $_."NSG Name"
    $nsgRuleName = $_."NSG Rule Name"
        $nsgRuleDescription = $_."NSG Rule Description"
    $nsgRuleAccess = $_."NSG Rule Access"
        $nsgRuleProtocol = $_."NSG Rule Protocol"
    $nsgRuleDirection = $_."NSG Rule Direction"
        $nsgRulePriority = $_."NSG Rule Priority"
    $nsgRuleSourceAddressPrefix = $_."NSG Rule Source Address Prefix"
        $nsgRuleSourcePortRange = $_."NSG Rule Source Port Range"
//    $nsgRuleDestinationAddressPrefix = $_."NSG Rule Destination Address Prefix"
        $nsgRuleDestinationPortRange = $_."NSG Rule Destination Port Range"

    #Getting the right NSG and setting new rule to it    
    $nsgRuleNameValue = Get-AzNetworkSecurityGroup 
       -Name $nsgName -ResourceGroupName $resourceGroupName | 
       Get-AzNetworkSecurityRuleConfig -Name $nsgRuleName -ErrorAction SilentlyContinue
    if($nsgRuleNameValue.Name -match $nsgRuleName){
       Write-Host "A rule with this name (" $nsgRuleNameValue.Name ") already exists"
       }
    else{
         Get-AzNetworkSecurityGroup -Name  $nsgName -ResourceGroupName $resourceGroupName | 
        Add-AzNetworkSecurityRuleConfig -Name $nsgRuleName 
                        -Description $nsgRuleDescription -Access `
        $nsgRuleAccess -Protocol $nsgRuleProtocol -Direction $nsgRuleDirection 
            -Priority $nsgRulePriority -SourceAddressPrefix $nsgRuleSourceAddressPrefix `
        -SourcePortRange $nsgRuleSourcePortRange 
              -DestinationAddressPrefix $nsgRuleDestinationAddressPrefix 
              -DestinationPortRange $nsgRuleDestinationPortRange.split(";") -Verbose | 
        Set-AzNetworkSecurityGroup -Verbose
       }
    }

关注点

在实施过程中,我遇到了一个有趣的错误,如果我们提供以 (,) 分隔的端口列表在 .CSV 文件的任何端口相关列中,则在运行 Powershell 脚本时会给出以下错误

Set-AzNetworkSecurityGroup : Security rule has invalid Port range. Value should be an
 integer OR integer range with '-' delimiter. Valid range 0-65535.
StatusCode: 400
ReasonPhrase: Bad Request

为了解决这个问题,我需要在 .CSV 文件中按以下格式提供端口值(多个端口和范围)

 443;5300;1025-5000

然后在我的 Powershell 脚本中,我需要使用 split(";") 函数以及该特定参数,其方式如下

-DestinationPortRange $nsgRuleDestinationPortRange.split(";")

请让我知道你对这个技巧的看法,并祝你编码愉快!

历史

  • 2019 年 7 月 3 日:首次发布
© . All rights reserved.