Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SQLtattoo/9f4f47d2f782652d850f7060561ca57c to your computer and use it in GitHub Desktop.
Save SQLtattoo/9f4f47d2f782652d850f7060561ca57c to your computer and use it in GitHub Desktop.
Assign Network Security Group to Azure VMs
# The "nsg-nointernet" Network Security Group must exist
$VMs = Get-AzVM | where Name -Like "APPSRV-*"
foreach ($VM in $VMs) {
$nic = Get-AzNetworkInterface -ResourceId $VM.NetworkProfile.NetworkInterfaces.id
$Nsg = Get-AzNetworkSecurityGroup -Name "nsg-nointernet"
Write-Output "Adding NSG: $Nsg to $VM"
$nic.NetworkSecurityGroup = $Nsg
$nic | Set-AzNetworkInterface
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment