Posted by: sajhamitra April 24, 2017
powershell
Login in to Rate this Post:     0       ?        
I would suggest you to use sqlps powershell module which can directly interact with SQL server instead of generic sqlclient.
You can run those command using invoke-sqlcmd.All those lines can be compressed to few lines.
Read Full Discussion Thread for this article