I hope this script does not become infamous for the wrong reasons! Please use caution.
I had to help a team recreate everything in a database and test their scripts but leave the roles and role grants in place. Basically, this meant that I could have scripted out the permissions and recreated the database but I thought it would be easier and more re-runnable to just drop everything else except the permissions.
Caution here means making sure you have good backups beforehand, ensuring that you pick the right database, and double-checking everything.