Modify the script to have your server in sServer and your database in sDatabase. By default the output will be in a directory on the C:\<databasename> . This should also be changed but is left as an exercise for the reader.
I attempted to use DMO to exactly reproduce the output that I get from Microsoft SQL Server Enterprise manager when I choose a database and then "Generate SQL Scripts" and choose the following settings:
General:
Script All Objects
Formatting:
Generate the CREATE command for each object
Generate the DROP command for each object
Generate scripts for all dependent objects
Include extended properties
Options:
Script database
Script database users and database roles
Script SQL Server Logins (Windows and SQL Server logins)
Script object-level permissions
Script indexes
Script full-text indexes
Script PRIMARY keys, FOREIGN keys, defaults, and check constraints
Windows text (ANSI)
Create one file per object
However, I do not want it to be exactly like that, as some things I do not want to delete first, but rather check if they exist first then only add them if they do. Specifically: Defaults,Users, Roles, and Logins. It turns out that when I tried to do this, generating the scripts for views, would for some reason trigger making several other objects at the same time regardless of your settings. So it required being made differently all by itself. I am posting this code here, because basically I have yet to see it all in one place. So in the spirit of the season, and just because I am an all around nice guy. Here is the code in VBScript. It is ugly and not commented at all... but I think you can get the point...Replace the values in sServer and sDatabase with stuff that works for you.