Invoke sqlcmd : Execution timeout expired

Mario2286 441 Reputation points
2022-07-27T04:21:42.67+00:00

I m executing this Powershell scripts but getting execution timeout expired so i have added the query timeout in the Powershell scripts but still getting the same error. Does anyone have any idea?

 $InstanceList = "DB03"  
 $filepath = "D:\cc8710\script1.sql"  
  
      
 $InstanceList |  
     ForEach-Object{  
         $databases = invoke-sqlcmd -ServerInstance $_ -Database "master" -Query "select name from sys.databases where name like 'mms_c%' and name != 'lms'" -Querytimeout 0  
      
         foreach ($database in $databases) {  
             #Execute scripts   
             Invoke-Sqlcmd -ServerInstance $_ -Database $database.name -InputFIle $filepath    
                   
         }  
 }  

225115-image.png

Windows for business Windows Server User experience PowerShell
SQL Server Other
{count} votes

Accepted answer
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2022-07-27T09:40:02.46+00:00

    You have -Querytimeout 0 for the query against sys.databases. That is a query that should always return immediately. But then you are running your script in some databases, but here you don't have -Querytimeout 0. That is, I think you need:

       foreach ($database in $databases) {  
                     #Execute scripts   
                     Invoke-Sqlcmd -ServerInstance $_ -Database $database.name -InputFIle $filepath  -Querytimeout 0  
    
    1 person found this answer helpful.

4 additional answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2022-07-27T05:28:49.937+00:00

    -ServerInstance $_

    What's $_ here?
    The error message is clear, the SQL Server, what ever is addressed with $_ isn't accessable.


  2. YufeiShao-msft 7,146 Reputation points
    2022-07-27T09:27:19.383+00:00

    HI @Mario2286 ,

    Use $_.name, you need to pipe the list of the SQL Server to foreach and provide the instance to the -ServerInstance parameter of the Invoke -Sqlcmd cmdlet, please check the instance
    https://devblogs.microsoft.com/powershell/invoke-sqlcmd-is-now-available-supporting-cross-platform/

    -------------

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Limitless Technology 39,916 Reputation points
    2022-07-27T14:24:02.52+00:00

    Hi,

    It would appear from the error message that the SQL instance or server is not available, and therefore the request is timing out. You have set the timeout for the query to 0, which means an infinite amount of time, therefore it's more likely to be a connection issue than a DB size issue.

    ------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept as answer--


  4. Tom Phillips 17,771 Reputation points
    2022-07-27T16:09:08.507+00:00

    From the error message, your script is erroring on the 2nd Invoke, not the first.

    Invoke-Sqlcmd -ServerInstance $_ -Database $database.name -InputFIle $filepath

    You should set a long QueryTimeout, like 120, on it and see what happens. Not QueryTimeout=0

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.