Getting "Execution Timeout Expired" Running SQL Server Backup through Invoke-Sqlcmd

So you have hit the famous 30 seconds timeout of the SMO object.

Grant Fritchey wrote about it back 2010. Read about it here: https://www.scarydba.com/2010/04/16/powershell-smo-problem/

If you set it = 0 - You will have told it to run infinite.

A more recent blog post about the same: https://fbrazeal.wordpress.com/2015/06/29/sqlps-tips/

Edited:

Depending on what version you're running, you should read this also: https://blogs.technet.microsoft.com/heyscriptingguy/2013/05/06/10-tips-for-the-sql-server-powershell-scripter/


Long question short answer you need to use parameter -Querytimeout X where X is time in seconds

query in question will work like this where 0 make infinite timeout we can use an integer value between 1 and 65535.

Invoke-Sqlcmd -ServerInstance "$serverInstance" -Querytimeout 0 -InputFile "C:\Program Files\DBBackups\dbfull_backups.sql" -Variable $sqlVariable -Verbose *> $outputFile