Configure network security groups with PowerShell - Azure Local | Microsoft Learn

Applies to: Azure Local 2311.2 and later; Windows Server 2022, Windows Server 2019, Windows Server 2016

This article provides instructions for configuring network security groups (NSGs) to manage data traffic flow using Datacenter Firewall for Software Defined Networking (SDN) in Azure Local using Windows PowerShell. You enable and configure Datacenter Firewall by creating network security groups that get applied to a subnet or a network interface.

The example scripts in this article use Windows PowerShell commands exported from the NetworkController module. You can also use Windows Admin Center to configure and manage network security groups.

Configure Datacenter Firewall to allow all traffic

Once you deploy SDN, you should test for basic network connectivity in your new environment. To accomplish this, create a rule for Datacenter Firewall that allows all network traffic, without restriction.

Use the entries in the following table to create a set of rules that allow all inbound and outbound network traffic.

| Source IP | Destination IP | Protocol | Source Port | Destination Port | Direction | Action | Priority |

| --- | --- | --- | --- | --- | --- | --- | --- |

| * | * | All | * | * | Inbound | Allow | 100 |

| * | * | All | * | * | Outbound | Allow | 110 |

In this example, you create a network security group with two rules:

  1. AllowAll_Inbound - allows all network traffic to pass into the network interface where this network security group is configured.

  2. AllowAllOutbound - allows all traffic to pass out of the network interface. This network security group, identified by the resource ID "AllowAll-1" is now ready to be used in virtual subnets and network interfaces.

You can run this command from any computer that has access to the Network Controller REST endpoint. First, open a PowerShell session. In this example, use the Enter-PSSession cmdlet and replace <computer-name> with the name of the computer that has the Network Controller REST endpoint.


Enter-PSSession <computer-name>

Then, run the following script to create the network security group:


$ruleproperties = new-object Microsoft.Windows.NetworkController.AclRuleProperties

$ruleproperties.Protocol = "All"

$ruleproperties.SourcePortRange = "0-65535"

$ruleproperties.DestinationPortRange = "0-65535"

$ruleproperties.Action = "Allow"

$ruleproperties.SourceAddressPrefix = "*"

$ruleproperties.DestinationAddressPrefix = "*"

$ruleproperties.Priority = "100"

$ruleproperties.Type = "Inbound"

$ruleproperties.Logging = "Enabled"

$aclrule1 = new-object Microsoft.Windows.NetworkController.AclRule

$aclrule1.Properties = $ruleproperties

$aclrule1.ResourceId = "AllowAll_Inbound"

$ruleproperties = new-object Microsoft.Windows.NetworkController.AclRuleProperties

$ruleproperties.Protocol = "All"

$ruleproperties.SourcePortRange = "0-65535"

$ruleproperties.DestinationPortRange = "0-65535"

$ruleproperties.Action = "Allow"

$ruleproperties.SourceAddressPrefix = "*"

$ruleproperties.DestinationAddressPrefix = "*"

$ruleproperties.Priority = "110"

$ruleproperties.Type = "Outbound"

$ruleproperties.Logging = "Enabled"

$aclrule2 = new-object Microsoft.Windows.NetworkController.AclRule

$aclrule2.Properties = $ruleproperties

$aclrule2.ResourceId = "AllowAll_Outbound"

$acllistproperties = new-object Microsoft.Windows.NetworkController.AccessControlListProperties

$acllistproperties.AclRules = @($aclrule1, $aclrule2)

New-NetworkControllerAccessControlList -ResourceId "AllowAll" -Properties $acllistproperties -ConnectionUri <NC REST FQDN>

Note

The Windows PowerShell command reference for Network Controller is in the Network Controller cmdlets.

Use network security groups to limit traffic on a subnet

In this example, you create a network security group that prevents virtual machines (VMs) within the 192.168.0.0/24 subnet from communicating with each other. This type of network security group is useful for limiting the ability of an attacker to spread laterally within the subnet, while still allowing the VMs to receive requests from outside of the subnet, and to communicate with other services on other subnets.

| Source IP | Destination IP | Protocol | Source Port | Destination Port | Direction | Action | Priority |

| --- | --- | --- | --- | --- | --- | --- | --- |

| 192.168.0.1 | * | All | * | * | Inbound | Allow | 100 |

| * | 192.168.0.1 | All | * | * | Outbound | Allow | 101 |

| 192.168.0.0/24 | * | All | * | * | Inbound | Block | 102 |

| * | 192.168.0.0/24 | All | * | * | Outbound | Block | 103 |

