Skip to content

Commit b0b858e

Browse files
authored
Merge pull request #29218 from rwestMSFT/rw-1130-refresh-wmi
Refresh WMI provider service management article
2 parents 3e13bbc + 5f2fad3 commit b0b858e

1 file changed

Lines changed: 174 additions & 166 deletions

File tree

Lines changed: 174 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,52 @@
11
---
2-
title: "Managing Services and Network Settings by Using WMI Provider"
3-
description: "Managing Services and Network Settings by Using WMI Provider"
2+
title: Manage services and network settings with the WMI provider
3+
description: Manage SQL Server services and network settings with the WMI provider in MMC.
44
author: "markingmyname"
55
ms.author: "maghan"
6-
ms.date: "08/06/2017"
6+
ms.reviewer: randolphwest
7+
ms.date: 11/30/2023
78
ms.service: sql
89
ms.topic: "reference"
910
helpviewer_keywords:
1011
- "WMI provider [SMO]"
1112
- "services [SQL Server], SMO"
1213
- "network settings [SMO]"
1314
- "monitoring [SMO]"
14-
monikerRange: "=azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current"
15+
monikerRange: "=azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current"
1516
---
16-
# Managing Services and Network Settings by Using WMI Provider
17+
# Manage services and network settings with the WMI provider
18+
1719
[!INCLUDE [sqlserver2022](../../../includes/applies-to-version/sqlserver2022.md)]
1820

19-
The WMI provider is a published interface that is used by [!INCLUDE[msCoName](../../../includes/msconame-md.md)] Management Console (MMC) to manage the [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] services and network protocols. In SMO, the <xref:Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer> object represents the WMI Provider.
20-
21-
The <xref:Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer> object operates independently of the connection established with the <xref:Microsoft.SqlServer.Management.Smo.Server> object to an instance of [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)], and uses Windows credentials to connect to the WMI service.
22-
23-
## Example
24-
To use any code example that is provided, you will have to choose the programming environment, the programming template, and the programming language in which to create your application. For more information, see [Create a Visual C&#35; SMO Project in Visual Studio .NET](../../../relational-databases/server-management-objects-smo/how-to-create-a-visual-csharp-smo-project-in-visual-studio-net.md).
25-
26-
27-
For programs that use the [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] WMI provider, you must include the **Imports** statement to qualify the WMI namespace. Insert the statement after the other **Imports** statements, before any declarations in the application, such as:
28-
29-
`Imports Microsoft.SqlServer.Management.Smo`
30-
31-
`Imports Microsoft.SqlServer.Management.Common`
32-
33-
`Imports Microsoft.SqlServer.Management.Smo.Wmi`
34-
35-
## Stopping and Restarting the Microsoft SQL Server Service to the Instance of SQL Server in Visual Basic
36-
This code example shows how to stop and start services by using the SMO <xref:Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer> object. This provides an interface to the WMI Provider for Configuration Management.
37-
38-
```VBNET
39-
'Declare and create an instance of the ManagedComputer object that represents the WMI Provider services.
21+
The WMI provider is a published interface that is used by [!INCLUDE [msCoName](../../../includes/msconame-md.md)] Management Console (MMC) to manage the [!INCLUDE [ssNoVersion](../../../includes/ssnoversion-md.md)] services and network protocols. In SMO, the <xref:Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer> object represents the WMI provider.
22+
23+
The <xref:Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer> object operates independently of the connection established with the <xref:Microsoft.SqlServer.Management.Smo.Server> object to an instance of [!INCLUDE [ssNoVersion](../../../includes/ssnoversion-md.md)], and uses Windows credentials to connect to the WMI service.
24+
25+
## Examples
26+
27+
To use any code example that is provided, choose the programming environment, template, and language in which to create your application. For more information, see [How to Create a Visual C# SMO Project in Visual Studio .NET](../how-to-create-a-visual-csharp-smo-project-in-visual-studio-net.md).
28+
29+
For programs that use the [!INCLUDE [ssNoVersion](../../../includes/ssnoversion-md.md)] WMI provider, you must include the `Imports` statement to qualify the WMI namespace. Insert the statement after the other `Imports` statements, before any declarations in the application, such as:
30+
31+
`Imports Microsoft.SqlServer.Management.Smo`
32+
`Imports Microsoft.SqlServer.Management.Common`
33+
`Imports Microsoft.SqlServer.Management.Smo.Wmi`
34+
35+
36+
## Stop and restart the SQL Server service in Visual Basic
37+
38+
This code example shows how to stop and start services by using the SMO <xref:Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer> object. This provides an interface to the WMI provider for Configuration Management.
39+
40+
```vbnet
41+
'Declare and create an instance of the ManagedComputer object that represents the WMI provider services.
4042
Dim mc As ManagedComputer
4143
mc = New ManagedComputer()
42-
'Iterate through each service registered with the WMI Provider.
44+
'Iterate through each service registered with the WMI provider.
4345
Dim svc As Service
4446
For Each svc In mc.Services
4547
Console.WriteLine(svc.Name)
4648
Next
47-
'Reference the Microsoft SQL Server service.
49+
'Reference the SQL Server service.
4850
svc = mc.Services("MSSQLSERVER")
4951
'Stop the service if it is running and report on the status continuously until it has stopped.
5052
If svc.ServiceState = ServiceState.Running Then
@@ -68,141 +70,147 @@ Else
6870
Console.WriteLine("SQL Server service is not running.")
6971
End If
7072
```
71-
72-
## Enabling a Server Protocol using a URN String in Visual Basic
73-
The code example shows how to identify a server protocol using a URN object, and then enable the protocol.
74-
75-
```VBNET
76-
'This program must run with administrator privileges.
77-
'Declare the ManagedComputer WMI interface.
78-
Dim mc As New ManagedComputer()
79-
80-
'Create a URN object that represents the TCP server protocol.
81-
Dim u As New Urn("ManagedComputer[@Name='V-ROBMA3']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']")
82-
83-
'Declare the serverProtocol variable and return the ServerProtocol object.
84-
Dim sp As ServerProtocol
85-
sp = mc.GetSmoObject(u)
86-
87-
'Enable the protocol.
88-
sp.IsEnabled = True
89-
90-
'propagate back to the service
91-
sp.Alter()
92-
```
93-
94-
## Enabling a Server Protocol using a URN String in PowerShell
95-
The code example shows how to identify a server protocol using a URN object, and then enable the protocol.
96-
97-
```powershell
98-
#This example shows how to identify a server protocol using a URN object, and then enable the protocol
99-
#This program must run with administrator privileges.
100-
101-
#Load the assembly containing the classes used in this example
102-
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement")
103-
104-
#Get a managed computer instance
105-
$mc = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer
106-
107-
#Create a URN object that represents the TCP server protocol
108-
#Change 'MyPC' to the name of the your computer
109-
$urn = New-Object -TypeName Microsoft.SqlServer.Management.Sdk.Sfc.Urn -argumentlist "ManagedComputer[@Name='MyPC']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']"
110-
111-
#Get the protocol object
112-
$sp = $mc.GetSmoObject($urn)
113-
114-
#enable the protocol on the object
115-
$sp.IsEnabled = $true
116-
117-
#propagate back to actual service
118-
$sp.Alter()
119-
```
120-
121-
## Starting and stopping a service in Visual C#
122-
The code example shows how to stop and start an instance of SQL Server.
123-
124-
```csharp
125-
{
126-
//Declare and create an instance of the ManagedComputer
127-
//object that represents the WMI Provider services.
128-
ManagedComputer mc;
129-
mc = new ManagedComputer();
130-
//Iterate through each service registered with the WMI Provider.
131-
132-
foreach (Service svc in mc.Services)
133-
{
134-
Console.WriteLine(svc.Name);
135-
}
136-
//Reference the Microsoft SQL Server service.
137-
Service Mysvc = mc.Services["MSSQLSERVER"];
138-
//Stop the service if it is running and report on the status
139-
// continuously until it has stopped.
140-
if (Mysvc.ServiceState == ServiceState.Running) {
141-
Mysvc.Stop();
142-
Console.WriteLine(string.Format("{0} service state is {1}", Mysvc.Name, Mysvc.ServiceState));
143-
while (!(string.Format("{0}", Mysvc.ServiceState) == "Stopped")) {
144-
Console.WriteLine(string.Format("{0}", Mysvc.ServiceState));
145-
Mysvc.Refresh();
146-
}
147-
Console.WriteLine(string.Format("{0} service state is {1}", Mysvc.Name, Mysvc.ServiceState));
148-
//Start the service and report on the status continuously
149-
//until it has started.
150-
Mysvc.Start();
151-
while (!(string.Format("{0}", Mysvc.ServiceState) == "Running")) {
152-
Console.WriteLine(string.Format("{0}", Mysvc.ServiceState));
153-
Mysvc.Refresh();
154-
}
155-
Console.WriteLine(string.Format("{0} service state is {1}", Mysvc.Name, Mysvc.ServiceState));
156-
Console.ReadLine();
157-
}
158-
else {
159-
Console.WriteLine("SQL Server service is not running.");
160-
Console.ReadLine();
161-
}
162-
}
163-
```
164-
165-
## Starting and stopping a service in PowerShell
166-
The code example shows how to stop and start an instance of SQL Server.
167-
168-
```powershell
169-
#Load the assembly containing the objects used in this example
170-
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement")
171-
172-
#Get a managed computer instance
173-
$mc = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer
174-
175-
#List out all sql server instnces running on this mc
176-
foreach ($Item in $mc.Services){$Item.Name}
177-
178-
#Get the default sql server datbase engine service
179-
$svc = $mc.Services["MSSQLSERVER"]
180-
181-
# for stopping and starting services PowerShell must run as administrator
182-
183-
#Stop this service
184-
$svc.Stop()
185-
$svc.Refresh()
186-
while ($svc.ServiceState -ne "Stopped")
187-
{
188-
$svc.Refresh()
189-
$svc.ServiceState
190-
}
191-
"Service" + $svc.Name + " is now stopped"
192-
"Starting " + $svc.Name
193-
$svc.Start()
194-
$svc.Refresh()
195-
while ($svc.ServiceState -ne "Running")
196-
{
197-
$svc.Refresh()
198-
$svc.ServiceState
199-
}
200-
$svc.ServiceState
201-
"Service" + $svc.Name + "is now started"
202-
203-
```
204-
205-
## See Also
206-
[WMI Provider for Configuration Management Concepts](../../../relational-databases/wmi-provider-configuration/wmi-provider-for-configuration-management.md)
207-
208-
73+
74+
## Enable a server protocol using a URN string in Visual Basic
75+
76+
The code example shows how to identify a server protocol using a URN object, and then enables the protocol.
77+
78+
```vbnet
79+
'This program must run with administrator privileges.
80+
'Declare the ManagedComputer WMI interface.
81+
Dim mc As New ManagedComputer()
82+
83+
'Create a URN object that represents the TCP server protocol.
84+
Dim u As New Urn("ManagedComputer[@Name='V-ROBMA3']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']")
85+
86+
'Declare the serverProtocol variable and return the ServerProtocol object.
87+
Dim sp As ServerProtocol
88+
sp = mc.GetSmoObject(u)
89+
90+
'Enable the protocol.
91+
sp.IsEnabled = True
92+
93+
'propagate back to the service
94+
sp.Alter()
95+
```
96+
97+
## Enable a server protocol using a URN string in PowerShell
98+
99+
The code example shows how to identify a server protocol using a URN object, and then enables the protocol.
100+
101+
```powershell
102+
#This example shows how to identify a server protocol using a URN object, and then enable the protocol
103+
#This program must run with administrator privileges.
104+
105+
#Load the assembly containing the classes used in this example
106+
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement")
107+
108+
#Get a managed computer instance
109+
$mc = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer
110+
111+
#Create a URN object that represents the TCP server protocol
112+
#Change 'MyPC' to the name of the your computer
113+
$urn = New-Object -TypeName Microsoft.SqlServer.Management.Sdk.Sfc.Urn -argumentlist "ManagedComputer[@Name='MyPC']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']"
114+
115+
#Get the protocol object
116+
$sp = $mc.GetSmoObject($urn)
117+
118+
#enable the protocol on the object
119+
$sp.IsEnabled = $true
120+
121+
#propagate back to actual service
122+
$sp.Alter()
123+
```
124+
125+
## Start and stop a service in C#
126+
127+
The code example shows how to stop and start an instance of [!INCLUDE [ssnoversion-md](../../../includes/ssnoversion-md.md)].
128+
129+
```csharp
130+
//Declare and create an instance of the ManagedComputer
131+
//object that represents the WMI provider services.
132+
ManagedComputer mc;
133+
mc = new ManagedComputer();
134+
135+
//Iterate through each service registered with the WMI provider.
136+
foreach (Service svc in mc.Services)
137+
{
138+
Console.WriteLine(svc.Name);
139+
}
140+
141+
//Reference the SQL Server service.
142+
Service mySvc = mc.Services["MSSQLSERVER"];
143+
144+
//Stop the service if it is running and report on the status
145+
// continuously until it has stopped.
146+
if (mySvc.ServiceState == ServiceState.Running)
147+
{
148+
mySvc.Stop();
149+
Console.WriteLine(string.Format("{0} service state is {1}", mySvc.Name, mySvc.ServiceState));
150+
while (!(string.Format("{0}", mySvc.ServiceState) == "Stopped"))
151+
{
152+
Console.WriteLine(string.Format("{0}", mySvc.ServiceState));
153+
mySvc.Refresh();
154+
}
155+
156+
Console.WriteLine(string.Format("{0} service state is {1}", mySvc.Name, mySvc.ServiceState));
157+
//Start the service and report on the status continuously
158+
//until it has started.
159+
mySvc.Start();
160+
while (!(string.Format("{0}", mySvc.ServiceState) == "Running"))
161+
{
162+
Console.WriteLine(string.Format("{0}", mySvc.ServiceState));
163+
mySvc.Refresh();
164+
}
165+
166+
Console.WriteLine(string.Format("{0} service state is {1}", mySvc.Name, mySvc.ServiceState));
167+
Console.ReadLine();
168+
}
169+
else
170+
{
171+
Console.WriteLine("SQL Server service is not running.");
172+
Console.ReadLine();
173+
}
174+
```
175+
176+
## Start and stop a service in PowerShell
177+
178+
The code example shows how to stop and start an instance of [!INCLUDE [ssnoversion-md](../../../includes/ssnoversion-md.md)].
179+
180+
```powershell
181+
#Load the assembly containing the objects used in this example
182+
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement")
183+
184+
#Get a managed computer instance
185+
$mc = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer
186+
187+
#List out all SQL Server instances running on this mc
188+
foreach ($Item in $mc.Services) { $Item.Name }
189+
190+
#Get the default SQL Server database engine service
191+
$svc = $mc.Services["MSSQLSERVER"]
192+
193+
# for stopping and starting services PowerShell must run as administrator
194+
195+
#Stop this service
196+
$svc.Stop()
197+
$svc.Refresh()
198+
while ($svc.ServiceState -ne "Stopped") {
199+
$svc.Refresh()
200+
$svc.ServiceState
201+
}
202+
"Service" + $svc.Name + " is now stopped"
203+
"Starting " + $svc.Name
204+
$svc.Start()
205+
$svc.Refresh()
206+
while ($svc.ServiceState -ne "Running") {
207+
$svc.Refresh()
208+
$svc.ServiceState
209+
}
210+
$svc.ServiceState
211+
"Service" + $svc.Name + "is now started"
212+
```
213+
214+
## Related content
215+
216+
- [WMI Provider for Configuration Management](../../wmi-provider-configuration/wmi-provider-for-configuration-management.md)

0 commit comments

Comments
 (0)