我正在通过Powershell通过电子邮件发送CSV文件,如果不要求输入对象,它将无法运行。
我不确定如何填充此参数。我应该指出,我可以在没有值的情况下“输入”提示,并且它可以正常工作。但这应该是自动化的。 :)
$SQLServer = "server"
$SQLDBName = "dbname"
$uid ="uid"
$pwd = "pwd"
$SqlQuery = 'SELECT TOP 1 * FROM TABLE'
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "string"
$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)
$DataSet.Tables[0] | Export-CSV -NoTypeInformation "C:\Testing\Stuff.csv"
$SqlConnection.Close()
$file = 'C:\Testing\stuff.csv'
Export-Csv -Force -NoTypeInformation -Path $file
$options = @{
'SmtpServer' = "mail.company.com"
'To' = "Me <me@company.com>"
'From' = "<donotreply@company.com>"
'Subject' = "Stuff"
'Body' = "text about stuff"
'Attachments' = $file
}
Send-MailMessage @options
我希望它能在没有任何用户提示的情况下正常工作。
答案 0 :(得分:3)
您实际上并没有在第二Export-csv
上导出任何内容,这是在提示您。如果您在没有任何输入的情况下运行Export-Csv
,则会提示您输入。如果删除它,那应该很好。
$SQLServer = "server"
$SQLDBName = "dbname"
$uid ="uid"
$pwd = "pwd"
$SqlQuery = 'SELECT TOP 1 * FROM TABLE'
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "string"
$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)
$DataSet.Tables[0] | Export-CSV -NoTypeInformation "C:\Testing\Stuff.csv"
$SqlConnection.Close()
$file = 'C:\Testing\stuff.csv'
****REMOVE THIS****
Export-Csv -Force -NoTypeInformation -Path $file
*******************
$options = @{
'SmtpServer' = "mail.company.com"
'To' = "Me <me@company.com>"
'From' = "<donotreply@company.com>"
'Subject' = "Stuff"
'Body' = "text about stuff"
'Attachments' = $file
}
Send-MailMessage @options