SQLServer achieve multiple servers using powershell batch execute SQL scripts

  In the operation and maintenance work, you will encounter a lot of repetitive operations. For a single server repetitive work can be achieved on a regular basis with the job of processing; whereas for the same server needs more than one, we can use powershell to perform batch operations on multiple servers the same batch operation. This paper focuses on the implementation of this program, and not entangled in the SQL script for each server specific implementation, so in the case of a simple SQL script as an example: execute a powershell script on a central server, that server can collect all Information. A similar report regularly to the task, you can also set the powershell script scheduled tasks.

  First, the server needs to perform batch operations on text files Serverlist.txt in:

  Central server: 10.120.100.101

  10.120.100.1

  10.120.100.2

  10.120.100.3

  All of the following files created and Serverlist.txt should be placed in a folder!

 

  A write SQL scripts implementation requirements, and save the file .SQL:

  1- script (script file name 1.Create_DB & Table.sql): create databases and tables to collect data on each server

  - The default setting to create a dedicated database for collecting information

  CREATE DATABASE [Info_stats]
   CONTAINMENT = NONE
   ON  PRIMARY
  ( NAME = N'Info_stats', FILENAME = N'D:\SQLServer\Data\Info_stats.mdf' , SIZE = 4096KB , FILEGROWTH = 1024KB )
   LOG ON
  ( NAME = N'Info_stats_log', FILENAME = N'D:\SQLServer\Data\Info_stats_log.ldf' , SIZE = 1024KB , FILEGROWTH = 10%)
  GO

  - Create a table need to use this example in the database

  Use [Info_stats];

  Create table DBName
  (Servername nvarchar(100),
  DBName nvarchar(100))

  2- script (script file name 2.Select_DBName.sql): Name of the database query for all users and stored on the server [Info_stats] -DBName table

  select name from sysdatabases where name not in(N'master', N'model', N'msdb', N'tempdb', N'distribution', N'DWDiagnostics', N'DWConfiguration',N'DWQueue',   N'resource',N'ReportServer',N'ReportServerTempDB')

  3- script (script file name 3.Copy Info to Centre.sql): check out the information of the synchronization server to a central server:

  Set nocount on;
  use [Info_stats];
  truncate table [Info_stats].[dbo].[DBName];
  insert into [Info_stats].[dbo].[DBName]
  select * from  
  (
  select top 1* from
  OPENDATASOURCE(
           'SQLOLEDB',
           'Data Source=10.120.100.1;Integrated Security=SSPI'
           ).[Info_stats].[dbo].[DBName] order by Check_Date desc) c
  union
  select * from  
  (
  select top 1* from
  OPENDATASOURCE(
           'SQLOLEDB',
           'Data Source=10.120.100.2;Integrated Security=SSPI'
           ).[Info_stats].[dbo].[DBName] order by Check_Date desc) c
  union
  (
  select * from (
  select top 1* from
  OPENDATASOURCE(
           'SQLOLEDB',
           'Data Source=10.120.100.3;Integrated Security=SSPI'
           ).[Info_stats].[dbo].[DBName]  order by Check_Date desc)z)


  Set nocount off;
  

  Note: You need to open the 'Ad Hoc Distributed Queries' option on a central server, to synchronize before you can use OPENDATASOURCE query results to a central server

  

  Second start powershell script ready:

  1. Create a powershell script call (1.Create_DB & Table.sql) for use when the first performance:

  Scripting name: Create_DB & Table.ps1

  $PSScriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Definition
  $Serverlist="$PSScriptRoot\Serverlist.txt"
  $Create_table="$PSScriptRoot\1.Create_DB&Table.sql"
  Foreach ($server in GC $Serverlist) {
  Invoke-Sqlcmd -ServerInstance $server -InputFile $Create_table
  }
  2.创建调用(2.Select_DBName.sql)的powershell脚本,用于收集数据:

  Scripting name: QueryData.ps1

  Split-PSScriptRoot the Path = $ $ -Parent MyInvocation.MyCommand.Definition
  $ Serverlist = "$ PSScriptRoot \ serverlist.txt"
  $ ITSPCheck = "$ PSScriptRoot \ 2.Select_DBName.sql"
  the Foreach ($ $ Serverlist Server in the GC) {
  Invoke- server -inputfile $ $ -ServerInstance the Sqlcmd ITSPCheck
  }
  3. create a powershell script call (3.Copy Info to Centre.sql) for integrating data to a central server:

  Scripting name: IntegrateResult.ps1

  $PSScriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Definition
  $IntegrateResult="$PSScriptRoot\3.Copy Info to Centre.sql"
  $Selfcheck="$PSScriptRoot\2.Select_DBName.sql"
  Invoke-Sqlcmd -ServerInstance $env:COMPUTERNAME -InputFile $IntegrateResult
  Invoke-Sqlcmd -ServerInstance $env:COMPUTERNAME -InputFile $Selfcheck
  4.创建统筹powershell脚本,来按步骤调用以上powershell脚本:

  Scripting name: ExecPs.ps1

  $PSScriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Definition
  powershell.exe "$PSScriptRoot\QueryData.ps1"
  powershell.exe "$PSScriptRoot\IntegrateResult.ps1"

  The first execution, will step databases and tables to create a multi-:

  Scripting name: ExecPs_FirstTime.ps1

  $PSScriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Definition
  powershell.exe "$PSScriptRoot\Create_DB&Table.ps1"
  powershell.exe "$PSScriptRoot\QueryData.ps1"
  powershell.exe "$PSScriptRoot\IntegrateResult.ps1"

  

  The first execution ExecPs_FirstTime.ps1, after performing ExecPs.ps1 can gather information to a central server, and the old data will be retained on each server node, the center server to the latest data collected by the judgment:

  

  

  For similar requirements, it can also be extended according to the present embodiment. Right on the central server running ExecPs.ps1 can get information about all server nodes, very convenient!

 

Guess you like

Origin www.cnblogs.com/LC0507/p/11209327.html