Well, figured it out. The scripter can do any combination of 2 things:
- output the script to an in memory
StringCollection
- output the script to a file determined by
scripter.Options.FileName
When scripting to a string collection, for some reason it does not script batch terminator GO
between sp calls, however it does when scripting to a file. So I changed the above code to look like this:
var scripter = new Scripter(server)
{
Options = new ScriptingOptions()
{
IncludeIfNotExists = true,
IncludeDatabaseRoleMemberships = true,
//ADDED THIS OPTION
FileName = Path.Combine(path, "Script.sql"),
// lots of other options here
}
};
foreach (User smoObject in database.Users)
{
var sc = scripter.Script(new Urn[] { smoObject.Urn });
// no need to write to file anymore since scripter automatically does it
}
The result was:
/****** Object: User [myuser] Script Date: 12/10/2013 5:00:57 PM ******/
IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = N'myuser')
CREATE USER [myuser] WITHOUT LOGIN WITH DEFAULT_SCHEMA=[dbo]
GO
sys.sp_addrolemember @rolename = N'r_execprocs', @membername = N'myuser'
GO
sys.sp_addrolemember @rolename = N'db_owner', @membername = N'myuser'
GO
sys.sp_addrolemember @rolename = N'db_datareader', @membername = N'myuser'
GO
sys.sp_addrolemember @rolename = N'db_datawriter', @membername = N'myuser'
GO