分享一个批量导出当前实例下的所有linkedserver脚本

原文:分享一个批量导出当前实例下的所有linkedserver脚本

分享一个批量导出当前实例下的所有linkedserver脚本

很多时候,我们都需要导出实例下面的登录用户,job,linkedserver等等

导出job比较复杂,下午写了一个脚本把所有的linkedserver导出来,但是密码不会显示出来

下面脚本在SQL2008 R2下面测试通过

-- =============================================
-- Author:      <桦仔>
-- Blog:        <http://www.cnblogs.com/lyhabc/>
-- Create date: <2014/11/3>
-- Description: <批量导出实例下所有linkedserver>
-- LINK: http://msdn.microsoft.com/zh-cn/library/ms189811.aspx
-- =============================================
SET NOCOUNT ON
USE [master]
GO
 DECLARE @servername NVARCHAR(2000)
 DECLARE @id INT
 DECLARE @scriptdate NVARCHAR(200)
 DECLARE @productName NVARCHAR(2000)
 DECLARE @datasource NVARCHAR(4000)
 DECLARE @useself BIT
 DECLARE @dist BIT
 DECLARE @remoteuser NVARCHAR(2000)
 DECLARE @collationcompatible BIT
 DECLARE @dataaccess BIT
 DECLARE @sub BIT
 DECLARE @pub BIT
 DECLARE @rpc BIT
 DECLARE @rpcout BIT
 DECLARE @connecttimeout BIGINT
 DECLARE @lazyschemavalidation BIT
 DECLARE @querytimeout BIGINT
 DECLARE @useremotecollation BIT
 DECLARE @remoteproctransactionpromotion BIT

DECLARE LinkserverNameCur CURSOR
FOR
    SELECT  srv.name AS [Name] ,
            CAST(srv.server_id AS INT) AS [ID]
    FROM    sys.servers AS srv
    WHERE   ( srv.server_id != 0 )

OPEN LinkserverNameCur
FETCH NEXT FROM LinkserverNameCur INTO @servername, @id
WHILE @@FETCH_STATUS = 0
    BEGIN
       SELECT @servername = srv.name ,
        @datasource = ISNULL(srv.data_source, N‘‘‘‘) ,
        @productName = srv.product ,
        @collationcompatible = CAST(srv.is_collation_compatible AS BIT) ,
        @dataaccess = CAST(srv.is_data_access_enabled AS BIT) ,
        @dist = CAST(srv.is_distributor AS BIT) ,
        @pub = CAST(srv.is_publisher AS BIT) ,
        @rpc = CAST(srv.is_remote_login_enabled AS BIT) ,
        @rpcout = CAST(srv.is_rpc_out_enabled AS BIT) ,
        @sub = CAST(srv.is_subscriber AS BIT) ,
        @connecttimeout = srv.connect_timeout ,
        @lazyschemavalidation = srv.lazy_schema_validation ,
        @querytimeout = srv.query_timeout ,
        @useremotecollation = srv.uses_remote_collation ,
        @remoteproctransactionpromotion = CAST(srv.is_remote_proc_transaction_promotion_enabled AS BIT)
 FROM   sys.servers AS srv
 WHERE  ( srv.server_id != 0 )
        AND ( srv.name = @servername )
        AND ( srv.[server_id] = @id )

 SELECT @remoteuser = ISNULL(ll.remote_name, N‘‘) ,
        @useself = CAST(ll.uses_self_credential AS BIT)
 FROM   sys.servers AS srv
        INNER JOIN sys.linked_logins ll ON ll.server_id = CAST(srv.server_id AS INT)
        LEFT OUTER JOIN sys.server_principals sp ON ll.local_principal_id = sp.principal_id
 WHERE  ( ( srv.server_id != 0 )
          AND ( srv.name = @servername)
        )

        IF (@servername IS NOT NULL AND @id IS NOT NULL)
        BEGIN
        SELECT  @scriptdate=CONVERT(NVARCHAR(200),GETDATE(),120)
        PRINT ‘/*************************************SCRIPT FOR LINKED SERVER: [‘+@servername+‘]****************************************************/‘
        PRINT ‘/********************************************************************************************************************************/‘
        PRINT ‘USE [master]‘+CHAR(13)+‘GO‘
        PRINT ‘/****** Object:  LinkedServer [‘+@servername+‘]    Script Date: ‘+@scriptdate+‘ ******/‘
        PRINT ‘EXEC master.dbo.sp_addlinkedserver @server = N‘‘‘+@servername+‘‘‘, @srvproduct=N‘‘‘+@productName+‘‘‘‘
        PRINT ‘/* For security reasons the linked server remote logins password is changed with ######## */‘

        DECLARE @sql NVARCHAR(2000)
        IF (@remoteuser IS NOT  NULL AND @remoteuser != N‘‘)
        BEGIN
        SET @sql=‘EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N‘‘‘+@datasource+‘‘‘,@useself=N‘‘‘+CASE @useself WHEN 0 THEN ‘false‘ ELSE ‘true‘ END +‘‘‘,@locallogin=NULL,@rmtuser=N‘‘‘+@remoteuser+‘‘‘,@rmtpassword=‘‘########‘‘‘

        END
        ELSE
        BEGIN
        SET @sql=‘EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N‘‘‘+@datasource+‘‘‘,@useself=N‘‘‘+CASE @useself WHEN 0 THEN ‘false‘ ELSE ‘true‘ END +‘‘‘,@locallogin=NULL,@rmtuser=N‘‘‘‘,@rmtpassword=‘‘########‘‘‘

        END
        PRINT @sql
        PRINT ‘GO‘

        PRINT ‘EXEC master.dbo.sp_serveroption @server=N‘‘‘+@servername+‘‘‘, @optname=N‘‘collation compatible‘‘, @optvalue=N‘‘‘+CASE @useself WHEN 0 THEN ‘false‘ ELSE ‘true‘ END+‘‘‘‘
        PRINT ‘GO‘

        PRINT ‘EXEC master.dbo.sp_serveroption @server=N‘‘‘+@servername+‘‘‘, @optname=N‘‘data access‘‘, @optvalue=N‘‘‘+CASE @dataaccess WHEN 0 THEN ‘false‘ ELSE ‘true‘ END+‘‘‘‘
        PRINT ‘GO‘
        PRINT ‘EXEC master.dbo.sp_serveroption @server=N‘‘‘+@servername+‘‘‘, @optname=N‘‘dist‘‘, @optvalue=N‘‘‘+CASE @dist WHEN 0 THEN ‘false‘ ELSE ‘true‘ END+‘‘‘‘
        PRINT ‘GO‘
        PRINT ‘EXEC master.dbo.sp_serveroption @server=N‘‘‘+@servername+‘‘‘, @optname=N‘‘pub‘‘, @optvalue=N‘‘‘+CASE @pub WHEN 0 THEN ‘false‘ ELSE ‘true‘ END+‘‘‘‘
        PRINT ‘GO‘
        PRINT ‘EXEC master.dbo.sp_serveroption @server=N‘‘‘+@servername+‘‘‘, @optname=N‘‘rpc‘‘, @optvalue=N‘‘‘+CASE @rpc WHEN 0 THEN ‘false‘ ELSE ‘true‘ END+‘‘‘‘
        PRINT ‘GO‘
        PRINT ‘EXEC master.dbo.sp_serveroption @server=N‘‘‘+@servername+‘‘‘, @optname=N‘‘rpc out‘‘, @optvalue=N‘‘‘+CASE @rpcout WHEN 0 THEN ‘false‘ ELSE ‘true‘ END+‘‘‘‘
        PRINT ‘GO‘
        PRINT ‘EXEC master.dbo.sp_serveroption @server=N‘‘‘+@servername+‘‘‘, @optname=N‘‘sub‘‘, @optvalue=N‘‘‘+CASE @sub WHEN 0 THEN ‘false‘ ELSE ‘true‘ END+‘‘‘‘
        PRINT ‘GO‘
        PRINT ‘EXEC master.dbo.sp_serveroption @server=N‘‘‘+@servername+‘‘‘, @optname=N‘‘connect timeout‘‘, @optvalue=N‘‘‘+CAST(@connecttimeout AS NVARCHAR(200))+‘‘‘‘
        PRINT ‘GO‘
        PRINT ‘EXEC master.dbo.sp_serveroption @server=N‘‘‘+@servername+‘‘‘, @optname=N‘‘lazy schema validation‘‘, @optvalue=N‘‘‘+CASE @lazyschemavalidation WHEN 0 THEN ‘false‘ ELSE ‘true‘ END+‘‘‘‘
        PRINT ‘GO‘
        PRINT ‘EXEC master.dbo.sp_serveroption @server=N‘‘‘+@servername+‘‘‘, @optname=N‘‘query timeout‘‘, @optvalue=N‘‘‘+CAST(@querytimeout AS NVARCHAR(200))+‘‘‘‘
        PRINT ‘GO‘
        PRINT ‘EXEC master.dbo.sp_serveroption @server=N‘‘‘+@servername+‘‘‘, @optname=N‘‘use remote collation‘‘, @optvalue=N‘‘‘+CASE @useremotecollation WHEN 0 THEN ‘false‘ ELSE ‘true‘ END+‘‘‘‘
        PRINT ‘GO‘
        PRINT ‘EXEC master.dbo.sp_serveroption @server=N‘‘‘+@servername+‘‘‘, @optname=N‘‘remote proc transaction promotion‘‘, @optvalue=N‘‘‘+CASE @remoteproctransactionpromotion WHEN 0 THEN ‘false‘ ELSE ‘true‘ END+‘‘‘‘
        PRINT ‘GO‘

        PRINT ‘/********************************************************************************************************************************/‘
        PRINT ‘/********************************************************************************************************************************/‘
        PRINT CHAR(13)
        PRINT CHAR(13)
        PRINT CHAR(13)

        END 

        FETCH NEXT FROM LinkserverNameCur INTO @servername, @id
    END
CLOSE LinkserverNameCur
DEALLOCATE LinkserverNameCur

如果要迁移登录用户这里有一篇文章

如何在 SQL Server 2005 实例之间传输登录和密码

脚本如下:

USE master
GO
IF OBJECT_ID (‘sp_hexadecimal‘) IS NOT NULL
  DROP PROCEDURE sp_hexadecimal
GO
CREATE PROCEDURE sp_hexadecimal
    @binvalue varbinary(256),
    @hexvalue varchar (514) OUTPUT
AS
DECLARE @charvalue varchar (514)
DECLARE @i int
DECLARE @length int
DECLARE @hexstring char(16)
SELECT @charvalue = ‘0x‘
SELECT @i = 1
SELECT @length = DATALENGTH (@binvalue)
SELECT @hexstring = ‘0123456789ABCDEF‘
WHILE (@i <= @length)
BEGIN
  DECLARE @tempint int
  DECLARE @firstint int
  DECLARE @secondint int
  SELECT @tempint = CONVERT(int, SUBSTRING(@binvalue,@i,1))
  SELECT @firstint = FLOOR(@tempint/16)
  SELECT @secondint = @tempint - (@firstint*16)
  SELECT @charvalue = @charvalue +
    SUBSTRING(@hexstring, @firstint+1, 1) +
    SUBSTRING(@hexstring, @secondint+1, 1)
  SELECT @i = @i + 1
END

SELECT @hexvalue = @charvalue
GO

IF OBJECT_ID (‘sp_help_revlogin‘) IS NOT NULL
  DROP PROCEDURE sp_help_revlogin
GO
CREATE PROCEDURE sp_help_revlogin @login_name sysname = NULL AS
DECLARE @name sysname
DECLARE @type varchar (1)
DECLARE @hasaccess int
DECLARE @denylogin int
DECLARE @is_disabled int
DECLARE @PWD_varbinary  varbinary (256)
DECLARE @PWD_string  varchar (514)
DECLARE @SID_varbinary varbinary (85)
DECLARE @SID_string varchar (514)
DECLARE @tmpstr  varchar (1024)
DECLARE @is_policy_checked varchar (3)
DECLARE @is_expiration_checked varchar (3)

DECLARE @defaultdb sysname

IF (@login_name IS NULL)
  DECLARE login_curs CURSOR FOR

      SELECT p.sid, p.name, p.type, p.is_disabled, p.default_database_name, l.hasaccess, l.denylogin FROM
sys.server_principals p LEFT JOIN sys.syslogins l
      ON ( l.name = p.name ) WHERE p.type IN ( ‘S‘, ‘G‘, ‘U‘ ) AND p.name <> ‘sa‘
ELSE
  DECLARE login_curs CURSOR FOR

      SELECT p.sid, p.name, p.type, p.is_disabled, p.default_database_name, l.hasaccess, l.denylogin FROM
sys.server_principals p LEFT JOIN sys.syslogins l
      ON ( l.name = p.name ) WHERE p.type IN ( ‘S‘, ‘G‘, ‘U‘ ) AND p.name = @login_name
OPEN login_curs

FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin
IF (@@fetch_status = -1)
BEGIN
  PRINT ‘No login(s) found.‘
  CLOSE login_curs
  DEALLOCATE login_curs
  RETURN -1
END
SET @tmpstr = ‘/* sp_help_revlogin script ‘
PRINT @tmpstr
SET @tmpstr = ‘** Generated ‘ + CONVERT (varchar, GETDATE()) + ‘ on ‘ + @@SERVERNAME + ‘ */‘
PRINT @tmpstr
PRINT ‘‘
WHILE (@@fetch_status <> -1)
BEGIN
  IF (@@fetch_status <> -2)
  BEGIN
    PRINT ‘‘
    SET @tmpstr = ‘-- Login: ‘ + @name
    PRINT @tmpstr
    IF (@type IN ( ‘G‘, ‘U‘))
    BEGIN -- NT authenticated account/group

      SET @tmpstr = ‘CREATE LOGIN ‘ + QUOTENAME( @name ) + ‘ FROM WINDOWS WITH DEFAULT_DATABASE = [‘ + @defaultdb + ‘]‘
    END
    ELSE BEGIN -- SQL Server authentication
        -- obtain password and sid
            SET @PWD_varbinary = CAST( LOGINPROPERTY( @name, ‘PasswordHash‘ ) AS varbinary (256) )
        EXEC sp_hexadecimal @PWD_varbinary, @PWD_string OUT
        EXEC sp_hexadecimal @SID_varbinary,@SID_string OUT

        -- obtain password policy state
        SELECT @is_policy_checked = CASE is_policy_checked WHEN 1 THEN ‘ON‘ WHEN 0 THEN ‘OFF‘ ELSE NULL END FROM sys.sql_logins WHERE name = @name
        SELECT @is_expiration_checked = CASE is_expiration_checked WHEN 1 THEN ‘ON‘ WHEN 0 THEN ‘OFF‘ ELSE NULL END FROM sys.sql_logins WHERE name = @name

            SET @tmpstr = ‘CREATE LOGIN ‘ + QUOTENAME( @name ) + ‘ WITH PASSWORD = ‘ + @PWD_string + ‘ HASHED, SID = ‘ + @SID_string + ‘, DEFAULT_DATABASE = [‘ + @defaultdb + ‘]‘

        IF ( @is_policy_checked IS NOT NULL )
        BEGIN
          SET @tmpstr = @tmpstr + ‘, CHECK_POLICY = ‘ + @is_policy_checked
        END
        IF ( @is_expiration_checked IS NOT NULL )
        BEGIN
          SET @tmpstr = @tmpstr + ‘, CHECK_EXPIRATION = ‘ + @is_expiration_checked
        END
    END
    IF (@denylogin = 1)
    BEGIN -- login is denied access
      SET @tmpstr = @tmpstr + ‘; DENY CONNECT SQL TO ‘ + QUOTENAME( @name )
    END
    ELSE IF (@hasaccess = 0)
    BEGIN -- login exists but does not have access
      SET @tmpstr = @tmpstr + ‘; REVOKE CONNECT SQL TO ‘ + QUOTENAME( @name )
    END
    IF (@is_disabled = 1)
    BEGIN -- login is disabled
      SET @tmpstr = @tmpstr + ‘; ALTER LOGIN ‘ + QUOTENAME( @name ) + ‘ DISABLE‘
    END
    PRINT @tmpstr
  END

  FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin
   END
CLOSE login_curs
DEALLOCATE login_curs
RETURN 0
GO

如果脚本有错误的话可以跟我反映

如有不对的地方,欢迎大家拍砖o(∩_∩)o 哈哈

时间: 2024-10-11 05:13:33

分享一个批量导出当前实例下的所有linkedserver脚本的相关文章

[转]分享一个批量生成iOS App icons 及 LaunchImages的工具

这是一个LY自己在使用的mac app,绿色的不 可以批量由一张图片生成N多张app icons 及 launchIamges,如下图所示: 唯一美中不足的是,在Xcode 6.1所用的icon中,有一个[email protected],它未生成,如下所示: 不过这并不影响我们的使用,所谓的3x,意即60x60 Size的3倍,由[email protected]改为180x180尺寸,另存为[email protected]即可.  Art Helper.app.zip (669.03 KB

分享一个生产环境中利用SVN上线代码的脚本

本文讲述的是利用SVN+RSYNC上线代码的生产案例,适用于小型公司及企业,让运维从频繁的上线代码中解脱出来.以下对应的web服务器也适用于Windows服务器及Linux服务器 REPOS="$1" REV="$2" export LANG=zh_CN.UTF-8 export RSYNC_PASSWORD=RSYNC_PASSWORD RSYNC_USER=RSYNC_USER PROJECT_NAME=`basename $REPOS` SVN_DIR=/sv

分享一个很实用的svn自动merge的shell脚本

正在上传中...... 预计8月15前完成

今天写了一个批量检查ip是否ping得通的脚本,备忘

脚本内容如下,需要配合ip.txt使用.ip.txt里写上需要检查的ip for i in `cat ./ip.txt` do { ping -c 1 $i >/dev/null 2>&1 if [ ! $? -eq 0 ] then echo date >>./error_ip.txt echo $i >>./error_ip.txt echo >>./error_ip.txt fi }& done wait

一个简单的lunix系统下的jar 启动脚本

一.新建shell脚本 start.sh #!/bin/sh ##文件名 fileName="test" ##PID存放地址 当前目录 pidPath="./$fileName-tpid" rm -f $pidPath ##后台运行并输出日志文件 nohup java -jar ./$fileName.jar > ./out.log 2>&1 & echo $! >$pidPath 原文地址:https://www.cnblogs.

分享一个免杀的反弹shell工具(python脚本转换成exe)

有时候网站服务器上有杀毒软件,我们上传的nc.exe.lcx.exe等工具都被杀了,这时候就需要一款免杀的工具来反弹shell. 这篇博客主要是依据国外的一片文章翻译而来,根据国外大佬的教程将Python脚本转换成exe程序即可免杀. 参考链接:https://medium.com/bugbountywriteup/antivirus-evasion-with-python-49185295caf1 第1步:安装Python2.7和Py2exe 一定要安装32位的Python 2.7和32位的P

如何收藏互联网上的任意网页到系统某个分类下,之后进行批量导出发布等---博客备份专家的博文收藏功能您不可不知

我们常用的有道云笔记,印象笔记等软件都有一个实用的功能,复制任何你看到的喜欢的网页到笔记. 新版的博客备份专家也支持了这个功能.比有道云笔记更为强大的地方在于,等收藏的文章多了的时候,您可以通过博客备份专家最常用的导出PDF,CHM功能将这些您逐渐收藏的博文导出成一个专集,之后是离线浏览还是放到手机阅读随你便.接下来,我们就看看这个功能如何使用. 首先,假设您已经会新建收藏分类,如果还不会请先移步阅读一下之前我们写过的 [很喜欢看某方面的文章,如何将不同站点,不同博主同一类别的文章归类整合到一起

分享一个脚本建多级目录的脚本,批量复制的脚本

linux下的bash脚本,据说很简单,但是我没有学过脚本,甚至语法的书我都没看过,所以,脚本不会写,但大概能看懂啥意思,也不想刻意花大量时间去学. 这个脚本是这样的,我要在某个目录下建立很多目录,例如000,001,002,003...大概1000个,而在每一个子目录中还要建立子目录,也是000,001,002,...大概1000个,最终这些最底层目录下放文件,文件的目录大概是这样data/000/008/*.txt,现在要建立这样的目录结构,人工去建立肯定是打死也不想做的,于是想简单偷懒,从

百度网盘批量分享文件并导出分享链接密码手机

现在谈谈如何批量分享网盘文件,使文件夹中的每个文件形成单独的一个个分享链接.分享的链接可以进行自定义有效期.密码等,进而可将分享的每条链接导出等.那么,该如何批量分享网盘内的文件或文件夹呢?请在pc端操作非手机端使用 一.下载批量分享助手 首先下载百度网盘批量处理大师 软件,下载链接: https://www.lanzous.com/b825731/ 二.运行百度云盘批量分享工具 下载好后解压运行其中的 bdmaster.exe软件,软件界面如同网页版一样,直观可视,登录后并可开始使用. 三.自