Generate Scripts using SQL powershell

Hello EE,

I wish to 'generate scripts' from a database in sql using powershell and save the TSQL to a text file /sql file . In Sql Server Management Studio ( SSMS) it looks like ;

right click db , "tasks', 'generate scripts' , choose 'select specific database objects' , select boxes ' views , stored procedures,  user-defined functions' . Then under 'Advanced' , I select Script Object-Level Permissions =       True ,  Script DROP and CREATE =       Script DROP and CREATE . I then select , " Save to new query window " and the script is available.

I wish to automate this in a script using powershell .
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
Jose Gabriel Ortega CastroCEO Faru Bonon IT - EE Solution ExpertCommented:
Can you be more specific?
What do you want?
A PS script to drop and create a database?

If you can have the PS script with the TSQL saved into a Text file (.sql) and with PowerShell just call it.
Read the text and execute a query, but you'll need to set some parameters like authentication time, server, timeout, trusted connection?
Whatever contains a "typical Connection String"
davesnbAuthor Commented:
I want to be able to "generate the script" , TSQL output  for the actions specified above . Similar to when you generate a script from a database using the SSMS steps outlined above .
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

davesnbAuthor Commented:
I wish to have it in one master script  as Script Object-Level Permissions =       True ,  Script DROP and CREATE =       Script DROP and CREATE . Similar to how SSMS outputs it . The script mentioned on , outputs each individual object as a backup job
davesnbAuthor Commented:
any further ideas?
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
>> outputs each individual object as a backup job

Yes, that's correct and I've used it once earlier..

Since you wanted more customization to the script, I would suggest you to debug the Microsoft.SqlServer.SMO Assembly to see all configurations and then modify it to suit your needs.(I haven't used this and hence try it once in your local before doing it in Prod machines)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
davesnbAuthor Commented:
OK great , still looking for the 'drop and create' and 'objectlevel permissions' though
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
I hope that is already part of the link provided.. Let me clarify it in the other question thread..
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.