title: Powershell script to get output through Sql query in table format attached in email?
body: <p><strong>The below script is resulting in the error below when attempt to send mail is made.</strong></p>
  
  <p>New-Object : A positional parameter cannot be found that accepts argument '='.
  At line:22 char:18
  + ... onnection = New-Object System.Data.SqlClient.SqlConnection    $SqlCon ...
  +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidArgument: (:) [New-Object], ParameterBindingException
      + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewObjectCommand</p>
  
  <p>Exception calling "Fill" with "1" argument(s): "Login failed for user ''."
  At line:29 char:1
  + $SqlAdapter.Fill($DataSet)
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : SqlException</p>
  
  <p>Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.
  At line:44 char:17
  +     -BodyAsHtml $html_table `
  +                 ~~~~~~~~~~~
      + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
      + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage</p>
  
  <pre class="lang-bsh prettyprint-override"><code>$Servers = (Import-Csv -Path "D:\Scripts\input.csv").ComputerName    
  $SQLDBName = "ReportServer"    
  $SQLQuery = @"
      SELECT Distinct
          RL.RoleName,
          USR.UserName
      FROM  
          Catalog C
          INNER JOIN Policies PL 
                  ON C.PolicyID = PL.PolicyID
          INNER JOIN PolicyUserRole PUR
                  ON PUR.PolicyID = PL.PolicyID
          INNER JOIN Users USR
                  ON PUR.UserID = USR.UserID
          INNER JOIN dbo.Roles RL
                  ON RL.RoleID = PUR.RoleID    
          WHERE RoleName = 'Content Manager' 
          ORDER BY USR.UserName
  "@
  
  # This code connects to the SQL server and retrieves the data    
  $SQLConnection = New-Object System.Data.SqlClient.SqlConnection    $SqlConnection.ConnectionString = "Server = $Servers; Database = $SQLDBName;"    
  $SqlCmd = New-Object System.Data.SqlClient.SqlCommand    
  $SqlCmd.CommandText = $SqlQuery    
  $SqlCmd.Connection = $SqlConnection    
  $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter    
  $SqlAdapter.SelectCommand = $SqlCmd    
  $DataSet = New-Object System.Data.DataSet    
  $SqlAdapter.Fill($DataSet)    
  $SqlConnection.Close()               
  
  # This code outputs the retrieved data
  $html = $DataSet.Tables[0] | ConvertTo-Html -fragment    
  $results = $DataSet.Tables | format-table -autosize | out-string    
  $mail_body = $results
  
  # Send the email    
  $html_table = $dt | sort-object "Status" | ConvertTo-Html -Fragment  
  
  Send-MailMessage `
      -From "Reporting.Services@accenture.com" `
      -To 'aditi.m.singh@accenture.com' `
      -Subject 'Sending the Attachment' `
      -BodyAsHtml $html_table `
      -SmtpServer 'AMRINT.SMTP.ACCENTURE.COM'
  </code></pre>