电子邮件价值来自变量&超链接

时间:2015-06-24 12:27:05

标签: email powershell

在PowerShell中,我知道如何发送基本电子邮件。但是使用下面的语法,我怎样才能将每个$QueryName和每个$RowCount添加到电子邮件的正文中,并添加指向$FPath\$FormattedDate\中包含的值的超链接,以便电子邮件的正文看起来像像这样:

$QueryName - $RowCount 

(或实际数据)

Santa - 14
Mickey - 12
Mars - 2

这是我目前的PS脚本

Function Execute-SQLquery {
param ($QueryName, $QueryString)

$server = "Server"
$database = "DB1"
$FPath = "C:\Testing"

#Setting additional variables
$extension = ".csv" 
$date = Get-Date -f 'MM.dd.yy'
$FormattedDate = Get-Date -f 'MM.dd.yy'

$connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connectionString = [string]::Format($connectionTemplate, $server, $database)
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString

$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $QueryString
$command.Connection = $connection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$rowCount = $SqlAdapter.Fill($DataSet)

if(!(Test-Path -path "$FPath\$FormattedDate\")){New-Item "$FPath\$FormattedDate\" -type directory}

if ($rowCount -gt 0)
{
    if ($QueryName -eq "Santa")
    {
        $extractFile = "C:\Testing\TemplateFiles\Santa.csv"
        [System.IO.Directory]::CreateDirectory("$FPath\$FormattedDate\Santa\")
        Write-Host $rowCount -fore Red
        $dirName = "$FPath\$FormattedDate\Santa\"
        $filename = [IO.Path]::GetFileNameWithoutExtension($extractFile) + "_$date" + [IO.Path]::GetExtension($extractFile)
        $extractFile = Join-Path $dirname $filename
    }
    if ($QueryName -eq "Mickey")
    {
        $extractFile = "C:\Testing\TemplateFiles\Mickey.csv"
        [System.IO.Directory]::CreateDirectory("$FPath\$FormattedDate\Mickey\")
        Write-Host $rowCount -fore Red
        $dirName = "$FPath\$FormattedDate\Mickey\"
        $filename = [IO.Path]::GetFileNameWithoutExtension($extractFile) + "_$date" + [IO.Path]::GetExtension($extractFile)
        $extractFile = Join-Path $dirname $filename
    }
    if ($QueryName -eq "Mars")
    {
        $extractFile = "C:\Testing\TemplateFiles\Mickey\Mars.csv"
        [System.IO.Directory]::CreateDirectory("$FPath\$FormattedDate\Mars\")
        Write-Host $rowCount -fore Red
        $dirName = "$FPath\$FormattedDate\Mars\"
        $filename = [IO.Path]::GetFileNameWithoutExtension($extractFile) + "_$date" + [IO.Path]::GetExtension($extractFile)
        $extractFile = Join-Path $dirname $filename
    }
    $DataSet.Tables[0] | Export-Csv $extractFile -NoTypeInformation
} 
$connection.Close()
}

1 个答案:

答案 0 :(得分:1)

首先,由于基于$QueryName的唯一更改是直接引用$QueryName$extractFile中的值,因此最好不要重复整个街区。

对于邮件消息,您可以使用Send-MailMessage

要添加指向本地文件资源的链接,请使用file:///方案前缀并将所有反斜杠(\)更改为正斜杠(/),即。 file:///C:/Document/Path.ext或您的示例"file:///$("$FPath\$FormattedDate" -replace '\','/')"

Function Execute-SQLquery {
    param ($QueryName, $QueryString)

    # up to this point no change is required

    if ($rowCount -gt 0)
    {
        $extractFile = switch($QueryName){
            "Santa"  { "C:\Testing\TemplateFiles\Santa.csv" }
            "Mickey" { "C:\Testing\TemplateFiles\Mickey.csv" }
            "Mars"   { "C:\Testing\TemplateFiles\Mars\Mickey.csv" }
            default  { throw "Illegal QueryName" }
        }

        [System.IO.Directory]::CreateDirectory("$FPath\$FormattedDate\$QueryName\")
        Write-Host $rowCount -fore Red
        $dirName = "$FPath\$FormattedDate\$QueryName\"
        $filename = [IO.Path]::GetFileNameWithoutExtension($extractFile) + "_$date" + [IO.Path]::GetExtension($extractFile)
        $extractFile = Join-Path $dirname $filename

        $DataSet.Tables[0] | Export-Csv $extractFile -NoTypeInformation

        $EmailBody = @'
Here are the results:

{0} - {1}

Find the documents <a href="file:///{2}">here</a>
'@ -f $QueryName,$rowCount,$("$FPath\$FormattedDate" -replace '\','/')

        Send-MailMessage -From "me@company.example" -To "you@company.example" -Body $EmailBody -BodyAsHtml:$true -Subject "Data extracted!" -SmtpServer "your.mail.server.company.example"
    } 
    $connection.Close()
}