Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to union results in Powershell?

$
0
0

Hi!

I am running two separate Invoke SQLcmd -Querys from powershell

Both of them work individually, but when I put them together only the results of the first query appear. Is there a way to do it so that the results of both appear (one after the other)? Maybe like a union in SQL?

Here is my code:

<# 1st query CHECK IF DBS ARE OFFLINE#>
Invoke-Sqlcmd -Query "SET NOCOUNT ON
DECLARE	@Msg VARCHAR(8000)

SELECT	@Msg  = ISNULL(@Msg + CHAR(13), ' ') + z.Name + ', '
FROM	(
SELECT	 TOP 100 PERCENT @@SERVERNAME ServerName,
Name,
CONVERT(VARCHAR, DATABASEPROPERTYEX(Name, 'Status')) Status,
CONVERT(VARCHAR, GETDATE(), 109) Now
FROM	 master..sysdatabases
WHERE	 status & 512 = 512
ORDER BY	Name
) z
if @Msg like '%, %'
begin
set @Msg = (select substring(@Msg, 1, len(@Msg) -1))
end
If @Msg is null
select lower(@@servername) as [SERVERNAME], 'All Dbs online' as [OFFLINE DATABASES]
else

select lower(@@servername) as [SERVERNAME], @Msg as [OFFLINE DATABASES]
" -ServerInstance "Instance1"<# 2nd query lOOK FOR ERRORS IN LOG#>

Invoke-Sqlcmd -Query "SET NOCOUNT ON
declare @hours int
set @hours = 24

create table #errorlog
(logdate datetime, processinfo varchar(50),
[text] varchar(8000))

insert into #errorlog
exec sp_readerrorlog

delete from #errorlog
where logdate < cast(dateadd(hh,-@hours,
getdate()) as varchar(23))
or  text like
 '%\mssql\log\errorlog%'
or text like 
'the error log has been reinitialized. see the previous log for older entries.'
if exists (select * from #errorlog where text like '%error%' or text like '%failed%')
	select @@servername as [instance name], 
	(select 'SQL Log contains errors in last 24 hours. Please Check')
else
	select @@servername as [INSTANCE NAME], (select 'No Log errors') as ERRORS
drop table #errorlog" -ServerInstance "instance2"

Thanks,

Zoe


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>