简体   繁体   English

Powershell 脚本通过 Sql 以表格格式查询 output?

[英]Powershell script to get output through Sql query in table format attached in email?

The below script is resulting in the error below when attempt to send mail is made.尝试发送邮件时,以下脚本会导致以下错误。

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在 line:22 char:18 +... onnection = New-Object System.Data.SqlClient.SqlConnection $SqlCon... + ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo: InvalidArgument: (:) [New-Object], ParameterBindingException + FullyQualifiedErrorId:PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

Exception calling "Fill" with "1" argument(s): "Login failed for user ''."使用“1”参数调用“填充”的异常:“用户''登录失败。” At line:29 char:1 + $SqlAdapter.Fill($DataSet) + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo: NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId: SqlException在 line:29 char:1 + $SqlAdapter.Fill($DataSet) + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo: NotSpecified: (: ) [], MethodInvocationException + FullyQualifiedErrorId: SqlException

Send-MailMessage: Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Send-MailMessage:无法将“System.Object[]”转换为参数“Body”所需的“System.String”类型。 Specified method is not supported.不支持指定的方法。 At line:44 char:17 + -BodyAsHtml $html_table ` + ~~~~~~~~~~~ + CategoryInfo: InvalidArgument: (:) [Send-MailMessage], ParameterBindingException + FullyQualifiedErrorId: CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage在 line:44 char: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'

This should work for you.这应该适合你。 One issue you had is that the variable $dt was never initialized in your script.您遇到的一个问题是变量$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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM