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 .
davesnbAsked:
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:
0
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
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"
0
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 .
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

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 https://www.mssqltips.com/sqlservertip/4606/generate-tsql-scripts-for-all-sql-server-databases-and-all-objects-using-powershell/ , outputs each individual object as a backup job
0
davesnbAuthor Commented:
any further ideas?
0
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)
https://blogs.technet.microsoft.com/heyscriptingguy/2010/11/04/use-powershell-to-script-sql-database-objects/
0

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
0
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..
0
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
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.