By: Joe Gavin | Updated: 2019-01-02 | Comments (1) | Related: > Express Edition
Problem
You need to automate running some SQL, but the is you need to run it directly on an Express Edition of SQL Server that doesn't have the SQL Server Agent we know and love. How can you automate tasks for SQL Server Express?
Solution
PowerShell provides an easy way to script SQL tasks and the native Windows Task Scheduler, while not as capable as the SQL Server Agent will work quite nicely for us.
Example Query - Find Latest Backup for All Databases
For example, say we want to get an automated report of the last database backup for each database on a server automatically emailed in a format that can be opened with Excel. We'll start with a SQL query to gather the data, demonstrate three different ways to run the query with the Invoke-SqlCmd PowerShell cmdlet, redirect the output to a .csv file, email the .csv and finally run all of it against more than one SQL Servers.
This query from this blog will show us what we need. I've just added one field to show the server name which will come in handy later.
SELECT @@SERVERNAME AS ServerName, sdb.Name AS DatabaseName, COALESCE(CONVERT(VARCHAR(12), MAX(bus.backup_finish_date), 101),'-') AS LastBackUpTime FROM sys.sysdatabases sdb LEFT OUTER JOIN msdb.dbo.backupset bus ON bus.database_name = sdb.name GROUP BY sdb.Name
Running it from SQL Server Management Studio (SSMS) shows a nice clean output like this:
The next step is to call it with the PowerShell cmdlet Invoke-SqlCmd.
First example, we'll call the SQL query from the input file C:\scripts\GetLatestBackupDates.sql with the Invoke-SqlCmd -InputFile switch.
# pass sql script $sqlserver = "JGAVIN-L\SQL2016" Invoke-Sqlcmd -ServerInstance $sqlserver -Database msdb -InputFile "C:\scripts\GetLatestBackupDates.sql"
Second example, we'll call the SQL directly on the command line Invoke-SqlCmd -Query switch.
# call sql direct on command line $sqlserver = "JGAVIN-L\SQL2016" Invoke-Sqlcmd -ServerInstance $sqlserver -Database msdb -Query "SELECT sdb.Name AS DatabaseName, COALESCE(CONVERT(VARCHAR(12), MAX(bus.backup_finish_date), 101),'-') AS LastBackUpTime FROM sys.sysdatabases sdb LEFT OUTER JOIN msdb.dbo.backupset bus ON bus.database_name = sdb.name GROUP BY sdb.Name"
Third example, still using -Query but the SQL is declared in a PowerShell variable that is passed to it.
# declare sql in a variable and pass it to -Query switch $sqlserver = "JGAVIN-L\SQL2016" $sql=" SELECT sdb.Name AS DatabaseName, COALESCE(CONVERT(VARCHAR(12), MAX(bus.backup_finish_date), 101),'-') AS LastBackUpTime FROM sys.sysdatabases sdb LEFT OUTER JOIN msdb.dbo.backupset bus ON bus.database_name = sdb.name GROUP BY sdb.Name " Invoke-Sqlcmd -ServerInstance $sqlserver-Database msdb -Query $sql
Running each of these shows all three methods give us the same output.
Email SQL Server Backup Query Results as an Attachment
Next, before we can email an attached file we must create it. The output is piped to a .csv with the Export-Csv, then attach it to an email and send it with Send-MailMessage. We'll stay with our third example to keep it more readable.
- Add variables to define a file name, mail server, email from and email to
- Pipe the output to a .csv thru the Export-Csv cmdlet
- Use the cmdlet Send-MailMessage to email it as an attachment
# declare sql in a variable and pass it to -Query switch $sqlserver = "JGAVIN-L\SQL2016" $outfile = "$env:TEMP\LastBackupTimes.csv" # name of file to email $PSEmailServer = "smtp.domain.ext" # mail server name $emailfrom = "[email protected]" # doesn't have to be real email, just in the form [email protected] $emailto = "[email protected]" $sql=" SELECT sdb.Name AS DatabaseName, COALESCE(CONVERT(VARCHAR(12), MAX(bus.backup_finish_date), 101),'-') AS LastBackUpTime FROM sys.sysdatabases sdb LEFT OUTER JOIN msdb.dbo.backupset bus ON bus.database_name = sdb.name GROUP BY sdb.Name " # delete old out file if it exists If (Test-Path "$outfile"){Remove-Item "$outfile"} # run sql and export to .csv Invoke-Sqlcmd -ServerInstance $sqlserver -Database msdb -Query $sql | Export-Csv $outfile -NoTypeInformation # email report file Send-MailMessage -To $emailto -From "$emailfrom" -Subject "Latest Backup Dates" -Attachments $outfile
Before we schedule it let's make it a little more useful by adding the capability to run this on more than one SQL Server. We just need to do a couple of more things.
- Change the $sqlserver variable to an array called $sqlservers and enter the SQL Server names to it quoted and comma separated
- Add a -Append to the Export-Csv so we don't overwrite the file
- Save the script as GetLatestBackupDates.ps1
# declare sql in a variable, pass it to -Query switch, export to .csv and send as mail attachment Param ([array] $sqlservers = @("JGAVIN-L\SQL2016","JGAVIN-L\SQL2017")) $outfile = "$env:TEMP\LastBackupTimes.csv" # name of file to email $PSEmailServer = "smtp.domain.ext" # mail server name $emailfrom = "[email protected]" # doesn't have to be real email, just in the form [email protected] $emailto = "[email protected]" $sql=" SELECT @@SERVERNAME AS ServerName, sdb.Name AS DatabaseName, COALESCE(CONVERT(VARCHAR(12), MAX(bus.backup_finish_date), 101),'-') AS LastBackUpTime FROM sys.sysdatabases sdb LEFT OUTER JOIN msdb.dbo.backupset bus ON bus.database_name = sdb.name GROUP BY sdb.Name " # delete old out file if it exists If (Test-Path "$outfile"){Remove-Item "$outfile"} # run sql and export to .csv Foreach ($sqlserver in $sqlservers) { Invoke-Sqlcmd -ServerInstance $sqlserver -Database msdb -Query $sql | Export-Csv $outfile -NoTypeInformation -Append # } # email report file Send-MailMessage -To $emailto -From "$emailfrom" -Subject "Latest Backup Dates" -Attachments $outfile
And here is the Excel file that we will email.
Schedule Task Scheduler Job for SQL Server Express
Finally, we can get to the point of this tip and automate the task without SQL Server Agent with Windows Task Scheduler.
Open Windows Task Scheduler
- Actions
- Create Basic Task
- Name task
- Document the jobs purpose
- Click Next
- Choose Trigger
- Click Next
- Start date
- Start time
- Frequency
- Click Next
- Click Next
- Enter the script command (using correct file location): powershell.exe -File C:\scripts\GetLatestBackupDates.ps1
- Click Yes
- Click Next
- Change User or Group…
- Search for service account to that will run job
- Select ‘Run whether user is logged on or not' radio button
- Click OK
Right click on the job and click Run.
When status goes to Ready (may have to hit F5 to refresh) make sure the "Last Run Result" is "The operation completed successfully" and verify you received the emailed attachment.
Summary
To review, we've seen three ways to call SQL from PowerShell, direct the output to a .csv file, run it against more than one SQL Server, email the .csv as an attachment and automate it without SQL Server Agent.
There are several other uses for the PowerShell, Invoke-SqlCmd cmdlet.
Next Steps
- Here are some links for further reading:
About the author
This author pledges the content of this article is based on professional experience and not AI generated.
View all my tips
Article Last Updated: 2019-01-02