Toggle "Unknown Sources" with Tasker
This can't be done in a totally automated fashion, as indicated by the accepted answer to this StackOverflow question. Basically, it's against the Android security model to allow Unknown Sources to be changed programmatically.