JavaScript Editor Ajax toolkit     Ajax tutorials 



Main Page

Previous Page
Next Page

13.1. Programming SMO WMI Classes

Figure 13-1 shows the relationship between SMO classes used with the WMI Provider for Configuration Management.

This section shows how to programmatically use SMO WMI classes. The examples in this section are all built using Visual Studio 2005. You need a reference to the following assemblies to compile and run the examples:

Figure 13-1. SMO classes used with the WMI Provider for Configuration Management


  • Microsoft.SqlServer.ConnectionInfo

  • Microsoft.SqlServer.Smo

  • Microsoft.SqlServer.WmiEnum

Additional assembly references will be indicated for examples in which they are required.

13.1.1. Enumerating the WMI Installation

This example demonstrates how to instantiate a ManagedComputer object and iterate through its hierarchy of collections to enumerate information about the WMI installation. The example lists client protocols, connection settings, server aliases, service instances, and services on the local machine.

    using System;
    using System.Data;

    using Microsoft.SqlServer.Management.Common;
    using Microsoft.SqlServer.Management.Smo;
    using Microsoft.SqlServer.Management.Smo.Wmi;

    class Program
    {
        static void Main(string[] args)
        {
            ManagedComputer mc = new ManagedComputer(  );

            Console.WriteLine("-Client Protocols-");
            foreach (ClientProtocol cp in mc.ClientProtocols)
                Console.WriteLine(cp.DisplayName + ": " +
                    (cp.IsEnabled ? "Enabled" : "Disabled"));

            Console.WriteLine(Environment.NewLine + "-Connection Settings-");
            WmiConnectionInfo wci = mc.ConnectionSettings;
            Console.WriteLine("MachineName = " + wci.MachineName);
            Console.WriteLine("Timeout = " + wci.Timeout);
            Console.WriteLine("Username = " + wci.Username);

            Console.WriteLine(Environment.NewLine + "-Server Aliases-");
            foreach (ServerAlias sa in mc.ServerAliases)
                Console.WriteLine(sa.Name + ": " + sa.State);

            Console.WriteLine(Environment.NewLine + "-Server Instances-");
            foreach (ServerInstance si in mc.ServerInstances)
                Console.WriteLine(si.Name + ": " + si.State);

            Console.WriteLine(Environment.NewLine + "-Services-");
            foreach (Service s in mc.Services)
                Console.WriteLine(s.Name + ": " + s.ServiceState);

            Console.WriteLine(Environment.NewLine + "Press any key to continue.");
            Console.ReadKey(  );
        }
    }

Results are shown in Figure 13-2.

Figure 13-2. Results from enumerating WMI installation


The ManagedComputer class, which represents a WMI installation on a SQL Server instance, has an overloaded constructor. The overload used in the preceding example takes no arguments and initializes a new instance for the default SQL Server instance. The other two overloads let you specify the name of the computer to connect to, and optionally a login name and password. The ManagedComputer class exposes WMI functionality through the set of properties described in Table 13-1.

Table 13-1. ManagedComputer class public properties

Property

Return value

Description

ClientProtocols

ClientProtocolCollection

Gets a collection of ClientProtocol objects, each representing a client protocol defined on the WMI installation

ConnectionSettings

WmiConnectionInfo

Gets the connection settings for the WMI installation

Name

string

Gets the name of the WMI object

Properties

PropertyCollection

Gets a collection of Property objects, each representing a property defined on the WMI object

ServerAliases

ServerAliasCollection

Gets a collection of ServerAlias objects, each representing a server alias on the WMI installation

ServerInstances

ServerInstanceCollection

Gets a collection of ServerInstance objects, each representing an instance of SQL Server on the WMI installation

Services

ServiceCollection

Gets a collection of Service objects, each representing a SQL Server service on the WMI installation

State

SqlSmoState

Gets the state of the WMI object

Urn

Urn

Gets the URN address for the WMI object

UserData

object

Gets or sets user-defined data associated with the WMI object


