Skip to main content
deleted 3 characters in body
Source Link
RonDBA
  • 39
  • 1
  • 3

I've run a weekly report for management using PowerShell to execute the sql and e-mail the report to them. Here's a generic example

# Weekly Report
# Runs every Monday on using Task Scheduler of Windows OS.
# Sends report to manager

$sql =
@"
EXEC YourStoredProcedure;
"@

$FilePath = "C:\Users\UserName"

$OutFile = Join-Path -path $FilePath -childPath ("YourReport_" + (get-date).toString('yyyyMMdd_hhmmtt') + ".csv")


try {
    invoke-sqlcmd -query $sql -ServerInstance "YourSQLInstance" -Database "Database" -Username "UserName" -Password "" | Export-Csv $Outfile  -NoTypeInformation
    send-mailmessage -to "Joe User <[email protected]>" -from "Ronald Dameron <[email protected]>" -subject "Your Report" -SmtpServer "mail.domain.com" -Attachments $OutFile
} catch {
    send-mailmessage -to "Ronald Dameron <[email protected]>" -from "Ronald Dameron <[email protected]>" -subject "Report run FAILED!" -SmtpServer "mail.domain.com" -Body "Job failed to send weekly report." -Priority High
}

I've run a weekly report for management using PowerShell to execute the sql and e-mail the report to them. Here's a generic example

# Weekly Report
# Runs every Monday on using Task Scheduler of Windows OS.
# Sends report to manager

$sql =
@"
EXEC YourStoredProcedure;
"@

$FilePath = "C:\Users\UserName"

$OutFile = Join-Path -path $FilePath -childPath ("YourReport_" + (get-date).toString('yyyyMMdd_hhmmtt') + ".csv")


try {
    invoke-sqlcmd -query $sql -ServerInstance "YourSQLInstance" -Database "Database" -Username "UserName" -Password "" | Export-Csv $Outfile  -NoTypeInformation
    send-mailmessage -to "Joe User <[email protected]>" -from "Ronald Dameron <[email protected]>" -subject "Your Report" -SmtpServer "mail.domain.com" -Attachments $OutFile
} catch {
    send-mailmessage -to "Ronald Dameron <[email protected]>" -from "Ronald Dameron <[email protected]>" -subject "Report run FAILED!" -SmtpServer "mail.domain.com" -Body "Job failed to send weekly report." -Priority High
}

I've run a weekly report for management using PowerShell to execute the sql and e-mail the report to them. Here's a generic example

# Weekly Report
# Runs every Monday using Task Scheduler of Windows OS.
# Sends report to manager

$sql =
@"
EXEC YourStoredProcedure;
"@

$FilePath = "C:\Users\UserName"

$OutFile = Join-Path -path $FilePath -childPath ("YourReport_" + (get-date).toString('yyyyMMdd_hhmmtt') + ".csv")


try {
    invoke-sqlcmd -query $sql -ServerInstance "YourSQLInstance" -Database "Database" -Username "UserName" -Password "" | Export-Csv $Outfile  -NoTypeInformation
    send-mailmessage -to "Joe User <[email protected]>" -from "Ronald Dameron <[email protected]>" -subject "Your Report" -SmtpServer "mail.domain.com" -Attachments $OutFile
} catch {
    send-mailmessage -to "Ronald Dameron <[email protected]>" -from "Ronald Dameron <[email protected]>" -subject "Report run FAILED!" -SmtpServer "mail.domain.com" -Body "Job failed to send weekly report." -Priority High
}
Source Link
RonDBA
  • 39
  • 1
  • 3

I've run a weekly report for management using PowerShell to execute the sql and e-mail the report to them. Here's a generic example

# Weekly Report
# Runs every Monday on using Task Scheduler of Windows OS.
# Sends report to manager

$sql =
@"
EXEC YourStoredProcedure;
"@

$FilePath = "C:\Users\UserName"

$OutFile = Join-Path -path $FilePath -childPath ("YourReport_" + (get-date).toString('yyyyMMdd_hhmmtt') + ".csv")


try {
    invoke-sqlcmd -query $sql -ServerInstance "YourSQLInstance" -Database "Database" -Username "UserName" -Password "" | Export-Csv $Outfile  -NoTypeInformation
    send-mailmessage -to "Joe User <[email protected]>" -from "Ronald Dameron <[email protected]>" -subject "Your Report" -SmtpServer "mail.domain.com" -Attachments $OutFile
} catch {
    send-mailmessage -to "Ronald Dameron <[email protected]>" -from "Ronald Dameron <[email protected]>" -subject "Report run FAILED!" -SmtpServer "mail.domain.com" -Body "Job failed to send weekly report." -Priority High
}