使用 WMI 提供程序管理服务和网络设置
WMI 提供程序是可供 Microsoft 管理控制台 (MMC) 用来管理 SQL Server 服务和网络协议的已发布接口。 在 SMO 中,ManagedComputer 对象表示 WMI 提供程序。
ManagedComputer 对象的运行与通过 Server 对象建立的与 SQL Server 实例的连接无关,而是使用 Windows 凭据连接到 WMI 服务。
示例
若要使用所提供的任何代码示例,您必须选择创建应用程序所需的编程环境、编程模板和编程语言。有关详细信息,请参阅 SQL Server 联机丛书中的“如何在 Visual Studio .NET 中创建 Visual Basic SMO 项目”或“如何在 Visual Studio .NET 中创建 Visual C# SMO 项目”。
对于使用 SQL Server WMI 提供程序的程序来说,必须包括 Imports 语句才能限定 WMI 命名空间。 请在应用程序的其他 Imports 语句之后、任何声明之前插入该语句,例如:
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common
Imports Microsoft.SqlServer.Management.Smo.Wmi
在 Visual Basic 中停止并重新启动 SQL Server 实例的 Microsoft SQL Server 服务
该代码示例演示了如何通过使用 SMO ManagedComputer 对象停止和启动服务。 该示例为用于配置管理的 WMI 提供程序提供了接口。
'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
在 Visual Basic 中使用 URN 字符串启用服务器协议
此代码示例演示如何使用 URN 对象标识服务器协议并启用该协议。
'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()
在 PowerShell 中使用 URN 字符串启用服务器协议
此代码示例演示如何使用 URN 对象标识服务器协议并启用该协议。
#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()
在 Visual C# 中停止和启动服务
该代码示例演示了如何停止和启动 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();
}
}
在 PowerShell 中停止和启动服务
该代码示例演示了如何停止和启动 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"