You can't use wait like that because it doesn't know who "picked unit" is by the time the wait is done.
What you could do is save the group into a variable, do the loop as is, except without the wait and without the removing invulnerable part. Then have a the wait after the loop. Then do the loop again over the same group (using the variable) and remove invulnerable.
You would have to work out which town hall owns which group so that you don't overlap and cause bugs.
--------------------------------------------------
Another solution could be to temporarily give those peons an inventory (if they don't have one normally) and give them a powerup that has divine shield on it.