【问题标题】:Powershell script to get output through Sql query in table format attached in email?Powershell脚本通过电子邮件中附加的表格格式的Sql查询获取输出?
【发布时间】:2020-08-14 20:04:47
【问题描述】:

以下脚本在尝试发送邮件时导致以下错误。

New-Object:找不到接受参数“=”的位置参数。 在行:22 字符:18 + ... onnection = 新对象 System.Data.SqlClient.SqlConnection $SqlCon ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [New-Object], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

使用“1”参数调用“Fill”的异常:“用户''登录失败。” 在行:29 字符:1 + $SqlAdapter.Fill($DataSet) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SqlException

Send-MailMessage:无法将“System.Object[]”转换为参数“Body”所需的类型“System.String”。不支持指定的方法。 在行:44 字符:17 + -BodyAsHtml $html_table ` + ~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage

$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'

【问题讨论】:

  • 我建议为您的代码添加更多格式。 (以$Servers = ...开头的块)
  • 所以被注释掉的部分导致了错误?
  • @derekbaker783 底部的电子邮件部分说“字符串没有终止符”
  • @derekbaker783 在 $Servers 下,我传递了带有数据库服务器名称的输入 csv 文件,在 $sqldbname 下,我传递了数据库名称,在 $SQLQuery 中,我传递了 sql 查询。
  • 看看thisthis是否有帮助。

标签: sql powershell


【解决方案1】:

这应该适合你。您遇到的一个问题是变量$dt 从未在您的脚本中初始化。

param(
    $emailFrom = 'Reporting.Services@accenture.com',
    $emailTo = 'aditi.m.singh@accenture.com',
    $emailSubject = 'Sending the Attachment',
    $smtp = 'AMRINT.SMTP.ACCENTURE.COM',
    $Server = "$Env:ComputerName\MSSQLSERVER01",
    $SQLDBName = 'Master',
    $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 = $Server; Database = $SQLDBName; Integrated Security=true;"    
$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()               

$data = $DataSet.Tables[0]
$html = $data `
    | Select-Object -Property RoleName, UserName `
        | ConvertTo-Html -fragment `
            | Out-String

Send-MailMessage `
    -From $emailFrom `
    -To $emailTo `
    -Subject $emailSubject `
    -BodyAsHtml $html `
    -SmtpServer $smtp

【讨论】:

  • 这个答案的一个小问题是 $SQLDBName 对于给定的 SQL 查询应该是 = 'ReportServer' 而不是 Master。
猜你喜欢
  • 2014-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-15
  • 1970-01-01
  • 1970-01-01
  • 2015-03-10
  • 2020-04-29
相关资源
最近更新 更多