| * | * | All | * | * | Inbound | Allow | 104 |

| * | * | All | * | * | Outbound | Allow | 105 |

The network security group created by the example script below, identified by the resource ID Subnet-192-168-0-0, can now be applied to a virtual network subnet that uses the "192.168.0.0/24" subnet address. Any network interface that is attached to that virtual network subnet automatically gets the above network security group rules applied.

The following is an example script to create this network security group using the Network Controller REST API:

```PowerShell

import-module networkcontroller

$ncURI = "https://mync.contoso.local"

$aclrules = @()

$ruleproperties = new-object Microsoft.Windows.NetworkController.AclRuleProperties

$ruleproperties.Protocol = "All"

$ruleproperties.SourcePortRange = "0-65535"

$ruleproperties.DestinationPortRange = "0-65535"

$ruleproperties.Action = "Allow"

$ruleproperties.SourceAddressPrefix = "192.168.0.1"

$ruleproperties.DestinationAddressPrefix = "*"

$ruleproperties.Priority = "100"

$ruleproperties.Type = "Inbound"

$ruleproperties.Logging = "Enabled"

$aclrule = new-object Microsoft.Windows.NetworkController.AclRule

$aclrule.Properties = $ruleproperties

$aclrule.ResourceId = "AllowRouter_Inbound"

$aclrules += $aclrule

$ruleproperties = new-object Microsoft.Windows.NetworkController.AclRuleProperties

$ruleproperties.Protocol = "All"

$ruleproperties.SourcePortRange = "0-65535"

$ruleproperties.DestinationPortRange = "0-65535"

$ruleproperties.Action = "Allow"

$ruleproperties.SourceAddressPrefix = "*"

$ruleproperties.DestinationAddressPrefix = "192.168.0.1"

$ruleproperties.Priority = "101"

$ruleproperties.Type = "Outbound"

$ruleproperties.Logging = "Enabled"

$aclrule = new-object Microsoft.Windows.NetworkController.AclRule

$aclrule.Properties = $ruleproperties

$aclrule.ResourceId = "AllowRouter_Outbound"

$aclrules += $aclrule

$ruleproperties = new-object Microsoft.Windows.NetworkController.AclRuleProperties

$ruleproperties.Protocol = "All"

$ruleproperties.SourcePortRange = "0-65535"

$ruleproperties.DestinationPortRange = "0-65535"

$ruleproperties.Action = "Deny"

$ruleproperties.SourceAddressPrefix = "192.168.0.0/24"

$ruleproperties.DestinationAddressPrefix = "*"

$ruleproperties.Priority = "102"

$ruleproperties.Type = "Inbound"

$ruleproperties.Logging = "Enabled"

$aclrule = new-object Microsoft.Windows.NetworkController.AclRule

$aclrule.Properties = $ruleproperties

$aclrule.ResourceId = "DenySubnet_Inbound"

$aclrules += $aclrule

$ruleproperties = new-object Microsoft.Windows.NetworkController.AclRuleProperties

$ruleproperties.Protocol = "All"

$ruleproperties.SourcePortRange = "0-65535"

$ruleproperties.DestinationPortRange = "0-65535"

$ruleproperties.Action = "Deny"

$ruleproperties.SourceAddressPrefix = "*"

$ruleproperties.DestinationAddressPrefix = "192.168.0.0/24"

$ruleproperties.Priority = "103"

$ruleproperties.Type = "Outbound"

$ruleproperties.Logging = "Enabled"

$aclrule = new-object Microsoft.Windows.NetworkController.AclRule

$aclrule.Properties = $ruleproperties

$aclrule.ResourceId = "DenySubnet_Outbound"

$ruleproperties = new-object Microsoft.Windows.NetworkController.AclRuleProperties

$ruleproperties.Protocol = "All"

$ruleproperties.SourcePortRange = "0-65535"

$ruleproperties.DestinationPortRange = "0-65535"

$ruleproperties.Action = "Allow"

$ruleproperties.SourceAddressPrefix = "*"

$ruleproperties.DestinationAddressPrefix = "*"

$ruleproperties.Priority = "104"

$ruleproperties.Type = "Inbound"

$ruleproperties.Logging = "Enabled"

$aclrule = new-object Microsoft.Windows.NetworkController.AclRule

$aclrule.Properties = $ruleproperties

$aclrule.ResourceId = "AllowAll_Inbound"

$aclrules += $aclrule

$ruleproperties = new-object Microsoft.Windows.NetworkController.AclRuleProperties