简体   繁体   English

获取SQL Replication PowerShell脚本到电子邮件输出

[英]Get SQL Replication PowerShell script to email output

I am utilizing the following script to output replication status between SQL Servers in out environment. 我正在利用以下脚本在外部环境中的SQL Server之间输出复制状态。 The script works great and gives me what I need in the PowerShell console, however I need to email the results from the script to a specific DL. 该脚本很好用,可以在PowerShell控制台中提供所需的内容,但是我需要将脚本中的结果通过电子邮件发送到特定的DL。

I've tried a few methods but can't seem to get it to output/format properly. 我尝试了几种方法,但似乎无法正确输出/格式化。

##Cross-Server Replication Monitor
## Script referenced http://www.madeirasql.com/cross-server-replication-health-check-using-      powershell/

##If not Loaded, Please Load SQLServer Snapins Into PowerShell By Running The Following Cmdlets:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended")
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100

##If Script Execution Is Disabled Please Run The Following cmdlet:
##Set-ExecutionPolicy RemoteSigned

## Beginning of Monitor

## Path To Server ListFile: Edit Path and List Target Servers In File With Line Breaks, For Named     Instances Use The Typical "Server\Instance" Convention
 foreach ( $svr in get-content "C:\Scripts\Servers.txt") 
{
##Connection String With Server Variable, Distribution Database name is 'Distribution'
$con = "server= $svr;database=Distribution;Integrated Security=sspi" 

##Begin SQL Query
##Refreshing Replication Monitor Cache
$cmd = "SET NOCOUNT ON; EXEC sp_replmonitorhelppublisher" 
##Getting Info From Replication Sp and Tables, Joining and Selecting 
$cmd = $cmd + " CREATE TABLE #COUNTERS"
$cmd = $cmd + " ("
$cmd = $cmd + " [DATABASE]       SYSNAME,"
$cmd = $cmd + " REPLICATEDTRANS  INT,"
$cmd = $cmd + " REPRATESEC       FLOAT,"
$cmd = $cmd + " REPLATENCY       FLOAT,"
$cmd = $cmd + " LSN1             BINARY(10),"
$cmd = $cmd + " LSN2             BINARY(10)"
$cmd = $cmd + " )"
$cmd = $cmd + " INSERT INTO #COUNTERS"
$cmd = $cmd + " EXEC sp_replcounters"
$cmd = $cmd + " SELECT DISTINCT "
$cmd = $cmd + "        m.Publisher_db,"
$cmd = $cmd + "        m.Publication,"
$cmd = $cmd + "        s.name AS 'Subscriber',"
$cmd = $cmd + "        ma.Subscriber_db,"
$cmd = $cmd + "        CASE [Status]"
$cmd = $cmd + "             WHEN 1 THEN 'Started'"
$cmd = $cmd + "             WHEN 2 THEN 'Succeeded'"
$cmd = $cmd + "             WHEN 3 THEN 'In Progress'"
$cmd = $cmd + "             WHEN 4 THEN 'Idle'"
$cmd = $cmd + "             WHEN 5 THEN 'Retrying'"
$cmd = $cmd + "             WHEN 6 THEN 'Failed'"
$cmd = $cmd + "        END AS [Status],"
$cmd = $cmd + "        CASE warning"
$cmd = $cmd + "             WHEN 0 THEN NULL"
$cmd = $cmd + "             WHEN 1 THEN 'Expiration'"
$cmd = $cmd + "             WHEN 2 THEN 'Latency'"
$cmd = $cmd + "             ELSE 'MergeWarning'"
$cmd = $cmd + "        END AS [Warning],"
$cmd = $cmd + "        C.REPLICATEDTRANS AS 'Awaiting Transactions To Dist',"
$cmd = $cmd + "        CONVERT(INT, c.REPRATESEC) AS 'Avrage Trans/Sec to Dist',"
$cmd = $cmd + "        CONVERT(DECIMAL(10, 2), c.REPLATENCY) AS 'Avg. Latency to Dist/Sec',"
$cmd = $cmd + "        CONVERT(DECIMAL(10, 2), mm.cur_latency) AS 'Avg. Latency to Subscriber'"
$cmd = $cmd + "        INTO #MonitorRepl"
$cmd = $cmd + " FROM   dbo.MSpublications m"
$cmd = $cmd + "        INNER JOIN dbo.MSreplication_monitordata mm"
$cmd = $cmd + "             ON  mm.publisher_db = m.publisher_db"
$cmd = $cmd + "             AND mm.publication_id = m.publication_id"
$cmd = $cmd + "             AND mm.agent_type = 3"
$cmd = $cmd + "        INNER JOIN dbo.MSdistribution_agents ma"
$cmd = $cmd + "             ON  ma.publisher_id = m.publisher_id"
$cmd = $cmd + "             AND ma.publication = mm.publication"
$cmd = $cmd + "             AND ma.job_id = mm.job_id"
$cmd = $cmd + "        INNER JOIN sys.servers s"
$cmd = $cmd + "             ON  ma.subscriber_id = s.server_id"
$cmd = $cmd + "        INNER JOIN #COUNTERS C"
$cmd = $cmd + "             ON  m.publisher_db = C.[DATABASE]"
$cmd = $cmd + " ORDER BY"
$cmd = $cmd + "        m.publisher_db,"
$cmd = $cmd + "        m.publication,"
$cmd = $cmd + "        s.name,"
$cmd = $cmd + "        ma.subscriber_db"
$cmd = $cmd + "        DROP TABLE #COUNTERS"
$cmd = $cmd + " SELECT *"
$cmd = $cmd + " FROM   #MonitorRepl"
$cmd = $cmd + " DROP TABLE #MonitorRepl"
##Creating DataSet Object
$set = new-object system.data.dataset
##Running Query
$da = new-object System.Data.SqlClient.SqlDataAdapter ($cmd, $con)
##Filling DataSet With Results
$da.fill($set) | out-null
##Creating Table Object and Inserting DataSet
$dt = new-object System.Data.DataTable
$dt = $set.Tables[1]
##Displaying Current Server Name
$svr
##Formating DataTable To A Readable List And Presenting
$dt|Format-List 

} }

Thanks in advance! 提前致谢! If anyone already has a ready to use HTML output script for this purpose I wouldn't mind giving that a whirl either! 如果有人已经准备好为此目的使用HTML输出脚本,那么我也不会介意!

Not necessarily an answer, but I wanted to comment that you can more efficiently pass a query to sql by using a here string 不一定是答案,但我想评论一下,您可以使用here字符串更有效地将查询传递给sql

$Query = @"
ALTER DATABASE $dbname SET MULTI_USER;
ALTER DATABASE $dbname SET ONLINE;
BACKUP DATABASE $dbname TO DISK = '$DBbak' WITH INIT;
ALTER DATABASE $dbname SET OFFLINE WITH ROLLBACK IMMEDIATE;
"@

The only thing to note with that is the "@ on the last line can't be indented for some reason or it will break the string. 唯一需要注意的是,由于某种原因,最后一行的@不能缩进,否则它将破坏字符串。

There is also a cmdlet, ConvertTo-HTML...not sure if you've tried that. 还有一个cmdlet,ConvertTo-HTML ...不确定您是否尝试过。

Also, you may find some useful information on html reporting here: http://powershell.org/wp/ebooks/ 另外,您可以在此处找到有关html报告的一些有用信息: http : //powershell.org/wp/ebooks/

There is a free ebook with a lot of info on it. 有免费的电子书,上面有很多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM