I have not tested the script, but I am interested to hear of the use case.
Why would you do this in preference to simply doing a schema compare in a VS database project (database --> project)? This will script out all (or selected) objects for you, without needing any code at all.