共用方式為


新增輸入網路安全性群組規則

本範例指令碼會建立網路安全性群組規則,以允許連接埠 8081 上的輸入流量。 指令碼會取得網路安全性群組、建立新的網路安全性設定規則,並更新網路安全性群組。 視需要自訂參數。

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 若要開始使用,請參閱 安裝 Azure PowerShell。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

您可以視需要使用 Azure PowerShell 指南中的指示來安裝 Azure PowerShell。

範例指令碼

Login-AzAccount
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

指令碼說明

此指令碼會使用下列命令。 下表中的每個命令都會連結至命令特定的文件。

Command 注意
Get-AzResource 取得 Microsoft.Network/networkSecurityGroups 資源。
Get-AzNetworkSecurityGroup 依名稱取得網路安全性群組。
Add-AzNetworkSecurityRuleConfig 將網路安全性規則設定新增到網路安全性群組中。
Set-AzNetworkSecurityGroup 設定網路安全性群組的目標狀態。

下一步

如需有關 Azure PowerShell 模組的詳細資訊,請參閱 Azure PowerShell 文件