How do I programmatically create a list of new users?
If you are having trouble with drush_invoke, try drush_invoke_process, which has two advantages over drush_invoke:


It is executed in a separate process, so you can set options explicitly, and do not need to worry about option "pollution" from previous calls to drush_set_option()
The results of the operation, including all log messages, is returned as structured php data, so you have a better idea of what was done.


Example:

$values = drush_invoke_process("@self", "user-add-role", array("Premium members"), array('mail' =&gt; $val));


You will find the output of the command in $values['output'], and structured results (if available) in $values['object'].

Note that in general, there is nothing wrong with calling drush_invoke as you are doing above; you may use that technique if it works for your use case. It is likely to be faster, but may sometimes be fragile.