Combine two output results

I am very very new to powershell

Working on doing some automated patching in our SQL server environments. I am first taking a DB backup and then applying patches and then sending output in an email

I need to figure out the below:

how do I get the results of second command in the email? when I run this script I only get the output of the first command in the email. Columns in both the scripts are different.

 

$Results = backup-dbadatabase -sqlinstance abcsqlserver -database ‘abc’ -path \abcsqlserver\Backup -type full -copyonly -compressbackup -ignorefilecheck;update-dbainstance abcsqlserver -KB 4019094 -path \abcsqlserver\Patches -restart -confirm:$false

$to = ‘abc@abc.com’

$smtp = ‘mailrelay.abc.local’

$from = ‘abc@abc.com’

$subject = ‘SQL Server Patching on abcsqlserver’

$Body = $Results | convertto-html | Out-String

Send-MailMessage -To $to -From $from -Body $Body -bodyashtml -Subject $subject -SmtpServer $smtp

 

Any help is greatly appreciated

Thanks

 

 

Welcome to Powershell.org.

Please (re-)read the very first pinned post of this forum Read Me Before Posting! You’ll be Glad You Did!.

When you post code please format it as code using the code tag button (“PRE”). And you may try to avoid posting too much unnecessary white space. :wink:

What second command?

I concur with Olaf.

[quote quote=200504]$Results = backup-dbadatabase -sqlinstance abcsqlserver -database ‘abc’ -path \abcsqlserver\Backup -type full -copyonly -compressbackup -ignorefilecheck;update-dbainstance abcsqlserver -KB 4019094 -path \abcsqlserver\Patches -restart -confirm:$false[/quote]; is a command separator in PowerShell, so $Results will have only the first command output and if you want the second command output as well then need to append the output of the second command to $Results

[pre]
$Results = @()
$Results += backup-dbadatabase -sqlinstance abcsqlserver -database ‘abc’ -path \abcsqlserver\Backup -type full -copyonly -compressbackup -ignorefilecheck;
$Results += update-dbainstance abcsqlserver -KB 4019094 -path \abcsqlserver\Patches -restart -confirm:$false
[/pre]

Not sure what is the exact output from these commands, given suggestion considering they are string or int

Thank you.

Thanks!

When I run the append script, it only gives results from 1 st command and also the common fields from 1st and 2nd command, but not all the columns from 2nd command

for example:

The first command output is

BackupComplete BackupFile DatabaseName ComputerName InstanceName SqlInstance Start End
True Maint_202001281343.bak Maint abcsqlserver MSSQLSERVER abcsqlserver 1/28/2020 1:45:35 PM 1/28/2020 1:45:35 PM
 

and second command output is

ComputerName MajorVersion Build Architecture TargetVersion TargetLevel KB Successful Restarted
abcsqlserver 2014 12.0.5553 x64 @{SqlInstance=; Build=12.0.5556; NameLevel=2014; SPLevel=SP2; CULevel=CU7; KBLevel=4032541; BuildLevel=12.0.5556; SupportedUntil=1/14/2020 12:00:00 AM; MatchType=Exact; Warning=} SP2CU7 4032541 True False
I need all the columns from 1st and 2nd commands.

Thanks!

Then you should handle them separately.

Thank you.

 

You could create them seperately as fragments of an html file with ConvertTo-Html with the parameter -Fragment and join them afterwards.