【发布时间】: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 查询。
标签: sql powershell