Four collections accessed through properties of the ManagedComputer class allow access to most WMI functionality. These collections expose the objects described further in Table 13-2.

Table 13-2. ManagedComputer class collections

Class

Function

ClientProtocol

The ClientProtocol class represents a network protocol installed on a client that allows communication with a SQL Server instance over a network. The ClientProtocol class lets you:

  • Get or set the display name of the protocol

  • Enable or disable the protocol

  • Get the network library information file for the protocol as a NetLibInfo object

  • Get the network library filename for the protocol

  • Get or set the order in which the protocol is listed and applied relative to the supported protocols in the ClientProtocolCollection object

  • Access and modify the collection of attributes (as ProtocolProperty objects) defined on the protocol

The collection of ClientProtocol objects is accessed through the ClientProtocols property.

ServerAlias

A server alias is an alternate name that you can use to connect to an instance of SQL Server. The ServiceAlias class lets you:

  • Get or set the connection string used by the alias to connect to the SQL Server instance

  • Get or set the name of the alias

  • Get or set the parent (as a ManagedComputer object) of the alias

  • Get or set the name of the protocol used by the alias

  • Get or set the SQL Server instance that the alias connects to

  • Create, refresh, and drop server aliases

The collection of ServerAlias objects is accessed though the ServerAliases property.

ServerInstance

The ServerInstance class represents an instance of SQL Server and lets you:

  • Get or set the name of the server instance

  • Get or set the parent (as a ManagedComputer object) of the server instance

  • Access and modify the collection of protocols (as a collection of ServerProtocol objects) defined on the server in the WMI installation

The collection of ServerInstance objects is accessed though the ServerInstances property.

Service

The Service class represents an instance of a SQL Server service and lets you:

  • Find out whether the service can be paused or stopped

  • Get a list of services that are dependent on the service

  • Get the name and display name of the service

  • Get the parent of the service (as a ManagedComputer object)

  • Get the path of the binary file that implements the service

  • Get the process ID that uniquely identifies the service

  • Get the account under which the service is running

  • Get the state of the service (e.g., stopped, paused, running)

  • Get the method by which the service is started (as a ServiceStartMode enumeration value)

  • Get startup parameters for the service

  • Change the password for the account under which the service is running

  • Stop, pause, start, and resume the service

  • Refresh the service

  • Set the account under which the service runs

The collection of Service objects is accessed though the Services property.


The SMO classes for WMI are described in Table 13-3.

Table 13-3. SMO classes used with the WMI Provider for Configuration Management

Class

Description

ClientProtocol

Represents a client network protocol that allows the client to communicate with a SQL Server instance over a network.

ClientProtocolCollection

Represents a collection of client protocols as ClientProtocol objects. The ClientProtocols property of the ManagedComputer class returns the client protocols defined on the WMI installation.

ClientProtocolProperty

Represents an attribute of a WMI client network protocol.

ClientProtocolPropertyCollection

Represents a collection of client protocol properties as ClientProtocolProperty objects. The ProtocolProperties property of the ClientProtocol class returns the attributes of the client protocol.

IPAddressProperty

Represents an attribute of an IP address.

IPAddressPropertyCollection

Represents a collection of IP address properties as IPAddressProperty objects. The IPAddressProperties property of the ServerIPAddress class returns the attributes of the IP address.

ManagedComputer

Represents a WMI installation on a SQL Server instance.

NetLibInfo

Represents information about a network library file. The NetLibInfo property of the ClientProtocol class returns the NetLibInfo object for the protocol.

ServerAlias

Represents an alias for a server connection.

ServerAliasCollection

Represents a collection of server aliases as ServerAlias objects. The ServerAliases property of the ManagedComputer class returns the server aliases defined on the WMI installation.

ServerInstance

Represents a SQL Server instance.

ServerInstanceCollection

Represents a collection of SQL Server instances as ServerInstance objects. The ServerInstances property of the ManagedComputer class returns the server instances defined on the WMI installation.

ServerIPAddress

Represents an IP address of a server protocol defined on the WMI installation.

