Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Assign a Network Security Group in Azure using PowerShell

September 2, 2022
in Blog, Microsoft Azure, Powershell
0
ADVERTISEMENT

Table of Contents

An NSG comes with some default rules to allow the essential services to run on the new VMs, and the cloud administrator is responsible for managing all other traffic required. All rules will be evaluated based on their priority using these following five types of information: source, source port, destination, destination port, and protocol.

Understanding the basic PowerShell cmdlets

Before diving into the cmdlets to configure either a VNet or vNIC, we need to get acquainted with some basic PowerShell cmdlets that are required when managing NSGs.

Get-AzResourceGroup | Select ResourceGroupName
Get-AzNetworkInterface -ResourceGroupName “<ResourceGroupName>”
Get-AzNetworkSecurityGroup | Select Name,ResourceGroupName,Location
Get-AzVM | select Name,ResourceGroupName,Location -ExpandProperty NetworkProfile | fl

Managing network security groups at the virtual network interface level

If you want something more specific and are applying an NSG at the VM level, in this case, the Set-AzNetworkInterface cmdlet will be your tool of choice to perform this task.

The first step is to retrieve the network security groups and save the specific NSG into a variable. These two cmdlets are required:

$ResourceGroupName = "EASTUS-RG"
$Location = "eastus"
$SecurityGroupName = "EASTUS-nsg"
New-AzNetworkSecurityGroup `
    -Name $SecurityGroupName `
    -ResourceGroupName $ResourceGroupName  `
    -Location $Location
$nsg = Get-AzNetworkSecurityGroup `
    -ResourceGroupName $ResourceGroupName `
    -Name $SecurityGroupName

The second step is to list all vNICs available. First, find the vNIC attached to the VM that you want to apply the NSG. Then, we need to add the vNIC to a PowerShell variable.

Get-AzVM | select Name,ResourceGroupName,Location -ExpandProperty NetworkProfile | fl
$vNIC = Get-AzNetworkInterface `
    -ResourceGroupName $ResourceGroupName `
    -Name "vm-001-NIC"

The final step is to use the variables that we created in the previous step and apply the changes. We are going to do that using the $vNIC variable that we have just populated and configured the network security group. We are going to use the $nsg variable that we defined in the first step of this section. The process to apply the changes is to run the Set-AzNetworkInterface as an output of the $vNIC variable.

$vNIC.NetworkSecurityGroup = $nsg
$vNIC | Set-AzNetworkInterface
ADVERTISEMENT

Managing NSGs at VNet level

The recommendation is always to reduce the number of network security groups, and by doing that, we can have smaller building blocks applied to a subnet instead of a specific VM.

To assign network security groups to a VNet/Subnet level is using the Set-AzVirtualNetworkSubnetConfig cmdlet, which associates an NSG to a virtual network (VNet).

Get-AzNetworkSecurityGroup | Select Name,ResourceGroupName,Location
$nsg = Get-AzNetworkSecurityGroup -ResourceGroupName $rg -Name "NSGName"
Get-AzVirtualNetwork | select Name
$VNet = get-azvirtualnetwork -Name "VNet-Name"
Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $VNet | select Name,AddressPrefix
$VNetSubnet = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $VNet -Name default
Set-AzVirtualNetworkSubnetConfig `
    -Name $VNetSubnet.Name `
    -VirtualNetwork $VNet `
    -AddressPrefix $VNetSubnet.AddressPrefix `
    -NetworkSecurityGroup $nsg`
$VNet | Set-AzVirtualNetwork

The results can be seen in the Azure Portal. Logged on to the portal, click on the VNet, click on Subnets, select the desired subnet, check the network security group to see if there is an NSG associated to the subnet.

ADVERTISEMENT
5/5 - (1 vote)
Previous Post

How to Create a Azure VM using PowerShell

Next Post

How to Get Microsoft Teams Exploratory License in Microsoft 365

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory