SQLPSX icon indicating copy to clipboard operation
SQLPSX copied to clipboard

Close opened SQL Session without closing PowerShell

Open scriptkiddy666 opened this issue 8 years ago • 0 comments

Hey Mike, I'm a little bit confused. What is the right way to open one single connection to an SQL Server via your module, so that every Get-Sql or Set-Sql command is using this session and how to close this session after several operations.

I managed this like the following:

$sqlsrv = Get-SqlServer -sqlserver $ServerName $sqldb = Get-SqlPSXDatabase -sqlserver $sqlsrv -dbname $DBName

My Get- and Set-SqlData now looks like this:

Get-SqlData -sqlserver $sqlsrv -dbname $sqldb -qry $SQLQuery

If I run my script this way, all seems to be quiet fine, but I'm unable to manually close this server and/or database connection. The server session is automatically closed, then I close my PowerShell session.

If I would establish several connections to different servers and/or different databases it would be fine to close some not need session within the script and not then the scripts terminates or?

Regards Markus

scriptkiddy666 avatar Mar 16 '17 14:03 scriptkiddy666