ServerIPAddressCollection

Represents a collection of IP addresses as ServerIPAddress objects. The IPAddresses property of the ServerProtocol class returns the IP addresses defined on the WMI installation.

ServerProtocol

Represents a server network protocol that allows the server to communicate with SQL Server clients over a network.

ServerProtocolCollection

Represents a collection of server protocols as ServerProtocol objects. The ServerProtocols property of the ManagedComputer class returns the server protocols defined on the WMI installation.

ServerProtocolProperty

Represents an attribute of a WMI server network protocol.

ServerProtocolPropertyCollection

Represents a collection of server protocol properties as ServerProtocolProperty objects. The ProtocolProperties property of the ServerProtocol class returns the attributes of the server protocol.

Service

Represents an instance of a SQL Server service.

ServiceCollection

Represents a collection of SQL Server services as Service objects. The Services property of the ManagedComputer class returns the services defined on the WMI installation.

WmiConnectionInfo

Represents connection information used by a WMI installation. The ConnectionSettings property of the ManagedComputer class returns the WmiConnectionInfo object for the WMI installation.


13.1.2. Creating a Server Alias

This example demonstrates how to create new SMO WMI objects. The example creates a new server alias on the local SQL Server instance. It does so by instantiating a new ServerAlias object, associating it with the ManagedComputer object that represents WMI, and then invoking the Create( ) method of ServerAlias.

    using System;
    using System.Data;

    using Microsoft.SqlServer.Management.Common;
    using Microsoft.SqlServer.Management.Smo;
    using Microsoft.SqlServer.Management.Smo.Wmi;

    class Program
    {
        static void Main(string[] args)
        {
            ManagedComputer mc = new ManagedComputer(  );

            ServerAlias sa = new ServerAlias(  );
            sa.ConnectionString = "1433";
            sa.Name = "PSS2005 Alias";
            sa.Parent = mc;
            sa.ProtocolName = "tcp";
            sa.ServerName = "locahost";
            sa.Create(  );

            Console.WriteLine(Environment.NewLine + "Press any key to continue.");
            Console.ReadKey(  );
        }
    }

Figure 13-3 shows the new alias using the SQL Server Configuration Manager tool (Start Microsoft SQL Server 2005 Configuration Tools SQL Server Configuration Manager).

Figure 13-3. Creating a server alias results


13.1.3. Starting and Stopping a Service

This example shows how to stop and start a service by stopping and restarting SQL Server Reporting Services:

    using System;
    using System.Data;

    using Microsoft.SqlServer.Management.Common;
    using Microsoft.SqlServer.Management.Smo;
    using Microsoft.SqlServer.Management.Smo.Wmi;
    using System.Threading;

    class Program
    {
        static void Main(string[] args)
        {
            ManagedComputer mc = new ManagedComputer(  );

            Service 
 s = mc.Services["ReportServer"];
            Console.WriteLine("ReportServer status: " + s.ServiceState);

            s.Stop(  );
            Thread.Sleep(10000);
            s.Refresh(  );
            Console.WriteLine("ReportServer status: " + s.ServiceState);
            s.Start(  );
            Thread.Sleep(10000);
            s.Refresh(  );
            Console.WriteLine("ReportServer status: " + s.ServiceState);

            Console.WriteLine(Environment.NewLine + "Press any key to continue.");
            Console.ReadKey(  );
        }
    }

Results are shown in Figure 13-4.

Figure 13-4. Results from stopping and starting service example


The Service class has methods to control the state of a serviceStart( ), Pause( ), Resume( ), and Stop( ). The AcceptsPause and AcceptsStop properties should be checked before calling Pause( ) or Stop( ) to ensure that the service can be paused or stopped.

The example uses the static Sleep( ) method of the THRead class to pause for 10 seconds to allow the service state change operations to complete. The Refresh( ) method of the Service class refreshes the SQL Server service to ensure the status is current.


Previous Page
Next Page


JavaScript Editor Ajax toolkit     Ajax tutorials 
Photographe mariage Laval