• [转]通过PowerShell工具跨多台服务器执行SQL脚本


    转至:http://www.cnblogs.com/SameZhao/p/4743692.html

    有时候,当我们并没有合适的第三方工具(大部分需要付费)去管理多台数据库服务器,那么如何做最省力、省心呢?!Powershell一个强大的工具,可以很方便帮到我们处理日常的数据库维护工作 。简单的几步搞定,下面介绍一个简单例子便于大家理解:

    1,创建一个文件夹,如 D:ExecScriptOnMultiServer

    2,创建一个文本文件ServerList.txt,将所有需要管理的数据库实例名写到里面,格式如下:

        SQLInstanceName1
        SQLInstanceName2
        SQLInstanceName3
      ...
    然后保存到可以访问到的目录,如D:ExecScriptOnMultiServerServerList.txt
     
    3,将下面的Powershell脚本保存到目录 D:ExecScriptOnMultiServerExecuteQueryOnMultiServers.ps1
    复制代码
    ##Save the below powershell script in folder "D:ExecScriptOnMultiServerExecuteQueryOnMultiServers.ps1"
    
    $QueryPath= "D:ExecScriptOnMultiServerSQLQuery.sql"
    $OutputFile = "D:ExecScriptOnMultiServerQueryOutput.txt"
    
    $ExecuteQuery= Get-Content -path $QueryPath | out-string
    
    "Results -- > `r`n`r`n" > $OutputFile
    
    FOREACH($server in GC "D:ExecScriptOnMultiServerServerList.txt")
     {
        $server 
    
        "---------------------------------------------------------------------------------------------------------" >> $OutputFile
        $server >> $OutputFile
        "---------------------------------------------------------------------------------------------------------" >> $OutputFile
        invoke-sqlcmd -ServerInstance $server -query $ExecuteQuery -querytimeout 65534 | ft -autosize | out-string -width 4096 >> $OutputFile
     }
    复制代码

    4,将你要在上述ServerList文件中的服务器实例执行的SQL语句,可先写到目录文件D:ExecScriptOnMultiServerSQLQuery.sql

    例如:我要对所有实例下所有数据的的版本、SP和群集节点等信息收集,如下代码:

    复制代码
    declare @Nodes Varchar(100)
    set @Nodes=''
     if (SERVERPROPERTY('IsClustered') = 1)
    begin
    select @Nodes=@Nodes+ NodeName  
    +',' from sys.dm_os_cluster_nodes order by NodeName
    set @Nodes=substring(@Nodes,0,LEN(@Nodes))
    select 
    @Nodes as HostName, 
    SQLInstanceName = @@SERVERNAME,
    'Yes' as IsClustered,
    CAST(SERVERPROPERTY('ComputerNamePhysicalNetBIOS') AS VARCHAR(50)) As ActiveNode,
    SERVERPROPERTY('edition') As SQLEdition,
    Case 
      when cast(serverproperty('productversion') as varchar) like '8.%' then 'SQL2000'
           when cast(serverproperty('productversion') as varchar) like '9.%' then 'SQL2005'
           when cast(serverproperty('productversion') as varchar)  like '10.0%' then 'SQL2008'
           when cast(serverproperty('productversion') as varchar)  like '10.50.%' then 'SQL2008R2'
           when cast(serverproperty('productversion') as varchar)  like '11.%' then 'SQL2012'
      when cast(serverproperty('productversion') as varchar)  like '12.%' then 'SQL2014'
           ELSE 'SQL7.0' END +' '+
      cast(SERVERPROPERTY('productlevel') as varchar(50))+' ('+ cast(SERVERPROPERTY('productversion') as varchar(50)) + ')' as SQLVersion
    end
     else 
    begin
    select @Nodes=CAST(SERVERPROPERTY('ComputerNamePhysicalNetBIOS') AS VARCHAR(50)) 
    select 
    @Nodes as HostName, 
    SQLInstanceName = @@SERVERNAME,
    'No' as IsClustered,
    SERVERPROPERTY('edition') As SQLEdition,
    Case 
      when cast(serverproperty('productversion') as varchar) like '8.%' then 'SQL2000'
           when cast(serverproperty('productversion') as varchar) like '9.%' then 'SQL2005'
           when cast(serverproperty('productversion') as varchar)  like '10.0%' then 'SQL2008'
           when cast(serverproperty('productversion') as varchar)  like '10.50.%' then 'SQL2008R2'
           when cast(serverproperty('productversion') as varchar)  like '11.%' then 'SQL2012'
      when cast(serverproperty('productversion') as varchar)  like '12.%' then 'SQL2014'
           ELSE 'SQL7.0' END +' '+
      cast(SERVERPROPERTY('productlevel') as varchar(50))+' ('+ cast(SERVERPROPERTY('productversion') as varchar(50)) + ')' as SQLVersion
    end
    复制代码

    5,开启Window PowerShellISE程序,注意要用管理员身份启动,打开Powershell脚本ExecuteQueryOnMultiServers.ps1,点击执行。

    执行后结果保存如代码中标注,位置在D:ExecScriptOnMultiServerQueryOutput.txt 

    6,或者直接在PowerShell命令行执行,

    PS C:>D:ExecScriptOnMultiServerExecuteQueryOnMultiServers.ps1
  • 相关阅读:
    xcode中的udp接收数据
    FMX.MEDIA中的录音功能实现
    DELPHI XE功能
    xcode 中运用lame进行caf文件到mp3文件的转换
    xcode中用AVAudioRecorder录音到指定的caf文件
    enum 在c中的使用(枚举类型)
    c语言中typedef的几种用法
    pta 两个有序链表序列的交集
    Level-order Traversal(c语言函数指针样例)
    求二叉树高度
  • 原文地址:https://www.cnblogs.com/keepSmile/p/5800341.html
Copyright © 2020-2023  润新知