Skip to content

Long running query when using Invoke-SQL needs timeout setting #45

@PeterBizz

Description

@PeterBizz

Hi when using Invoke-SQL to restore a snapshot I encountered a time-out.
This left the database in restoring state.
Would be nice to have a timeout parameter added to Invoke-SQL, i.e. like this:
Invoke-SQL_SLOW -DatabaseServer $DatabaseServer -DatabaseName 'master' -SQLCommand $RestoreSnapshotSQL -Verbose -ErrorAction Stop -TimeOut 10000

Altered version of Invoke SQL:

function Invoke-SQL_SLOW {
    [CmdLetBinding()]
    param(
        [string] $DatabaseServer = [net.dns]::gethostname(),
        [String] $DatabaseInstance = '',
        [string] $DatabaseName = 'Master',
        [string] $SQLCommand = $(throw 'Please specify a query.'),
        [string] $TimeOut = 30
      )

    if (!([string]::IsNullOrEmpty($DatabaseInstance))){
        $DatabaseServer = "$($DatabaseServer)\$($DatabaseInstance)"
    }
    $connectionString = "Data Source=$DatabaseServer; Integrated Security=SSPI; Initial Catalog=$DatabaseName"

    write-Host -ForegroundColor Green "Invoke-SQL-SLOW with this statement on database '$DatabaseName':"
    Write-Host -ForegroundColor Gray $SQLCommand

    $connection = new-object system.data.SqlClient.SQLConnection($connectionString)
    

    $command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection)
    $command.CommandTimeout =$TimeOut
    $connection.Open()

    $adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
    $dataset = New-Object System.Data.DataSet
    
    $adapter.Fill($dataSet) | Out-Null

    $connection.Close()
    $connection.Dispose()

    $dataSet.Tables

    }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions