2017-05-04 14 views
0

我有一個查詢數據庫的函數。我希望通過電子郵件以excel格式發送查詢結果,而不需要人爲交互。這可能嗎?如何以Excel格式發送PowerShell功能

$Query="SELECT T1.Author, 
     T1.Revision, 
     T1.Msg AS LogNumber, 
     T1.Path as Checkin, 
     T2.ThePath as DS_Dependencies 
    FROM Table AS T1 
    INNER 
    JOIN Table AS T2 
    ON T1.condition = T2.condition" 

# redirecting one output stream to the success output stream by using &1 
$output = Invoke-Sqlcmd -ServerInstance "sqlInstance" -Database "Database" -Query $Query | Out-String 

if ($Output){ 
    Write-Output "Sending Report" 
    $MailUsername = "abc" 
    $MailPassword = "xyz" 
    $cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($MailUsername,(ConvertTo-SecureString -String $MailPassword -AsPlainText -Force)) 
    Send-MailMessage -To "[email protected]" -From "[email protected]" -SmtpServer mail.test.net -Credential $cred -Subject "Hey testing:" -Body "$Output" 
} 

回答

0

使用

$output | Export-CSV 

以及CSV通過電子郵件發送給自己。然後你可以加載它在Excel中的導入功能:)