Combine two output results

Welcome Forums General PowerShell Q&A Combine two output results

  • This topic has 5 replies, 3 voices, and was last updated 2 months ago by
    Participant
    .
Viewing 5 reply threads
  • Author
    Posts
    • #200504
      Participant
      Topics: 1
      Replies: 1
      Points: 17
      Rank: Member

      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

       

       

    • #200516
      Participant
      Topics: 4
      Replies: 1908
      Points: 4,259
      Helping Hand
      Rank: Community Hero

      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. 😉

      how do I get the results of second command in the email?

      What second command?

    • #200570
      Moderator
      Topics: 3
      Replies: 212
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      I concur with Olaf.

      $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

      ; 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

      $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
      

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

      Thank you.

    • #200690
      Participant
      Topics: 1
      Replies: 1
      Points: 17
      Rank: Member

      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!

    • #200840
      Moderator
      Topics: 3
      Replies: 212
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      Then you should handle them separately.

      Thank you.

       

    • #200843
      Participant
      Topics: 4
      Replies: 1908
      Points: 4,259
      Helping Hand
      Rank: Community Hero

      I need all the columns from 1st and 2nd commands.

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

Viewing 5 reply threads
  • You must be logged in to reply to this topic.