From the course: PowerShell for SQL Server Administration

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Closing connections

Closing connections - PowerShell Tutorial

From the course: PowerShell for SQL Server Administration

Start my 1-month free trial

Closing connections

- [Instructor] So one thing that's going to be really important when you're writing PowerShell code is to close connections properly. This is one of the biggest things I see when it comes to people running code in PowerShell, code doesn't properly close connections. So connections in PowerShell do not close automatically. Because PowerShell is .NET-based, all the same rules are going to apply that apply in .NET, and that includes the fact that things don't close automatically. When you finally close the IDE in the PowerShell script, then things are going to close. But if you've got a script that's going to be running for an hour or two, and it's going to be doing a lot of different things, and a lot of creating connections and using those connections for stuff, those connections are not going to close until you actually close them. So this can lead to thread starvation on your SQL Server. This can lead to application problems, 'cause users are getting error messages, 'cause now they…

Contents