Currenly, 저는 SQL 서버에서 powershell 작업을 설정하여 일부 결과를 테이블 형식으로 데이터베이스 메일로 보냅니다.Powershell SQL 서버의 쿼리 결과를 이메일로 보내기
$SMTPProperties = @{
To = "[email protected]","[email protected]"
Cc = "[email protected]"
From = "[email protected]"
Subject = "SQL Report Status"
SMTPServer = "192.168.xx.xx"
}
$server = "192.168.xx.xx"
$database = "DBName"
$username = "abc"
$password = "abc124"
$query = "select top 10* from testing"
function ExecuteSqlQuery ($Server, $Database, $query) {
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$Server';database='$Database';User ID='$username'; Password='$password';trusted_connection=true;"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = $query
$Reader = $Command.ExecuteReader()
$Datatable = New-Object System.Data.DataTable
$Datatable.Load($Reader)
$Connection.Close()
return $Datatable
}
$resultsDataTable = New-Object System.Data.DataTable
$resultsDataTable = ExecuteSqlQuery $Server $Database $query
Send-MailMessage @SMTPProperties -Body $query -BodyAsHTML | Format-Table
몇 가지 질문이 온다 :
1 여기 내 스크립트입니다. @SMTPProperties
에서 여러 수신자에게 어떻게 보낼 수 있습니까? 해결됨
. 스크립트는 작동하지만 수신 된 전자 메일의 내용에서 (select top 10* from testing)
이라는 텍스트를 반환합니다. 쿼리 결과가 아닙니다.
. 전자 메일에 HTML 테이블을 출력하도록 스크립트가 올바 릅니까 내용? 그렇지 않다면 어떻게 변경할 수 있습니까?
. 위의 스크립트에서 UID
과 Password
을 제공하지 않고 어떻게 실행시킬 수 있습니까?
고맙습니다.
왜 $ resultsDataTable을 본문으로 보내지 않으십니까? 작동하는지 모르겠지만, 그보다 더 많은 작업이 필요하다고 생각합니다.하지만 적어도 쿼리 자체가 아니라 쿼리 결과를 얻게됩니다. –
아직 한 게시물에 여러 질문을하는 것이 좋지 않은 스타일로 간주됩니다. 스테프 테오 (StefTheo)가 귀하의 질문에 답변했지만 한 가지 대답 만 수락 할 수 있습니다. –