Get only selected columns and combine two output results

  • SQLAddict01

    Say Hey Kid

    Points: 690

    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 two things:

    1.  How do I get  only few columns displayed in the output? for example the backup-dbadatabase gives me about 20 columns which I don't need
    2.  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

    $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

  • Sue_H

    SSC Guru

    Points: 90707

    In reference to your first question, you can pipe the output and use select (select-object) to select the columns. If you were just doing the backup with something like $Results = backup-dbadatabase -sqlinstance etc then you can select the columns from $Results like:

    $Results | select sqlinstance, Database, Start

    In terms of multiple commands, if subsequent result sets contain different column lists than the first, those result sets are not displayed. I haven't played with the update-dbainstance but I would guess that is the issue. Try using different two different variables for results for the two different commands.

    Sue

  • SQLAddict01

    Say Hey Kid

    Points: 690

    Thank you I got the first one working. Still trying to  figure out the multiple outputs one.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply