Powershell 无法理解此参数返回空值的原因
此PowerShell脚本运行SQL查询并填充Excel电子表格,然后通过电子邮件发送该电子表格:Powershell 无法理解此参数返回空值的原因,powershell,Powershell,此PowerShell脚本运行SQL查询并填充Excel电子表格,然后通过电子邮件发送该电子表格: $FileName = "C:\Users\user\Documents\Report.xlsx"; $ConnectionString = "OurConnectionString" $secpasswd = "emailpassword" $cred = New-Object System.Management.Automation.PSCredential ("emailaddress",
$FileName = "C:\Users\user\Documents\Report.xlsx";
$ConnectionString = "OurConnectionString"
$secpasswd = "emailpassword"
$cred = New-Object System.Management.Automation.PSCredential ("emailaddress", $secpasswd)
$SmtpCred = $cred
$ToAddress = 'to@contoso.com'
$FromAddress = 'from@contoso.com'
$SmtpServer = 'smtp.server.com'
$SmtpPort = '587'
$Subject = 'Report'
$Body = "Here is your report"
$SqlQuery = @"
SELECT TOP 10 * FROM dbo.table
"@;
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection;
$SqlConnection.ConnectionString = $ConnectionString;
$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);
$DataSetTable = $DataSet.Tables["Table"];
$xlsFile = @($DataSetTable | Export-Excel $FileName -AutoSize)
$SqlConnection.Close()
$Attachment = $xlsFile
$mailparam = @{
To = $ToAddress
From = $FromAddress
Subject = $Subject
Body = $Body
Attachments = $Attachment
SmtpServer = $SmtpServer
Port = $SmtpPort
Credential = $SmtpCred
}
Send-MailMessage @mailparam -UseSsl
运行此操作时出现的错误是:
Send-MailMessage : Cannot validate argument on parameter 'Attachments'. The
argument is null, empty, or an element of the argument collection contains a
null value. Supply a collection that does not contain any null values and then
try the command again.
At C:\Users\Desktop\Untitled1.ps1:58 char:18
+ Send-MailMessage @mailparam -UseSsl
+ ~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage
发送邮件消息:无法验证参数“附件”上的参数。这个
参数为null、空或参数集合的元素包含
空值。提供一个不包含任何空值的集合,然后
请重试该命令。
在C:\Users\Desktop\Untitled1.ps1:58 char:18
+发送MailMessage@mailparam-usesl
+ ~~~~~~~~~~
+CategoryInfo:InvalidData:(:)[Send MailMessage],参数BindingValidationException
+FullyQualifiedErrorId:ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage
我搞不清楚
$Attachment
是如何以null或空结尾的?我知道我以前也写过类似的剧本,效果很好。我的脚本中是否遗漏了其他人可以看到的内容?此外,从技术上讲,我不需要先将Excel文件保存到文件系统中,我只需要将其作为附件发送电子邮件。因此,如果答案排除了保存到文件系统部分的操作(如果这是可以做到的话),那么就可以了。参数-Attachments
需要一个字符串或字符串列表
看
这应该是需要附加的文件的完整路径,因此需要先存储这些文件。一般来说,如果我以后不需要这些文件,你可以删除它们。还请注意,您需要传递全名
由于我看不到导出Excel
函数的输出,我假设它返回一个文件对象,那么这应该可以工作
$mailparam = @{
To = $ToAddress
From = $FromAddress
Subject = $Subject
Body = $Body
Attachments = $xlsFile.FullName
SmtpServer = $SmtpServer
Port = $SmtpPort
Credential = $SmtpCred
}
如果没有返回文件对象,则需要首先获取该项并传递“全名”
$MyExcelFile = Get-Item -Path 'C:\MyExcelFile.xlsx'
$mailparam = @{
To = $ToAddress
From = $FromAddress
Subject = $Subject
Body = $Body
Attachments = $MyExcelFile.FullName
SmtpServer = $SmtpServer
Port = $SmtpPort
Credential = $SmtpCred
}