Get SQL Replication PowerShell script to email output -
i utilizing following script output replication status between sql servers in out environment. script works great , gives me need in powershell console, need email results script specific dl.
i've tried few methods can't seem 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 powershell running 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 disabled please run following cmdlet: ##set-executionpolicy remotesigned ## beginning of monitor ## path server listfile: edit path , list target servers in file line breaks, named instances use typical "server\instance" convention foreach ( $svr in get-content "c:\scripts\servers.txt") { ##connection string server variable, distribution database name '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 replication sp , tables, joining , 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 #counters" $cmd = $cmd + " exec sp_replcounters" $cmd = $cmd + " select distinct " $cmd = $cmd + " m.publisher_db," $cmd = $cmd + " m.publication," $cmd = $cmd + " s.name 'subscriber'," $cmd = $cmd + " ma.subscriber_db," $cmd = $cmd + " case [status]" $cmd = $cmd + " when 1 'started'" $cmd = $cmd + " when 2 'succeeded'" $cmd = $cmd + " when 3 'in progress'" $cmd = $cmd + " when 4 'idle'" $cmd = $cmd + " when 5 'retrying'" $cmd = $cmd + " when 6 'failed'" $cmd = $cmd + " end [status]," $cmd = $cmd + " case warning" $cmd = $cmd + " when 0 null" $cmd = $cmd + " when 1 'expiration'" $cmd = $cmd + " when 2 'latency'" $cmd = $cmd + " else 'mergewarning'" $cmd = $cmd + " end [warning]," $cmd = $cmd + " c.replicatedtrans 'awaiting transactions dist'," $cmd = $cmd + " convert(int, c.repratesec) 'avrage trans/sec dist'," $cmd = $cmd + " convert(decimal(10, 2), c.replatency) 'avg. latency dist/sec'," $cmd = $cmd + " convert(decimal(10, 2), mm.cur_latency) 'avg. latency subscriber'" $cmd = $cmd + " #monitorrepl" $cmd = $cmd + " dbo.mspublications m" $cmd = $cmd + " inner join dbo.msreplication_monitordata mm" $cmd = $cmd + " on mm.publisher_db = m.publisher_db" $cmd = $cmd + " , mm.publication_id = m.publication_id" $cmd = $cmd + " , mm.agent_type = 3" $cmd = $cmd + " inner join dbo.msdistribution_agents ma" $cmd = $cmd + " on ma.publisher_id = m.publisher_id" $cmd = $cmd + " , ma.publication = mm.publication" $cmd = $cmd + " , 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 + " #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 results $da.fill($set) | out-null ##creating table object , inserting dataset $dt = new-object system.data.datatable $dt = $set.tables[1] ##displaying current server name $svr ##formating datatable readable list , presenting $dt|format-list
}
thanks in advance! if has ready use html output script purpose wouldn't mind giving whirl either!
not answer, wanted comment can more efficiently pass query sql using here string
$query = @" alter database $dbname set multi_user; alter database $dbname set online; backup database $dbname disk = '$dbbak' init; alter database $dbname set offline rollback immediate; "@
the thing note "@ on last line can't indented reason or break string.
there cmdlet, convertto-html...not sure if you've tried that.
also, may find useful information on html reporting here: http://powershell.org/wp/ebooks/
there free ebook lot of info on it.
Comments
Post a Comment