using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Smo.Wmi;

namespace WMINetwork
{
    class Program
    {
        static void Main(string[] args)
        {
            // Enable and disable network protocols using WMI

            ManagedComputer m = new ManagedComputer();

            for (int i = 0; i < m.ServerInstances.Count;i++ )
            {
                if (m.ServerInstances[i].Name == "MSSQLSERVER")
                    for (int j = 0; j < m.ServerInstances[i].ServerProtocols.Count; j++)
                    {
                        Console.WriteLine(m.ServerInstances[i].ServerProtocols[j].Name);
                        Console.WriteLine(m.ServerInstances[i].ServerProtocols[j].IsEnabled);

                        if (j == 0)
                        {
                            foreach (Property pp in m.ServerInstances[i].ServerProtocols[j].Properties)
                                Console.WriteLine(pp.Name);
                        }

                        // This sets remote connections for NP
                        // You must stop and restart server for change
                        // to take effect
                        if (m.ServerInstances[i].ServerProtocols[j].Name == "Np")
                        {
                            m.ServerInstances[i].ServerProtocols[j].IsEnabled = true;
                            m.ServerInstances[i].ServerProtocols[j].Alter();          
                        }
                    }
            }
           
        }
    }
}
