Verwalten von Diensten und Netzwerkeinstellungen durch die Nutzung von WMI-Anbieter

Der WMI-Anbieter ist eine veröffentlichte Schnittstelle, die von der Microsoft Management Console (MMC) zur Verwaltung von SQL Server-Diensten und Netzwerkprotokollen verwendet wird. In SMO stellt das ManagedComputer-Objekt den WMI-Anbieter dar.

Das ManagedComputer-Objekt arbeitet unabhängig von der mit dem Server-Objekt zu einer Instanz von SQL Server hergestellten Verbindung und nutzt Windows-Anmeldeinformationen für die Verbindung zum WMI-Dienst.

Beispiel

Um die bereitgestellten Codebeispiele verwenden zu können, müssen Sie die Programmierumgebung, die Programmiervorlage und die Programmiersprache wählen, in der die Anwendung erstellt werden soll. Weitere Informationen finden Sie in der SQL Server-Onlinedokumentation unter "Vorgehensweise: Erstellen eines Visual Basic SMO-Projekts in Visual Studio .NET" oder "Vorgehensweise: Erstellen eines Visual C# SMO-Projekts in Visual Studio .NET".

Für Programme, die den SQL Server-WMI-Anbieter verwenden, müssen Sie die Imports-Anweisung einschließen, um den WMI-Namespace zu qualifizieren. Fügen Sie die Anweisung nach den anderen Imports-Anweisungen und vor jeglichen Deklarationen in der Anwendung wie folgt ein:

Imports Microsoft.SqlServer.Management.Smo

Imports Microsoft.SqlServer.Management.Common

Imports Microsoft.SqlServer.Management.Smo.Wmi

Beenden und erneutes Starten des Microsoft SQL Server-Diensts zur Instanz von SQL Server in Visual Basic

Dieses Codebeispiel zeigt, wie Dienste mit dem SMO-ManagedComputer-Objekt beendet und gestartet werden. Hierdurch wird eine Schnittstelle zum WMI-Anbieter für die Konfigurationsverwaltung bereitgestellt.

'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

Aktivieren eines Serverprotokolls mit einer URN-Zeichenfolge in Visual Basic

Das Codebeispiel veranschaulicht, wie ein Serverprotokoll mithilfe eines URN-Objekts gekennzeichnet und das Protokoll anschließend aktiviert wird.

'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()

Aktivieren eines Serverprotokolls mit einer URN-Zeichenfolge in PowerShell

Das Codebeispiel veranschaulicht, wie ein Serverprotokoll mithilfe eines URN-Objekts gekennzeichnet und das Protokoll anschließend aktiviert wird.

#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()

Starten und Beenden eines Diensts in Visual C#

Das Codebeispiel zeigt, wie eine Instanz von SQL Server beendet und gestartet wird.

{ 
   //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();
   } 
}

Starten und Beenden eines Diensts in PowerShell

Das Codebeispiel zeigt, wie eine Instanz von SQL Server beendet und gestartet wird.

#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"

Siehe auch

Konzepte

Konzepte des WMI-Anbieters für die Konfigurationsverwaltung