Pro SQL Server 2008 Policy-Based Management- P3

50 360 0
Pro SQL Server 2008 Policy-Based Management- P3

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

CHAPTER „ EVALUATING POLICIES Figure 3-36 Export Registered Servers dialog box Right-click the Central Management Server folder and select Tasks o Import In the Import Registered Servers dialog box, browse to the file you saved, as shown in Figure 3-37, and then click OK Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 83 CHAPTER „ EVALUATING POLICIES Figure 3-37 Import Registered Servers dialog box The Central Management Server now has the same folder structure and SQL Server instances registered as defined in the Local Server Groups registered server list Evaluating Policies against a Central Management Server Group Right-clicking a server group provides you with a few options that you can execute against all the servers in the group, as shown in Figure 3-38 In this section, we’ll look at the Evaluate Policies option for a Central Management Server group 84 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark CHAPTER „ EVALUATING POLICIES Figure 3-38 Central Management Server group options Now that you have servers in you Central Management Server groups, you can see the real power of using Policy-Based Management with Central Management Servers Execute one or more policies against all of the servers in a server group by following these steps: Right-click a Central Management Server group and select Evaluate Policies (see Figure 3-38), In the Evaluate Policies dialog box, you’ll need to select the source for your policy store Click the ellipsis button next to the Source field (see Figure 312) In the Select Source dialog box, select whether your policies are on the file system or stored on a SQL Server instance For this walk-through, we’re going to use the ones stored on our SQL Server 2008 instance (see Figure 39) The Evaluate Policies dialog box will now be populated with the policies that are stored on the server (see Figure 3-13) Select a single or multiple policies to evaluate, as shown in Figure 3-39, and then click the Evaluate button Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 85 CHAPTER „ EVALUATING POLICIES Figure 3-39 Selecting policies to evaluate against a Central Management Server group You’ve now evaluated the selected policies against all the servers in the group at the same time The Evaluate Policies dialog box will show the evaluation status of each object (in this example, databases) in the Targets Details section, as shown in Figure 3-40 86 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark CHAPTER „ EVALUATING POLICIES Figure 3-40 Evaluate Policies dialog box showing results of evaluating against a Central Server Management group Summary In this chapter, we introduced you to the different execution modes you can use to evaluate policies We have shown the out-of-the-box ways to evaluate a single policy, as well as to evaluate multiple policies at once We also demonstrated how you can configure a Central Management Server that will allow you to evaluate your policies against multiple SQL Server instances Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 87 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark CHAPTER „„„ Policy-Based Management Using PowerShell PowerShell is a very useful scripting tool that enables system administrators to automate server administration, gather information, and deploy applications Microsoft introduced support for Windows PowerShell version with SQL Server 2008, and support for Windows PowerShell version will be released with SQL Server 2008 Release In this chapter, we will create some basic PowerShell scripts to demonstrate how to use this tool with Policy-Based Management We will use PowerShell to run a single policy against a single instance of SQL Server, and then to run a group of policies against a single instance of SQL Server We will then store and query the policy results Finally, we will tie in the use of a Central Management Server to execute and store the results of a category of policies against all the instances in a server group Creating a Basic PowerShell Script When you create a PowerShell script, there are two ways of connecting to and querying a SQL Server instance: using T-SQL and using SQL Server Management Objects (SMO) We’ll look at both the T-SQL and SMO methods, and then demonstrate how to interrogate the class to find out the properties and methods available to you Using T-SQL You can connect to a database from PowerShell, run a T-SQL query against the database, and return a result set back to PowerShell The procedure is as follows: Create a connection to SQL Server using the NET data provider Build your connection string Create a T-SQL command to run against a SQL Server database Populate a container (DataSet) with the results of the T-SQL query Return the results stored in the container to the shell window Close the connection to SQL Server Listing 4-1 shows an example of connecting to and querying a database using T-SQL in PowerShell Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 89 CHAPTER „ POLICY-BASED MANAGEMENT USING POWERSHELL Listing 4-1 Connecting to and querying a SQL Server instance using T-SQL in PowerShell $SQLCon = New-Object System.Data.SqlClient.SqlConnection $SQLCon.ConnectionString = "Server = TESTLAB01\BENCHDBS04TESTLAB01\BENCHDEV04; Database = msdb; Integrated Security = True" $SQLCmd = New-Object System.Data.SqlClient.SqlCommand $SQLCmd.CommandText = "SELECT [name] FROM dbo.syspolicy_policies" $SQLCmd.Connection = $SQLCon $SQLDataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $SQLDataAdapter.SelectCommand = $SQLCmd $DataSet = New-Object System.Data.DataSet $SQLDataAdapter.Fill($DataSet) $DataSet.Tables[0] $SQLCon.Close() Edit the code in Listing 4-1 so that the server is your SQL Server 2008 Policy-Based Management instance Then save it as Example4-1.ps1 Execute Example4-1.ps1 by opening a command prompt and running the sqlps utility, as shown in Figure 4-1 The results should list the policies that you currently have stored, as shown in the example in Figure 4-2 Figure 4-1 Running Example4-1.ps1 90 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark CHAPTER „ POLICY-BASED MANAGEMENT USING POWERSHELL Figure 4-2 Results from Example4-1.ps1 Using SQL Server Management Objects Another way to connect to and query an instance is to use SMO classes In this section we’re going to look at the following namespaces and objects: Microsoft.SQLServer.Management.sdk.sfc: A namespace that contains a set of classes, interfaces, structures, delegates, and enumerations that support SQL SMO Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 91 CHAPTER „ POLICY-BASED MANAGEMENT USING POWERSHELL Microsoft.SQLServer.Management.DFM: A namespace that contains classes that represent the SQL Server Policy-Based Management objects SQLStoreConnection: A class in the Microsoft.SQLServer.Management.sdk.sfc namespace that represents a connection to a SQL Server instance PolicyStore: An object in the Microsoft.SQLServer.Management.DFM namespace that represents a Policy-Based Management policy store that holds information about policies, conditions, object sets, and subscriptions Furthermore, our SMO example makes use of an important SMO class known as $Policy.Name It’s a useful class that implements many properties and methods to help you work with Policy-Based Management from PowerShell Listing 4-2 shows how to connect and query for the same information as in Listing 4-1 The difference is that this time we are using SMO rather than T-SQL Listing 4-2 Connecting to and querying a SQL Server instance using SMO in PowerShell $SQLPBMConnection = new-object Microsoft.SQLServer.Management.Sdk.Sfc.SqlStoreConnection("server= TESTLAB01\BENCHDBS04TESTLAB01\BENCHDEV04; Trusted_Connection=true"); $SQLPolicyStore = new-object Microsoft.SqlServer.Management.DMF.PolicyStore($SQLPBMConnection); foreach ($Policy in $SQLPolicyStore.Policies) { $Policy.Name } Edit the code in Listing 4-2 so that the server is your SQL Server 2008 Policy-Based Management instance Then save it as Example4-2.ps1 Execute Example4-2.ps1 The results should look similar to Figure 4-3 and identical to the results from Listing 4-1 92 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark ... „ POLICY-BASED MANAGEMENT USING POWERSHELL Listing 4-1 Connecting to and querying a SQL Server instance using T -SQL in PowerShell $SQLCon = New-Object System.Data.SqlClient.SqlConnection $SQLCon.ConnectionString... dbo.syspolicy_policies" $SQLCmd.Connection = $SQLCon $SQLDataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $SQLDataAdapter.SelectCommand = $SQLCmd $DataSet = New-Object System.Data.DataSet $SQLDataAdapter.Fill($DataSet)... than T -SQL Listing 4-2 Connecting to and querying a SQL Server instance using SMO in PowerShell $SQLPBMConnection = new-object Microsoft.SQLServer.Management.Sdk.Sfc.SqlStoreConnection( "server=

Ngày đăng: 20/10/2013, 11:15

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan