添加入站网络安全组规则

本示例脚本创建网络安全组规则,以允许端口 8081 上的入站流量。 该脚本获取网络安全组、创建新的网络安全配置规则,并更新网络安全组。 根据需要自定义参数。

注意

建议使用 Azure Az PowerShell 模块与 Azure 交互。 请参阅安装 Azure PowerShell 以开始使用。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

必要时,请使用 Azure PowerShell 指南中的说明安装 Azure PowerShell。

示例脚本

Connect-AzAccount -Environment AzureChinaCloud
Get-AzSubscription
Set-AzContext -SubscriptionId "yourSubscriptionID"

$RGname="sfclustertutorialgroup"
$port=8081
$rulename="allowAppPort$port"
$nsgname="sf-vnet-security"

# Get the NSG resource
$nsg = Get-AzNetworkSecurityGroup -Name $nsgname -ResourceGroupName $RGname

# Add the inbound security rule.
$nsg | Add-AzNetworkSecurityRuleConfig -Name $rulename -Description "Allow app port" -Access Allow `
    -Protocol * -Direction Inbound -Priority 3891 -SourceAddressPrefix "*" -SourcePortRange * `
    -DestinationAddressPrefix * -DestinationPortRange $port

# Update the NSG.
$nsg | Set-AzNetworkSecurityGroup

脚本说明

此脚本使用以下命令。 表中的每条命令均链接到特定于命令的文档。

命令 注释
Get-AzResource 获取 Microsoft.Network/networkSecurityGroups 资源。
Get-AzNetworkSecurityGroup 按名称获取网络安全组。
Add-AzNetworkSecurityRuleConfig 将网络安全规则配置添加到网络安全组。
Set-AzNetworkSecurityGroup 设置网络安全组的目标状态。

后续步骤

有关 Azure PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档