To associate the NSG that has been created earlier to a subnet, you can run the following cmdlet:
Get-AzureRmVirtualNetwork -ResourceGroupName PacktPub -Name PSVNet| Set-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork PSVNet -Name NSubnet -AddressPrefix 192.168.1.0/24 -NetworkSecurityGroup $NSG
Then, you need to save your configuration by running the following cmdlet:
Set-AzureRmVirtualNetwork -VirtualNetwork PSVNet