Condividi tramite


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

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

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

Esempio

Per usare qualsiasi esempio di codice fornito, è necessario scegliere l'ambiente di programmazione, il modello di programmazione e il linguaggio di programmazione in cui creare l'applicazione. Per altre informazioni, vedere "Procedura: Creare un progetto SMO di Visual Basic in Visual Studio .NET" o "Procedura: Creare un progetto SMO visual C# in Visual Studio .NET" nella documentazione online di SQL Server.

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

Imports Microsoft.SqlServer.Management.Smo

Imports Microsoft.SqlServer.Management.Common

Imports Microsoft.SqlServer.Management.Smo.Wmi

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

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

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

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

'This program must run with administrator privileges.  
        '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='V-ROBMA3']/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  
  
        'propagate back to the service  
        sp.Alter()  

Abilitazione di un protocollo server tramite una stringa URN in PowerShell

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

#This example shows how to identify a server protocol using a URN object, and then enable the protocol  
#This program must run with administrator privileges.  
  
#Load the assembly containing the classes used in this example  
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement")  
  
#Get a managed computer instance  
$mc = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer  
  
#Create a URN object that represents the TCP server protocol  
#Change 'MyPC' to the name of the your computer   
$urn = New-Object -TypeName Microsoft.SqlServer.Management.Sdk.Sfc.Urn -argumentlist "ManagedComputer[@Name='MyPC']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']"  
  
#Get the protocol object  
$sp = $mc.GetSmoObject($urn)  
  
#enable the protocol on the object  
$sp.IsEnabled = $true  
  
#propagate back to actual service  
$sp.Alter()  

Avvio e arresto di un servizio in Visual C#

Nell'esempio di codice viene illustrato come arrestare e avviare un'istanza di SQL Server.

{   
   //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.   
  
   foreach (Service svc in mc.Services)  
   {   
      Console.WriteLine(svc.Name);   
   }   
//Reference the Microsoft SQL Server service.   
  Service Mysvc = mc.Services["MSSQLSERVER"];   
//Stop the service if it is running and report on the status  
// continuously until it has stopped.   
   if (Mysvc.ServiceState == ServiceState.Running) {   
      Mysvc.Stop();   
      Console.WriteLine(string.Format("{0} service state is {1}", Mysvc.Name, Mysvc.ServiceState));   
      while (!(string.Format("{0}", Mysvc.ServiceState) == "Stopped")) {   
         Console.WriteLine(string.Format("{0}", Mysvc.ServiceState));   
          Mysvc.Refresh();   
      }   
      Console.WriteLine(string.Format("{0} service state is {1}", Mysvc.Name, Mysvc.ServiceState));   
//Start the service and report on the status continuously   
//until it has started.   
      Mysvc.Start();   
      while (!(string.Format("{0}", Mysvc.ServiceState) == "Running")) {   
         Console.WriteLine(string.Format("{0}", Mysvc.ServiceState));   
         Mysvc.Refresh();   
      }   
      Console.WriteLine(string.Format("{0} service state is {1}", Mysvc.Name, Mysvc.ServiceState));  
      Console.ReadLine();  
   }   
   else {   
      Console.WriteLine("SQL Server service is not running.");  
      Console.ReadLine();  
   }   
}  

Avvio e arresto di un servizio in PowerShell

Nell'esempio di codice viene illustrato come arrestare e avviare un'istanza di SQL Server.

#Load the assembly containing the objects used in this example  
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement")  
  
#Get a managed computer instance  
$mc = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer  
  
#List out all sql server instnces running on this mc  
foreach ($Item in $mc.Services){$Item.Name}  
  
#Get the default sql server datbase engine service  
$svc = $mc.Services["MSSQLSERVER"]  
  
# for stopping and starting services PowerShell must run as administrator  
  
#Stop this service  
$svc.Stop()  
$svc.Refresh()  
while ($svc.ServiceState -ne "Stopped")  
{  
$svc.Refresh()  
$svc.ServiceState  
}  
"Service" + $svc.Name + " is now stopped"  
"Starting " + $svc.Name  
$svc.Start()  
$svc.Refresh()  
while ($svc.ServiceState -ne "Running")  
{  
$svc.Refresh()  
$svc.ServiceState  
}  
$svc.ServiceState  
"Service" + $svc.Name + "is now started"

Vedere anche

Concetti relativi al provider WMI per la gestione della configurazione