Gestione di servizi e di impostazioni di rete tramite il provider WMI

Il provider WMI è un'interfaccia pubblicata utilizzata da Microsoft Management Console (MMC) per gestire i servizi SQL Server e i protocolli di rete. In SMO l'oggetto ManagedComputer rappresenta il provider WMI.

L'oggetto ManagedComputer funziona indipendentemente dalla connessione stabilita con l'oggetto Server a un'istanza di SQL Server e utilizza credenziali di Windows per connettersi al servizio WMI.

Esempio

Per utilizzare qualsiasi esempio di codice fornito, è necessario scegliere l'ambiente di programmazione, il modello di programmazione e il linguaggio di programmazione per la creazione dell'applicazione. Per ulteriori informazioni, vedere "Procedura: Creare un progetto Visual Basic SMO in Visual Studio .NET" o "Procedura: Creare un progetto Visual C# SMO in Visual Studio .NET" nella documentazione online di SQL Server.

Per i programmi che utilizzano il provider WMI di SQL Server, è necessario includere l'istruzione Imports per qualificare lo spazio dei nomi WMI. Inserire l'istruzione dopo le altre istruzioni Imports, ma prima di qualsiasi dichiarazione nell'applicazione, ad esempio:

Imports Microsoft.SqlServer.Management.Smo

Imports Microsoft.SqlServer.Management.Common

Imports Microsoft.SqlServer.Management.Smo.Wmi

Interruzione e riavvio del servizio Microsoft SQL Server nell'istanza di SQL Server in Visual Basic

In questo esempio di codice viene illustrato come interrompere e avviare servizi tramite l'oggetto ManagedComputer SMO. Viene fornita un'interfaccia al provider WMI per la gestione della configurazione.

'Declare and create an instance of the ManagedComputer object that represents the WMI Provider services.
Dim mc As ManagedComputer
mc = New ManagedComputer()
'Iterate through each service registered with the WMI Provider.
Dim svc As Service
For Each svc In mc.Services
    Console.WriteLine(svc.Name)
Next
'Reference the Microsoft SQL Server service.
svc = mc.Services("MSSQLSERVER")
'Stop the service if it is running and report on the status continuously until it has stopped.
If svc.ServiceState = ServiceState.Running Then
    svc.Stop()

    Console.WriteLine(String.Format("{0} service state is {1}", svc.Name, svc.ServiceState))
    Do Until String.Format("{0}", svc.ServiceState) = "Stopped"
        Console.WriteLine(String.Format("{0}", svc.ServiceState))
        svc.Refresh()
    Loop
    Console.WriteLine(String.Format("{0} service state is {1}", svc.Name, svc.ServiceState))
    'Start the service and report on the status continuously until it has started.
    svc.Start()
    Do Until String.Format("{0}", svc.ServiceState) = "Running"
        Console.WriteLine(String.Format("{0}", svc.ServiceState))
        svc.Refresh()
    Loop
    Console.WriteLine(String.Format("{0} service state is {1}", svc.Name, svc.ServiceState))

Else
    Console.WriteLine("SQL Server service is not running.")
End If

Abilitazione di un protocollo server utilizzando una stringa URN in Visual Basic

Nell'esempio di codice viene illustrato come identificare un protocollo server utilizzando un oggetto URN e quindi abilitare il protocollo.

'Declare the ManagedComputer WMI interface.
Dim mc As New ManagedComputer()
'Create a URN object that represents the TCP server protocol.
Dim u As New Urn("ManagedComputer[@Name=MYPC']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']")
Declare the serverProtocol variable and return the ServerProtocol object.
Dim sp As ServerProtocol
sp = mc.GetSmoObject(u)
'Enable the protocol.
sp.IsEnabled = True
sp.Alter()

Abilitazione di un protocollo server utilizzando una stringa URN in Visual C#

Nell'esempio di codice viene illustrato come identificare un protocollo server utilizzando un oggetto URN e quindi abilitare il protocollo.

{ 
   //Declare and create an instance of the ManagedComputer 
   //object that represents the WMI Provider services. 
   ManagedComputer mc; 
   mc = new ManagedComputer(); 
   //Iterate through each service registered with the WMI Provider. 
   Service svc; 
   foreach ( svc in mc.Services) { 
      Console.WriteLine(svc.Name); 
   } 
//Reference the Microsoft SQL Server service. 
   svc = mc.Services("MSSQLSERVER"); 
//Stop the service if it is running and report on the status
// continuously until it has stopped. 
   if (svc.ServiceState == ServiceState.Running) { 
      svc.Stop(); 
      Console.WriteLine(string.Format("{0} service state is {1}", svc.Name, svc.ServiceState)); 
      while (!(string.Format("{0}", svc.ServiceState) == "Stopped")) { 
         Console.WriteLine(string.Format("{0}", svc.ServiceState)); 
          svc.Refresh(); 
      } 
      Console.WriteLine(string.Format("{0} service state is {1}", svc.Name, svc.ServiceState)); 
//Start the service and report on the status continuously 
//until it has started. 
      svc.Start(); 
      while (!(string.Format("{0}", svc.ServiceState) == "Running")) { 
         Console.WriteLine(string.Format("{0}", svc.ServiceState)); 
         svc.Refresh(); 
      } 
      Console.WriteLine(string.Format("{0} service state is {1}", svc.Name, svc.ServiceState)); 
   } 
   else { 
      Console.WriteLine("SQL Server service is not running."); 
   } 
}